Web Hosting Talk







View Full Version : database question (php)


Gamenati
03-12-2003, 09:03 PM
I am trying to make a sort of a display results thing and i decided to practice on a table called 'news'. Each page shows 5 results. My question is how can i make the next page show the next 5 results. www.gamenationx.com/testes.php

here is the code so far:



<?php

$link_id = mysql_connect ("localhost", "gamenat_admin", "rmb1990") or
die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("gamenat_everything");

$records_per_page = "5" ;

$sql = "SELECT newsid FROM news LIMIT 5";
$query = mysql_query($sql);

$query2 = "SELECT count(*) FROM news" ;
$result = mysql_query($query2) ;
$querydata = mysql_fetch_row($result);
$total_num_news = $querydata[0] ;

$total_pages = ceil($total_num_news/$records_per_page) ;

$page_num = $cur_page + 1;

echo "Page $page_num out of $total_pages<br><br>";

while ($query_data = mysql_fetch_array($query)) {
echo $query_data[newsid] . "<br>" ;
}

?>




hope i made sense

Gamenati
03-13-2003, 07:47 AM
come on guys

PaulC-ehosting
03-13-2003, 08:14 AM
I havent tested this, but off the top of my head!

Replace your the line starting $sql with these lines
$sql = "SELECT newsid FROM news LIMIT ";
$sql .= ($cur_page-1)*$records_per_page . "," . $records_per_page
That should work, if not put "echo $sql" on the next line and post the output (so I can see the actual query)

Also, I would rewrite these 4 lines:
$query2 = "SELECT count(*) FROM news" ;
$result = mysql_query($query2) ;
$querydata = mysql_fetch_row($result);
$total_num_news = $querydata[0] ;
as
$result = mysql_query("SELECT count(*) FROM news");
$total_num_news = mysql_result($result,0,0);
It wont make much difference to the speed, but I fine it easier to see what the scripts doing.

Cheers,
Paul

Gamenati
03-13-2003, 04:25 PM
when i tried this:


<?php

$link_id = mysql_connect ("localhost", "gamenat_admin", "rmb1990") or
die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("gamenat_everything");

$records_per_page = "5" ;

$sql = "SELECT newsid FROM news LIMIT ";
$sql .= ($cur_page-1)*$records_per_page . "," . $records_per_page ;

$result = mysql_query("SELECT count(*) FROM news");
$total_num_news = mysql_result($result,0,0);

$total_pages = ceil($total_num_news/$records_per_page) ;

$page_num = $cur_page + 1;

echo "Page $page_num out of $total_pages<br><br>";

while ($query_data = mysql_fetch_array($query)) {
echo $query_data[newsid] . "<br>" ;
}

?>




it yeilded:


Page 1 out of 2


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/gamenat/public_html/testes.php on line 21


i also need to know how to make a link to the next page

PaulC-ehosting
03-13-2003, 04:47 PM
You need the command to run the sql query

Place this underneath the two $sql lines

$query = mysql_query($sql);

Gamenati
03-13-2003, 05:21 PM
ok, but how do i make a link to the next page with the next 5 results on it?

Gamenati
03-13-2003, 07:15 PM
anyone know?

iamdave
03-13-2003, 08:39 PM
This page gives you a detailed walk-through on how to do this. http://www.phpfreaks.com/tutorials/43/5.php