开发者

PHP/MySQL pagination going wrong

Using PHP on my website linking to a library database in MySQL. The one area I can't seem to get right is the pagination of results. I've tried about twenty different code snippets and have the same problem each time - can't get more than the first page of results to display. Despite reading lots of forum posts on this one, can't get past it. My select query is slightly more complex in that several keywords can be combined and it is searching multiple fields. When results are displayed, they also pull data from different tables. I really hope someone can spot where I'm going wrong. Switch statement used dependent on which type of search the user requests, so the code for one case given below.

$term = trim($_REQUEST['term']);
$searchterm = mysql_real_escape_string($term, $link);
$index = $_REQUEST['index'];
switch ($index)
{
     case "title":
     $array = explode(" ", $searchterm); 
     $sql = "select identifier, title, publication_date from publication where";
     foreach ($array as $key => $keyword) { 
 $sql .= " (title like '%$keyword%' or alternative_title like '%$keyword%')"; 
 if ($key != (sizeof($array) - 1)) $sql .= " and "; 
 if ($key == (sizeof($array) - 1)) $sql .= " order by title"; 
     }
     if(!$result = mysql_query($sql, $link)) { 
 showerror();
}
else {
 $numrows = mysql_num_rows($result);
 if ($numrows == 0) {
  echo "<h2>No records found</h2>";
}
//start pagination
$perPage = 10; 
$page = (isset($_REQUEST['page'])) ? (int)$_REQUEST['page'] : 1; 
$startAt = $perPage * ($page - 1);  
$t开发者_StackOverflow中文版otalPages = ceil($numrows / $perPage);
$links = ""; 
for ($i = 1; $i <= $totalPages; $i++) {   
  $links .= ($i != $page ) ? "&nbsp;<a href='search.php?page=$i&search=$term'>
Page $i</a> " : "$page "; 
}   
echo "<h3>Displaying page $page of $totalPages:</h3><br />";
$counter = 1;
$sql = "select identifier, title, publication_date from publication where";
foreach ($array as $key => $keyword) { 
  $sql .= " (title like '%$keyword%' or alternative_title like '%$keyword%')"; 
  if ($key != (sizeof($array) - 1)) $sql .= " and "; 
  if ($key == (sizeof($array) - 1)) $sql .= " order by title 
LIMIT $startAt, $perPage"; 
}
$result = mysql_query($sql, $link);
echo "<table class=\"results\">\n"; 
while($row = mysql_fetch_array($result)) 
{
  //echo data
}
echo "</table><br />\n";
echo $links; // show links to other pages
echo "<p>&nbsp;</p>";
   }
break;


as stated above, -1 vote for placing someone elses code instead of trying it yourself. This code is wayyyy to much for what a pagination script should do. You could have added more specific code snippet and a better question. I'll try to take a look at it however.

As you can't get further than page 1 it seems that somethings going wrong aroung $page = (isset($_REQUEST['page'])) ? (int)$_REQUEST['page'] : 1 ; or a little below. Try echoëing your SQL code for each page and take a look if the LIMIT is right or that it stays the same, probably the last. At least that gives you a starting point from where to take a look in your code.

Does it give you the right amount of links to go to?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜