PHP script -- page break, pagination -

Viewed 43
  1. i have problem viewing a general search on my database query. on the view report/result i need to create a page-break on every 100rows display and pagination selections; ie: Page 1 2 3 4 5 5...... so on. please help.... thank you

...

        <h2>DEPOSIT MANAGEMENT SYSTEM</h2>
        <h3><b>VIEW DEPOSITORIES</b></h3>

        <a class="hyperlink" href="dep_mgmt_search.php"><b>BACK TO SEARCH FORM</b></a><br>
        <a class="hyperlink" href="dep_mgmt_main.php"><b>BACK TO MENU</b></a>

        <br><br>

        <?php 

            $sql = 'select * from table_data where 1';

            $criteria = '';

            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'search') {

                foreach($_REQUEST as $key => $value) {

                    if ($key != 'action') {

                        if ($value != '') {

                            $sql .= ' and ' . $key . ' like "%' . $value . '%" '; 
                            $criteria .= $key . '=' . $value . ', ';

                        }
                    }

                }

            }

            if ($criteria != '') {

                $criteria = substr($criteria, 0, strlen($criteria) - 2);

                echo '<b>Search criteria:</b> ' . $criteria . '<br><br>';

            }


            $qry = $db->query($sql);

            if ($qry->num_rows > 0) {
                ?>

                    <table width="100%">
                        <thead>
                            <tr>
                                <th><b>NO</b></th>
                                <th><b>NAME</b></th>
                                <th><b>NRIC</b></th>
                                <th><b>CURR BAL</b></th>
                                <th><b>DATA TYPE</b></th>
                                <th><b>DATA SUBTYPE</b></th>
                                <th><b style="color: #0ff;">ACTION</b></th>
                            </tr>
                            <tr>
                                <th colspan="8" style="border-top: 1px solid #fff;"></th>
                            </tr>
                        </thead>
                        <tbody>

                        <?php 

                            $i = 0;


                            while ($fetch = $qry->fetch_assoc()) {

                                $i++;

                                ?>
                                    <tr>
                                        <td><?php echo $i; ?></td>
                                        <td><?php echo $fetch['NAME']; ?></td>
                                        <td><?php echo $fetch['NRIC']; ?></td>
                                        <td><?php echo number_format($fetch['CURR_BAL'], 2); ?></td>
                                        <td><?php echo $fetch['DATA_TYPE']; ?></td>
                                        <td><?php echo $fetch['DATA_SUBTYPE']; ?></td>
                                        <td><a class="hyperlink" href="dep_mgmt_form.php?id=<?php echo $fetch['ID']; ?>"><b>VIEW</b></a></td>
                                    </tr>
                                
                                <?php 

                            }

                        ?>

                        </tbody>
                    </table>

                <?php

            } 

            else {

                ?>

                <br><br><b>DATA NOT FOUND</b><br>Please try again with a new search criteria<br><br>

                <?php 
            }

        ?>




        <br><br><br><br>

        <br>
        <a class="hyperlink" href="dep_mgmt_search.php"><b>BACK TO SEARCH FORM</b></a><br>
        <a class="hyperlink" href="dep_mgmt_main.php"><b>BACK TO MENU</b></a>


    <?php

}

include '_template.php';

?> ...

so based on this coding, can anyone help me to rectify where is the problem i'm encounter for the page break for the records view

1 Answers

This is just wrong.
It is poor practice to switch back and forth between PHP mode and HTML mode.
There is no reason to do that.
There is significant overhead each time you switch.

This is horrific coding:

 ?>
                                    <tr>
                                        <td><?php echo $i; ?></td>
                                        <td><?php echo $fetch['NAME']; ?></td>
                                        <td><?php echo $fetch['NRIC']; ?></td>
                                        <td><?php echo number_format($fetch['CURR_BAL'], 2); ?></td>
                                        <td><?php echo $fetch['DATA_TYPE']; ?></td>
                                        <td><?php echo $fetch['DATA_SUBTYPE']; ?></td>
                                        <td><a class="hyperlink" href="dep_mgmt_form.php?id=<?php echo $fetch['ID']; ?>"><b>VIEW</b></a></td>
                                    </tr>
                                
                                <?php 

I would NEVER do this

$sql = 'select * from table_data where 1';

I always specify the columns I want, even if I want them all.

SELECT `NAME`,`NRIC`,`DATA_TYPE`,`DATA_TYPE`,`CURR_BAL` FROM ...

I would change this

while ($fetch = $qry->fetch_assoc()) {

To where this will create the paged tables. 100 records per table. The Next button will close the current "tab/page" and show the next table. The Previous button will close current and show previous page.

$tablehead = '<tr><th><b>NO</b></th><th><b>NAME</b></th><th><b>NRIC</b></th><th><b>CURR BAL</b></th><th><b>DATA TYPE</b></th><th><b>DATA SUBTYPE</b></th><th><b style="color: #0ff;">ACTION</b></th></tr>';

echo "<table>$tablehead";
$js = "var pages = {";
while (list($NAME,$NRIC,$DATA_TYPE,$DATA_TYPE,$CURR_BAL ) = $qry->fetch_array(MYSQLI_NUM);) {
    $i++;
    echo <<<EOT
    <tr><td>$i</td>
     <td>$NAME</td>
     <td>$NRIC</td>
     <td>$CURR_BAL</td>
     <td>$DATA_TYPE</td>
     <td>$DATA_SUBTYPE</td>
     <td><a class="hyperlink" href="dep_mgmt_form.php?id=$ID"><b>VIEW</b></a></td>
    </tr>
    EOT;
   if($i%100 == 0){
     $page++;
     echo "</table></div><div id=\"t$page\"><table>";
     $js .= "$page:[null],";
   }

}
$js = trim($js,",") . '}';
echo <<< EOT
</table>
<button onclick="next()">Next</button><buttononclick="prev()">Previous</button>
<script>
var current = 1;
var last = 0;
$js
for(let id in pages){
  pages[id][0] = document.getElementById('t' + id);
  pages[id].style.display = 'none';
  last = id;
}
pages[1].style.display = 'block';

function next(){
  pages[current][0].style.display = 'none';
  current++;
  if (current > last){current = 1;}
  pages[current][0].style.display = 'block';
}
function prev(){
  pages[current][0].style.display = 'none';
  current--;
  if (current < 1){current = last;}
  pages[current][0].style.display = 'block';
}
</script>
</body></html>

EOT;

This is an example of pagination where each page is a different category. This was a fun (challenging) app to write. It's not easy to update the order with hundreds of check boxes per page. Every time a category is selected, I up date the order with the changes made on the current page.
You could easily do this with the code I gave you here.
It's getting late where I live. So if you are interested in what I have done here I will make this app into a functional demo.

I do not know where you learned to code in PHP, but you need help.
I'll do what I can if you want.

Doctors Portal for Order Entry for Allergy Lab Tests

Related