2012-02-09 4 views
3

мою проблему: --- 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 < < 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>    

ответ

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; 
} 
+0

, что писать в SQL запросе? @ Dan – User1988

+0

Вы можете оставить остальную часть страницы, как это. – Dan

+0

О, я просто понял, что вы используете вместо него пост. замените пост с $ _GET в моем коде, и он должен работать. – Dan

2

Ваша форма метода - это получить. Вы можете заменить

$rowsperpage = 10; 

с

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

ya его рабочий тон – User1988

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 

И fianlly это мое п agination в ссылке и в выпадающем списке

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

Смежные вопросы