使用PHP和MySQL分页? [英] Pagination with PHP and MySQL?

查看:96
本文介绍了使用PHP和MySQL分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件.有showReport.php和pages.php.代码很好,没有错误,但是显示错误的结果.您可以帮我完成期末考试吗?因为我试图更改它,但仍然无法正常工作.请忽略变量名或表名,因为我使用的是印度尼西亚语.

I have 2 files. There are showReport.php and paging.php. The codes are fine, no errors, but it showing incorrect results. Can you help me for my final exams? Because I've tried to change it but still doesn't work. Please ignore for the variable name or table name, because I'm using Indonesian.

showReport.php

<?php 
            $query = mysql_query("select * from barang");
            $item_per_page = 2; // tambahan
            paging( $query,$item_per_page );
            $limitt = ($page - 1) * $item_per_page;

            $query_ = mysql_query("SELECT * FROM barang ORDER BY id_transaksi DESC LIMIT $limitt, $item_per_page"); // tmbh
            $sql_ = mysql_query($query_);       

            $i = 1;

            while ($data = mysql_fetch_array($query)) {
            ?>
            <tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
                <td><?php echo $i; ?></td>
                <td>
                    <?php 
                    echo $data['tanggal']; 

                    //previlege admin
                    if ($_SESSION['role'] == 'operator') {
                    ?>
                        <div class="row-actions">
                        <a href="edit_barang.php?uid=<?php echo $data['id_transaksi'];?>">Update</a>
                         | <a href="delete_barang.php?uid=<?php echo $data['id_transaksi'];?>" onClick="return confirm('Delete this report?')" class="delete">Delete</a>




                    </div>
                    <?php } ?>
                </td>



                <td><?php echo $data['barang_in']; ?></td>
                <td><?php echo $data['bijih_out']; ?></td>
                <td><?php echo $data['htm_out']; ?></td>
                <td><?php echo $data['pth_out']; ?></td>
                <td><?php echo $data['bijih']; ?></td>
                <td><?php echo $data['kantong_htm']; ?></td>
                <td><?php echo $data['kantong_pth']; ?></td>
                <td><?php echo $data['note']; ?></td>
            </tr>



            <?php 
                $i++;
            } 
            ?>

paging.php

<?php
function paging($query,$item_per_page){
   $page          =  isset($_GET['page']) ? $_GET['page'] : 1 ; 
   if( ( $page < 1) && (empty( $page ) ) ){
      $page=1;
   }
   $sql         = mysql_query( $query_);
   $amount_data   = mysql_num_rows($query);
   $amount_page    = ceil( $amount_data/$item_per_page );
   if( $page>$amount_page ){
      $page=$amount_page;
   }
   $lanjut  = $page + 1;
   $sebelum = $page - 1;
   ?>
<style type="text/css">
.tengah {
    text-align: center;
}
</style>

<?php echo $page;?> dari <?php echo $amount_page;?><br />
   <a href="?page=1"></a><a href="?page=<?php echo $sebelum; ?>">sebelumnya</a>;
   ||
   <a href="?page=<?php echo $lanjut;?>">selanjutnya</a><a href="?page=<?php echo $jumlah_hal;?>"></a>
   Ke Halaman: <form action="" method="get"><input type="text" name="page"><input type="submit" value="Go"></form>
   <?php
}
?>

页数正确,但仍将所有数据显示在一页中.例如,我有4个数据,我只想为每个页面显示2个数据.那么页面数是2(右),但是为什么我的数据不会在每页显示2个数据?预先谢谢你

The number of pages are right, but it still showed all of data in one page. For example I have 4 data, I wanna show just 2 data for each page. So number of pages are 2 ( right ), but why my data won't to show 2 data for each page? Thank you in advance

推荐答案

用于限制显示的数据量

<?php 
$sqlCount = "select count(id_transaksi) from barang";  
$rsCount = mysql_fetch_array(mysql_query($sqlCount));  
$banyakData = $rsCount[0];  
$page = isset($_GET['page']) ? $_GET['page'] : 1;  
$limit = 2;  
$start_from = $limit * ($page - 1);  
$sql_limit = "select * from barang ORDER BY id_transaksi limit $start_from, $limit";  
$result = mysql_query($sql_limit);  ?>

用于打印页数/分页

<?php
$numberofPages = ceil($amountData / $limit);  
echo 'Pages: ';  
for($i = 1; $i <= $numberofPages; $i++){  
 if($page != $i){  
 echo '[<a href="showReport.php?page='.$i.'">'.$i.'</a>] ';  
 }else{  
 echo "[$i] ";  
 }  
}  
?>  

根据我的问题,不再使用 paging.php .希望可以帮助像我这样以前遇到问题的任何人,加油! ;)

Based on my question, paging.php isn't any longer used. Hope can help anyone who gets problem like me before, cheers! ;)

这篇关于使用PHP和MySQL分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆