0

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

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

4
  • Hope this can solve your problem.. stackoverflow.com/a/17159388/1358004 Commented Mar 12, 2014 at 13:03
  • Your paging query doesn't appear to return anything, nor does it use global variables or pass parameters by reference to update the values of passed parameters . Not sure what the value of $limitt will be when it gets to the query. Commented Mar 12, 2014 at 13:19
  • Visit [pagination using php and mysql][1]. <br> I answered here. [1]: stackoverflow.com/questions/12315920/… Commented Apr 18, 2014 at 4:45
  • visit: [pagination][1] here. I already answered for this question. [1]: stackoverflow.com/questions/12315920/… Commented Apr 18, 2014 at 4:46

2 Answers 2

0

Please put:

echo $query;

after:

$sql_ = mysql_query($query_);

in showReport.php.

Check if the query is created how you expect it to be. Also in showReport.php, I see you are using the variable '$page' while i don't see it was given a value first. Are both pages imported in another one?

Sign up to request clarification or add additional context in comments.

4 Comments

yaaap, in showReport.php there is include('paging.php); I've tried, but the data still showing all of it hmm....
The echo command should show the query you are using. You can then try the query directly into your MySQL database to verify if it gives the correct data there.
but how to make the data just showing 2 data per pages? the query is correct, but it confusing why the data won't show just 2 data, beside the item for page just set 2
In showReport.php your while will go trough all the records found with that query. If the query returns 4 records, 4 records will be shown while you only want to show 2. Check if the query only return 2 records instead of 4.
0

For making limit the amount of data displayed

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

For making number of pages / pagination

<?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] ";  
 }  
}  
?>  

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.