【问题标题】:to show different rows per page after select option n php pagination?在选择选项 n php 分页后每页显示不同的行?
【发布时间】:2014-04-22 14:51:15
【问题描述】:
$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,以便当我选择任何选项时它将根据选择的选项显示每页的行数。提前感谢您的帮助。

【问题讨论】:

  • mmmm....你在哪里卡住队友...任何错误/警告??
  • 不,我只想创建一个选择框。我知道如何创建它,但我不明白如何在选择选项上获取每页的行数
  • 谷歌搜索分页会节省你很多时间!! :D

标签: php html mysql pagination


【解决方案1】:

你必须改变

$recordPerPage= '30';

到

$recordPerPage= $_REQUEST ['recordPerPage'];

并在选择框中使用它作为名称。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多