Paginate来自多变量查询mysql数据库

时间:2012-06-07 16:29:57

标签: php mysql pagination

我有一个mysql数据库并使用多变量搜索页面find.php来输入变量。结果是正确的(计数是正确的,结果的第1页也是如此)但是当我尝试转到下一页时我得到一个错误::未定义索引:term1第60行::未定义索引:term2第61行等等上。 Search2.php如下所示:

<?php

include "db.inc.php";

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 15;

$term1 = $_POST['term1'];
$term2 = $_POST['term2'];
$term3 = $_POST['term3'];
$term4 = $_POST['term4'];

$sql ="SELECT * FROM cdrequests WHERE pname  LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%' LIMIT $start_from, 15";


$rs_result = mysql_query ($sql);
$num_rows = mysql_num_rows($rs_result);
$query = mysql_query("SELECT * FROM cdrequests WHERE pname  LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'"); 
$number=mysql_num_rows($query); 
print "<font size=\"5\" color=white><b>CD Requests</b></font> </P>";
?>

分页结构

<?php 

$sql = "SELECT COUNT(id) FROM cdrequests WHERE pname  LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'";  
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0];
$total_pages = ceil($total_records / 15);


/******  build the pagination links ******/
// range of num links to show
$range = 3;



// if not on page 1, don't show back links
if ($page > 1) {
   // show << link to go back to page 1
   echo " <a href='search2.php?page=1'><b>First</b></a> ";
   // get previous page num
   $prev = $page - 1;
   // show < link to go back to 1 page
   echo " <a href='search2.php?page=$prev'><b>&laquo;</b></a> ";
} // end if 



// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $total_pages)) {
      // if we're on current page...
      if ($x == $page) {
         // 'highlight' it but don't make a link
         echo " <font  size='5' color=yellow><b> $x </b></font> ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='search2.php?page=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for


// if not on last page, show forward and last page links        
if ($page != $total_pages) {
   // get next page
   $next = $page + 1;
    // echo forward link for next page 
   echo " <a href='search2.php?page=$next'><b>&raquo;</b></a> ";
   // echo forward link for lastpage
   echo " <a href='search2.php?page=$total_pages'><b>Last</b></a> ";
} // end if
/****** end build pagination links ******/

echo '</table>';

?>

以某种方式进入第2页未能传递变量项1,term2等的正确信息。 任何想法/帮助赞赏

1 个答案:

答案 0 :(得分:0)

您的网页链接未将term1term2等传回服务器。此外,如果您要在链接中传递它们,则需要检查$_REQUEST['term1']$_REQUEST['term2']等,以涵盖GET和POST请求。

链接的代码应该是这样的:

echo " <a href='search2.php?page=$next".
    "&amp;term1=".urlencode($term1).
    "&amp;term2=".urlencode($term2).
    "&amp;term3=".urlencode($term3).
    "&amp;term4=".urlencode($term4)"'><b>&raquo;</b></a> ";

如果您有太多参数传递给服务器,那么您应该考虑在使用JavaScript单击链接时发送POST请求。