User1988
User1988

Reputation: 1997

I want to add pagination in my script

my problem:---enter image description here

This is my wifi_log.php file

    <?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);
?>

My data displaying here:=

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

Here is my pagination like FIRST << 1 2 3 >> LAST-->this is working properly

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

?>

Problem is here how to do that i know

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

Upvotes: 3

Views: 1057

Answers (3)

User1988
User1988

Reputation: 1997

This is my final pagination coding:

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

this is my records display

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

And fianlly this is my pagination in link and in combobox

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

Happy :)

Upvotes: 0

Dan
Dan

Reputation: 1888

You already have a variable there that does the job. You only need to fill it from the $_POST variable you are sending.

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

Upvotes: 1

Salman Khimani
Salman Khimani

Reputation: 515

Your form method is get. You can replace

$rowsperpage = 10;

with

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

Upvotes: 2

Related Questions