douruoshen1449
douruoshen1449
2014-04-14 02:32

用户的PHP分页?

已采纳

I've tried to make a pagination with PHP, but it seems to not work. I want to limit my data which showing in each page, I have this code and it uses PHP code:

showUsers.php

<?php 
$query = mysql_query("select * from users");

while ($data = mysql_fetch_array($query)) {
?>

<td><?php echo $data['no_peg']; ?></td>
<td>
    <?php 
    echo $data['username']; 

    //previlage admin
    if ($_SESSION['role'] == 'admin') {
    ?>
        <div class="row-actions">
        <a href="edit.php?uid=<?php echo $data['id_user'];?>">Edit</a>
        <?php if ($data['role'] != 'admin') {?>
         | <a href="delete.php?uid=<?php echo $data['id_bio'];?>" onClick="return confirm('Hapus akun ini?')" class="delete">Delete</a>
        <?php } ?>
    </div>
    <?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>

I want to show just 10 names per page, to avoid long scrolling, but how can it work?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douhan4093 douhan4093 7年前
    <?php 
    
    $sqlCount = "select count(id_user) from users";   
    $rsCount = mysql_fetch_array(mysql_query($sqlCount));  
    $totalData = $rsCount[0];  
    $page = isset($_GET['page']) ? $_GET['page'] : 1;  
    $limit = 10;  
    $start_from = $limit * ($page - 1);  
    $sql_limit = "SELECT * FROM users limit $start_from, $limit";  
    $result = mysql_query($sql_limit);  
    
    
    
            while ($data = mysql_fetch_array($result)) {
            ?>
    
                <td><?php echo $data['no_peg']; ?></td>
                <td>
                    <?php 
                    echo $data['username']; 
    
                    //previlage admin
                    if ($_SESSION['role'] == 'admin') {
                    ?>
                        <div class="row-actions">
                        <a href="edit.php?uid=<?php echo $data['id_user'];?>">Edit</a>
                        <?php if ($data['role'] != 'admin') {?>
                         | <a href="delete.php?uid=<?php echo $data['id_bio'];?>" onClick="return confirm('Hapus akun ini?')" class="delete">Delete</a>
                        <?php } ?>
    </div>
    <?php } ?>
    </td>
    <td><?php echo $data['fullname']; ?></td>
    <td><?php echo $data['telephone']; ?></td>
                <td><?php echo $data['email']; ?></td>
            </tr>
    
    
    
            <?php 
    
            } 
            ?>
    
    
    
    
    
    <?php
    $totalPage = ceil($totalData / $limit);  
    echo 'Page : ';  
    for($i = 1; $i <= $totalPage; $i++){  
    if($page != $i){  
    echo '[<a href="showUsers.php?page='.$i.'">'.$i.'</a>] ';  
    }else{  
    echo "[$i] ";  
      }  
     }  
    ?>  
    
    点赞 评论 复制链接分享
  • dtoka218420 dtoka218420 7年前

    Hi,if you want to make a pagination, you should add in a LIMIT clause into your queries, like this:

    SELECT* FROM users LIMIT 0, 10
    

    With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.

    So, what's the next?

    You need to add a parameter to your query url specifies the number of page when you list the users.

    Such as:

    showUsers.php?page=1
    

    Then, in your program, you can get parameter by this:

    $page = isset($_GET['page']) ? $_GET['page'] : 1;
    

    I hope it will help you, I'm new here.

    点赞 评论 复制链接分享
  • doufan6033 doufan6033 7年前
    <?php 
    if(is_int($_GET('pageNo'))) // getting the page number from the URL i.e script.php?pageNo=2
    {
    
    
    
                $query = mysql_query("select * from users limit ".$_GET['pageNo']." ,10");
    
                while ($data = mysql_fetch_array($query)) {
                ?>
    
                <td><?php echo $data['no_peg']; ?></td>
                <td>
                    <?php 
                    echo $data['username']; 
    
                    //previlage admin
                    if ($_SESSION['role'] == 'admin') {
                    ?>
                        <div class="row-actions">
                        <a href="edit.php?uid=<?php echo $data['id_user'];?>">Edit</a>
                        <?php if ($data['role'] != 'admin') {?>
                         | <a href="delete.php?uid=<?php echo $data['id_bio'];?>" onClick="return confirm('Hapus akun ini?')" class="delete">Delete</a>
                        <?php } ?>
                    </div>
                    <?php } ?>
                </td>
                <td><?php echo $data['fullname']; ?></td>
                <td><?php echo $data['telephone']; ?></td>
                <td><?php echo $data['email']; ?></td>
            </tr>
    
    
    
            <?php 
    
            }
    

    } // not sure where the closing if should be you figure it out :P ?>

    点赞 评论 复制链接分享
  • dongxun6690 dongxun6690 7年前

    Where is your pagination code? Your code just shows this query:

    $query = mysql_query("select * from users");
    

    But for basic pagination you need to set a LIMIT like so:

    $query = mysql_query("select * from users LIMIT 0,10");
    

    So that would only grab the first 10 items. And then—let’s say, on page 2 you could do this:

    $query = mysql_query("select * from users LIMIT 11,10");
    

    That would grab the next 10 items starting from item 11.

    That’s the basic concept. But you have to code the logic for passing along pagination values & such.

    点赞 评论 复制链接分享

相关推荐