PDA

View Full Version : Help with Pagination code...


gigiloumill
07-17-2009, 12:30 AM
Would someone tell me why I keep getting this error:

Parse error: syntax error, unexpected T_DNUMBER in C:\wamp\www\websites\sun\Untitled-3.php on line 10

<?php
02.// database connection info
03.$conn = mysql_connect('localhost','id','pass') or trigger_error("SQL", E_USER_ERROR);
04.$db = mysql_select_db('messages',$conn) or trigger_error("SQL", E_USER_ERROR);
05.
06.// find out how many rows are in the table
07.$sql = "SELECT COUNT(*) FROM message";
08.$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
09.$r = mysql_fetch_row($result);
10.$numrows = $r[0];
11.
12.// number of rows to show per page
13.$rowsperpage = 10;
14.// find out total pages
15.$totalpages = ceil($numrows / $rowsperpage);
16.
17.// get the current page or set a default
18.if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
19. // cast var as int
20. $currentpage = (int) $_GET['currentpage'];
21.} else {
22. // default page num
23. $currentpage = 1;
24.} // end if
25.
26.// if current page is greater than total pages...
27.if ($currentpage > $totalpages) {
28. // set current page to last page
29. $currentpage = $totalpages;
30.} // end if
31.// if current page is less than first page...
32.if ($currentpage < 1) {
33. // set current page to first page
34. $currentpage = 1;
35.} // end if
36.
37.// the offset of the list, based on current page
38.$offset = ($currentpage - 1) * $rowsperpage;
39.
40.// get the info from the db
41.$sql = "SELECT id, messagetxt FROM message LIMIT $offset, $rowsperpage";
42.$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
43.
44.// while there are rows to be fetched...
45.while ($list = mysql_fetch_assoc($result)) {
46. // echo data
47. echo $list['id'] . " : " . $list['messagetxt'] . "<br />";
48.} // end while
49.
50./****** build the pagination links ******/
51.// range of num links to show
52.$range = 3;
53.
54.// if not on page 1, don't show back links
55.if ($currentpage > 1) {
56. // show << link to go back to page 1
57. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
58. // get previous page num
59. $prevpage = $currentpage - 1;
60. // show < link to go back to 1 page
61. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
62.} // end if
63.
64.// loop to show links to range of pages around current page
65.for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
66. // if it's a valid page number...
67. if (($x > 0) && ($x <= $totalpages)) {
68. // if we're on current page...
69. if ($x == $currentpage) {
70. // 'highlight' it but don't make a link
71. echo " [<b>$x</b>] ";
72. // if not current page...
73. } else {
74. // make it a link
75. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
76. } // end else
77. } // end if
78.} // end for
79.
80.// if not on last page, show forward and last page links
81.if ($currentpage != $totalpages) {
82. // get next page
83. $nextpage = $currentpage + 1;
84. // echo forward link for next page
85. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
86. // echo forward link for lastpage
87. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
88.} // end if
89./****** end build pagination links ******/
90.?>

edbr
07-17-2009, 01:02 AM
not sure about this

$sql = "SELECT COUNT(*) FROM message";
replace it with
$sql = "SELECT * FROM message";
and see what happens then

gigiloumill
07-17-2009, 11:04 AM
Same thing but now I am getting this:

Parse error: syntax error, unexpected T_DNUMBER in C:\wamp\www\websites\sun\Untitled-3.php on line 11

gigiloumill
07-17-2009, 11:56 AM
Thanks E, I tried this one and it works but I can't figure out why when I click on other pages, the browser says "NO Page found". also, I was under the impression pagination would retrieve the first few lines. I am getting the complete story per field and not separated. It looks as one huge story. Any suggestions? Thanks again.

<?php
$username="name";
$password="pass";
$database="mysql";

$dbh=mysql_connect(localhost,$username,$password) or die ('I cannot connect to the database because: ' . mysql_error());
@mysql_select_db($database);


$result = mysql_query("SELECT COUNT(*) AS total_entries FROM help_topic") or die(mysql_error());
$row = mysql_fetch_row($result);


$total_entries = $row[0];



$entries_per_page = 15;


if(isset($_GET['page_number'])) {
$page_number = $_GET['page_number'];
} else {
$page_number = 1;
}



$total_pages = ceil($total_entries / $entries_per_page);


$offset = ($page_number - 1) * $entries_per_page;


$result = mysql_query("SELECT * FROM help_topic LIMIT $offset, $entries_per_page") or die(mysql_error());
while($obj = mysql_fetch_array($result)) {
// Display the data however you want here.
print $obj['name'];
echo "<br>";
}


for($i = 1; $i <= $total_pages; $i++) {
if($i == $page_number) {
// This is the current page. Don't make it a link.
print "$i ";
} else {
// This is not the current page. Make it a link.
print "<a href=\"yourpage.php?page_number=$i\">$i</a> ";
}
}
?>

gigiloumill
07-19-2009, 04:29 PM
Does anyone in this forum know anything about pagination?

edbr
07-21-2009, 09:38 AM
where di you get this from?

gigiloumill
07-21-2009, 10:59 AM
I got it done already. thanks