在选择选项n php分页后显示每页不同的行数?

时间:2014-04-22 06:20:15

标签: php html mysql pagination

$start_date = $_REQUEST['date1'];
$end_date = $_REQUEST['date2'];
$condition="1=1";
if($start_date!="")
$condition.=" AND event_date>='".date( "Y-m-d", strtotime($start_date))."'";
if($end_date!="")
$condition.=" AND event_date<='".date( "Y-m-d", strtotime($end_date))."'";
$start_date = date( "Y-m-d", strtotime($start_date)); 
$end_date = date( "Y-m-d", strtotime($end_date));
$link = mysql_connect('localhost', 'username', 'password');

if (!$link) 
 {
die('Could not connect: ' . mysql_error());
}
if($_REQUEST["dir"]=="" || $_REQUEST["dir"]=="desc")
$dir="asc";
else
    $dir="desc";

  if($_REQUEST["orderby"]!="")$ord = " ORDER BY ".$_REQUEST["orderby"];
  if($_REQUEST["dir"]!="")$ord .= " ".$_REQUEST["dir"];

 mysql_select_db("intern_db", $link);


$page = (intval($_GET['page'])>0) ? $_GET['page'] : 1;
$recordPerPage= '30';
$startPoint = ($page - 1)*$recordPerPage;
$result = mysql_query("SELECT count(*) cnt FROM ` admin_crmf_poc_event_history` where $condition");
        $row = mysql_fetch_array($result);
        $num_rows = $row["cnt"];

$result = mysql_query("SELECT * FROM ` admin_crmf_poc_event_history` where $condition $ord LIMIT $startPoint,$recordPerPage");
        $totalPages=ceil($num_rows/$recordPerPage);

if(!$totalPages){
   echo "There is no record.";
}
else{
   echo "$recordPerPage records are shown.";

   }
 echo "<table  width='100%'>

   <tr>
     <th><code><a href= 'index.php?page=".$_GET["page"]."&date1=".$_GET["date1"]."&date2=".$_GET["date2"].          "&orderby=id&dir=".$dir."'>Id</code></th>
     <th><a href= 'index.php?page=".$_GET["page"]."&date1=".$_GET["date1"]."&date2=".$_GET["date2"]."&orderby=event_date&dir=".$dir."'>Event Date</a></th>
     <th><a href= 'index.php?page=".$_GET["page"]."&date1=".$_GET["date1"]."&date2=".$_GET["date2"]."&orderby=bdm_name&dir=".$dir."'>BDM Name</th>
     <th><a href= 'index.php?page=".$_GET["page"]."&date1=".$_GET["date1"]."&date2=".$_GET["date2"]."&orderby=event_type&dir=".$dir."'>Event Performed</th>
     <th><a href= 'index.php?page=".$_GET["page"]."&date1=".$_GET["date1"]."&date2=".$_GET["date2"]."&orderby=completed&dir=".$dir."'>Completed</th>
   </tr>";

while($row = mysql_fetch_array($result))
    {
      echo "<tr>";
      echo "<td>" . $row['id'] . "</td>";
      echo "<td>" . date( "m/d/Y", strtotime($row['event_date'])) . "</td>";
      echo "<td>" . $row['bdm_name'] . "</td>";
      echo "<td>" . $row['event_type'] . "</td>";
      echo "<td>" . $row['completed'] . "</td>";
      echo "</tr>";
    }
 echo "</table>";
mysql_close($link);

上面的代码显示分页中的sql记录只显示每页30行,因为$recordPerPage= '30';。我想在html中创建一个包含30到100行选项的选择框,并将其传递给$recordPerPage,这样当我选择任何选项它将根据所选的选项显示每页的行数。提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

你必须改变

$recordPerPage= '30';

$recordPerPage= $_REQUEST ['recordPerPage'];

并将其用作选择框中的名称。

答案 1 :(得分:0)

您已经在代码中得到了答案,只需执行您为获取$ page变量所做的操作。如果出现问题,您也可以考虑将页码重置为1。我的意思是,如果用户在第2页并且将记录值从30更改为100,则将页面重置为1可能是个好主意。

$recordPerPage = (intval($_GET['records'])>0) ? $_GET['records'] : 1;