Jump to content

Problem with pagination script.


nysmenu

Recommended Posts

Hi, I've used this script designed by CV and for some reason I can not get it to work. This is what i keep getting and I can't find a solution.

 

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

 

Has anyone ever been successful with this script? I need a script for a site I am building and can't seem to get one working correctly. Can someone please help. Thanks

 

1. <?php  
   2. // database connection info  
   3. $conn = mysql_connect('localhost','pass','pass') or trigger_error("SQL", E_USER_ERROR);  
   4. $db = mysql_select_db('mysql',$conn) or trigger_error("SQL", E_USER_ERROR);  
   5.   
   6. // find out how many rows are in the table   
   7. $sql = "SELECT COUNT(*) FROM hot_topic";  
   8. $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
   9. $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, url FROM hot_topic 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['url'] . "<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. ?>  

Link to comment
https://forums.phpfreaks.com/topic/166548-problem-with-pagination-script/
Share on other sites

i never did like the way the tut tells you how to get the total number of rows from the db, try the following instead

 

remove count() from * in your query and remove $r = mysql_fetch_row($result);  then set $numrows to mysql_num_rows($result);

 

that should return the correct values..

 

Hope it helps

 

Stuie

<?php  
// database connection info  
$conn = mysql_connect('localhost','pass','pass') or trigger_error("SQL", E_USER_ERROR);  
$db = mysql_select_db('mysql',$conn) or trigger_error("SQL", E_USER_ERROR);  

// find out how many rows are in the table   
$sql = "SELECT COUNT(*) FROM hot_topic";  
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
$r = mysql_fetch_row($result);  
$numrows = $r['0'];  

// number of rows to show per page  
$rowsperpage = 10;  
// find out total pages  
$totalpages = ceil($numrows / $rowsperpage);  

// get the current page or set a default  
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  

Thanks, I did like you said and still getting the same error. This script is weird, no matter what I do to it, it'll always come out with the same error. I tried without including a password and still the same error. You don't have a working code do you? I'm behind on this site and I can't find a solution. the other code I used (see it below) shows all the pages but , when view from page to page it'll always come back with the same info.

 

<?php
$username="pass";
$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 = 25;


if(isset($_GET['currentpage'])) {
$page_number = $_GET['currentpage'];
} 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 ['url'];
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=\"untitled-1.php?page_number=$i\">$i</a> ";
} 
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.