为什么我的分页代码不起作用?

时间:2013-08-31 17:34:41

标签: php pagination

我有这个分页代码,&pagenum=whatever在我的网址中显示完全正常,但它绝对没有任何内容,我真的不知道为什么。这是我的代码:

paginate.php:

 <?php

include("config.php"); 

 if (!(isset($pagenum))) 

 { 

 $pagenum = 1; 

 } 

$data = mysql_query("SELECT * FROM shop
    WHERE MATCH (name,description,keywords) AGAINST ('$search' IN BOOLEAN MODE)") or die(mysql_error()); 

 $rows = mysql_num_rows($data);  

 $page_rows = 5; 

 $last = ceil($rows/$page_rows); 

 if ($pagenum < 1) 

 { 

 $pagenum = 1; 

 } 

 elseif ($pagenum > $last) 

 { 

 $pagenum = $last; 

 } 

 $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

 ?>

的search.php:

<?php
include("config.php");
$search = mysql_real_escape_string($_GET['result']);
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<div id="wrapper">
<div class="searchleft">
</div>
<div class="search">
<center>
<form method="get" action="search.php">
<input type="text" name="result" value="<?php echo $search; ?>" />
<input type="submit" />
</form>
<?php   
include("paginate.php");

$data_p = mysql_query("SELECT * FROM shop
    WHERE MATCH (name,description,keywords) AGAINST ('$search' IN BOOLEAN MODE) $max") or die(mysql_error());

$num_rows = mysql_num_rows($data_p);

if ($num_rows == "1") {
    echo "Returned 1 result.";
} else { echo "Returned ".$num_rows." results."; }

while ($info = mysql_fetch_assoc($data_p)) {
    $name = stripslashes($info['name']);
    $desc = stripslashes($info['description']);
    $desc = substr($desc, 0, 150);
    $price = stripslashes($info['price']);
    Print "<div style=\"width:600px; height:150px; border:1px solid black; overflow:hidden\"><div style=\"height:148px; width:25%; border:1px solid red; float:left\"><center><img src=\"".$picture."\" height=\"120\" width=\"120\" style=\"margin-top:15px\" /></center></div><div style=\"height:150px; width:50%; border:1px solid blue; float:left; text-overflow: ellipsis; padding-top:5px\"><center><font size=\"+1\"><b><a href=\"result.php?product=".urlencode($name)."\">".$name."</b></a></font><br><br>".$desc."...</center></div><div style=\"height:150px; width:24%; border:1px solid green; float:left\"><center><h1>$".$price."</h1><button>Add to Cart</button></center></div></div>";
}
echo " --Page $pagenum of $last-- <p>";
 if ($pagenum == 1) 
 {

 } 

 else 

 {
 echo " <a href='{$_SERVER['PHP_SELF']}?result=".$search."&pagenum=1'> First</a> ";
 $previous = $pagenum-1;
 echo " <a href='{$_SERVER['PHP_SELF']}?result=".$search."&pagenum=$previous'>Previous</a> ";

 }  
  if ($pagenum == $last) 
 {
 } 
 else {
 $next = $pagenum+1;
 echo " <a href='{$_SERVER['PHP_SELF']}?result=".$search."&pagenum=$next'>Next</a> ";

 echo " ";

 echo " <a href='{$_SERVER['PHP_SELF']}?result=".$search."&pagenum=$last'>Last</a> ";

 } 

 ?> 

老实说,我很难过。

1 个答案:

答案 0 :(得分:1)

查看评论,而不是

if (!(isset($pagenum))) 
{ 
  $pagenum = 1; 
} 

你需要这个:

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

// or with a ternary line
$pagenum = (isset($_GET['pagenum'])) ? $_GET['pagenum'] : 1;

您的代码的这部分也在进行昂贵的数据库调用:

$data = mysql_query("SELECT * FROM shop WHERE MATCH (name,description,keywords) AGAINST ('$search' IN BOOLEAN MODE)") or die(mysql_error()); 
$rows = mysql_num_rows($data);

进行计数而不是取整个表:

$data = mysql_query("SELECT COUNT(*) FROM shop WHERE MATCH (name,description,keywords) AGAINST ('$search' IN BOOLEAN MODE)") or die(mysql_error()); 
$result = mysql_fetch_array($data, MYSQL_NUM);
$rows = $result[0];