ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP-我想在我的脚本中添加分页

2019-12-01 20:33:44  阅读:158  来源: 互联网

标签:pagination php


我的问题: – –

这是我的wifi_log.php文件

    <?php // find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM log_wifi";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
echo $resultpage= $_GET['results_page'];
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if


// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if

// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM log_wifi LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
?>

我的数据显示在这里:=

<?php

 //while there are rows to be fetched...
while ($row = mysql_fetch_assoc($result))
 {
   ?>
    <td><?php echo $row['id']?></td>
    <td><?php echo $row['user_id']?></td>                                       
    <td><?php echo $row['imei']?></td>
    <td><?php echo $row['time_stamp']?></td>
    <td><?php echo $row['sending_time']?></td>
    <td><?php echo $row['RSSI']?></td>
    <td><?php echo $row['BSSID']?></td>
    <td><?php echo $row['SSID']?></td>
    <td><?php echo $row['sleep_time_start']?></td>
    <td><?php echo $row['sleep_time_end']?></td>
    </tr>           
   <?php
} //// end while
?>

这是我的分页,例如FIRST<< 1 2 3>最后->这正常工作

<tr><th id="footer" colspan="10">

    <?php 
    /******  build the pagination links ******/
// range of num links to show
$range = 3;
if($currentpage==1)
{
    echo '<span class="prn">  First &lt;&lt;</span>&nbsp;';
} 
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&result_page=$resultpage'>First</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&result_page=$resultpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " <b>$x</b> ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&result_page=$resultpage'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&result_page=$resultpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&result_page=$resultpage'>Last</a> ";
} // end if
else
{
    echo '<span class="prn"> Last &gt;&gt;</span>&nbsp;';
}
/****** end build pagination links ******/

?>

问题是我该怎么做

<form action="./wifi_log.php" method="get">
   <select name="results_page" id="results_page" onchange="this.form.submit();" >
        <option value="5" NO >5</option>
        <option value="10" SELECTED >10</option>
        <option value="20" NO>20</option>
        <option value="50" NO >50</option>
    </select>       
     results per page
</form></th>
    </tr>
    </tbody>
</table>            

解决方法:

您已经有了一个可以完成工作的变量.您只需要从发送的$_POST变量中填充它即可.

// number of rows to show per page
if(isset($_GET['results_page']) && is_numeric($_GET['results_page']))
{
    $rowsperpage = (int)$_GET['results_page'];
}
else
{
    $rowsperpage = 10;
}

标签:pagination,php
来源: https://codeday.me/bug/20191201/2083196.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有