0

我想在分页结果中对我的多术语 sql 查询进行分页,第 1 页工作正常,但第 2 页 ..previous 或 next 不传递变量:

<?php


include "db.inc.php";

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

$term1 = $_REQUEST['term1'];
$term2 = $_REQUEST['term2'];
$term3 = $_REQUEST['term3'];
$term4 = $_REQUEST['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>";


print "<table class=\"table1\"    STYLE=\"word-wrap:break-word;\"   width=1100 border=\"1\"  bordercolor=\"#000000\" bgcolor=\"E6E6E6\" style=\"border-collapse: collapse\"   cellpadding=\"2\" cellspacing=\"1\"> .............
?>

<?php 

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


$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 "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size='4' color=white>Total Records</font> <font size='5' color=yellow><b>$number</b></font>";

echo '</table>';

?>

不知道我需要输入什么 echo " <a href='search2.php?page=$next'><b>&raquo;</b></a> ";才能调用条款

谢谢

4

2 回答 2

0

让自己更轻松。建表一次。

对它应用 jQuery 数据表

$('#table_id).datatables();

分页完成。真的就是这么简单!它只需要 jQuery 和DataTables 插件,以及几行 CSS。作为奖励,它会过滤、排序、限制等,每个所需的功能只需额外的代码行。此外,它可以使用 Themeroller 进行样式设置,制作出比大多数开发人员都更好看的桌子。

于 2012-06-12T16:30:39.607 回答
0

这不起作用吗?

...
$start_from=$page*15

$query = mysql_query("SELECT * FROM cdrequests WHERE ...");  
$all_rows = mysql_num_rows($query); 
$totalPages = ceil($all_rows/15)-1; #How many pages?
$sql = $query . " LIMIT $start_from,15"; 
print "<font size=\"5\" color=white><b>CD Requests</b></font> </P>";

print "<table class=\"table1\..."
$terms= '&term1='.$term1 . '&term2='.$term2 . '&term3='.$term3 . '&term4='.$term4;
if($totalPages>1){ 
#Paging Starts Now
?>
<div align="center">
<? if ($page > 1) { ?>
<a href="search2.php?page=<? echo ($page-1); echo $terms; ?>">Previous</a>
<a href="search2.php?page=1<? echo $terms; ?>">First</a>
<? } ?>
Page <? echo $page; ?> of <? echo $totalPages+1; ?>

<? if ($page < $totalPages) { ?>
<a href="search2.php?page<? echo ($page+1); echo $terms; ?>">Next</a>
<a href="search2.php?page<? echo ($totalPages+1); echo $terms; ?>">Last</a>
</div>
<? } }?>
于 2012-06-12T16:43:45.767 回答