douzhoubing2805 2014-10-11 12:50
浏览 39

PHP分页 - 只有1页有效

I have been messing around, and been trying to make this script work (for later implenting it in my own projects). It works fine, beside that I when I click on a new page, the results doesnt' change..

here is the script:

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 5; 
$sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, 20"; 
$rs_result = mysql_query ($sql); 
?> 
<table>
<tr><td>Name</td><td>Phone</td><td>Salary</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><?php echo $row['emp_id']; ?></td>
            <td><?php echo $row['emp_name']; ?></td>
            <td><?php echo $row['emp_salary']; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(emp_name) FROM employee"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 5); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination?page=".$i."'>".$i."</a> "; 
}; 
?>
  • 写回答

1条回答 默认 最新

  • donglu6303 2014-10-17 12:32
    关注

    Actually there seems nothing fundamentally wrong. Did you try a hard reload in your browser ?

    Use this in the unshown head part of your page to get rid of some caching issues:

    <html>
            <head>
                <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
                <META HTTP-EQUIV="Expires" CONTENT="-1">
                <meta http-equiv="cache-control" content="no-cache">
            </head>
    ...
    </html>
    

    I changed some things anyway:

    • $perpage contains the number of entries per page nice in one place
    • never use unsanitized values in sql so I (int)ed the $_GET

    --- need this line for the code to show ---

    <?php 
    $perpage = 5;
    
    if (isset($_GET["page"])) { $page  = abs ((int)$_GET["page"]); } else { $page=1; }; 
    $start_from = ($page-1) * $perpage; 
    $sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, $perpage"; 
    $rs_result = mysql_query ($sql); 
    ?> 
    <table>
    <tr><td>Name</td><td>Phone</td><td>Salary</td></tr>
    <? php 
    while ($row = mysql_fetch_assoc($rs_result)) { 
    ?> 
                <tr>
                <td><?php echo $row['emp_id']; ?></td>
                <td><?php echo $row['emp_name']; ?></td>
                <td><?php echo $row['emp_salary']; ?></td>
                </tr>
    <?php 
    }; 
    ?> 
    </table>
    <?php 
    $sql = "SELECT COUNT(emp_name) FROM employee"; 
    $rs_result = mysql_query($sql); 
    $row = mysql_fetch_row($rs_result); 
    $total_records = $row[0]; 
    $total_pages = ceil($total_records / 5); 
    
    for ($i=1; $i<=$total_pages; $i++) { 
                echo "<a href='pagination?page=".$i."'>".$i."</a> "; 
    }; 
    ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化