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

Php 为什么我的分页代码不起作用?,php,pagination,Php,Pagination,我有这个分页代码,&pagenum=无论什么在我的URL中显示得非常好,但它完全不起作用,我真的不知道为什么。这是我的密码: paginate.php: <?php include("config.php"); if (!(isset($pagenum))) { $pagenum = 1; } $data = mysql_query("SELECT * FROM shop WHERE MATCH (name,description,keywords)

我有这个分页代码,&pagenum=无论什么在我的URL中显示得非常好,但它完全不起作用,我真的不知道为什么。这是我的密码:

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> ";

 } 

 ?> 

老实说,我被难住了。

看评论,而不是

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];

$pagenum设置在哪里?我想是paginate.php的顶部。如果未设置,则设置为1。如果它已经设置好了,那么它就是它设置为的任何对象,而您可能希望使用$\u GET['pagenum']来代替它。嗯&pagenum=无论什么对象都可以很好地工作,只是它对页面没有任何作用是的,要访问URL中的变量,您需要使用$\u GET,就像您在另一个文件顶部使用$search一样。这工作得非常好。现在是个小问题……假设我有11个条目,可以产生三页。有没有一种简单的方法可以让我制作一个下拉列表,上面写着跳转到页面:然后下拉列表有1、2和3作为选项,或者有多少页面?