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

时间:2012-02-09 07:56:20

标签: php pagination

我的问题:--- enter image description here

这是我的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&lt;&lt; 1 2 3&gt;最后 - &gt;这是正常的

<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>            

3 个答案:

答案 0 :(得分:2)

您的表单方法已获取。 你可以替换

$rowsperpage = 10;

$rowsperpage = isset($_GET['results_page']) && is_numeric($_GET['results_page']) ? $_GET['results_page'] : 10;

答案 1 :(得分:1)

你已经有一个变量来完成这项工作。您只需要从正在发送的$ _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;
}

答案 2 :(得分:0)

这是我最后的分页编码:

     // number of rows to show per page
    //$rowsperpage = 10; //I COMMENTED THIS AND WRITEN FOLLOWING
/***********************************************************/
    if(isset($_GET['results_page']) && is_numeric($_GET['results_page']))
    {
        $rowsperpage = (int)$_GET['results_page'];
    }
    else
    {
        $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);

这是我的记录显示

//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

而且这是我在链接和组合框中的分页

/******  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&results_page=$rowsperpage'>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&results_page=$rowsperpage'><</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&results_page=$rowsperpage'>$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&results_page=$rowsperpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&results_page=$rowsperpage'>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>

开心:)