X

Navigating on Multiple Pages with PHP

After setting up the connection to the database and then picked up the values ​​that we put in a table, this time we are to create dynamic lists that contain a set number of rows. We must do this because the company database we will certainly hundreds of values, why in this lesson we will see how to create lists of values ​​and in the next we will also see how to perform searches and also with filtering and sorting data. In fact this work there is quite simplified, as some articles ago I wrote a post about how to display list of values ​​in PHP and MySQL, then resume the information that my work.
The first thing to do is to fill up our database with PHPMyAdmin of values​​, I suggest you put a dozen inventory items as they put 3 records per page, then just changing a value to a variable, we can adapt it to 10 or 20 lines per page. Here are the entries you need to do to the page code than that of the previous lesson:

...
public function GetNumRows()
{
   if ($this->m_Rowset != NULL)
 return mysql_num_rows($this->m_Rowset);

   return 0;
}
...

We have added a new feature to our class CMySQL to get the total number of records, then we can calculate the number of rows to skip to have the exact record:

...
$rows = 4;
$page = 2;

$tot_rows = $items->GetNumRows();
$last_page = (int)($tot_rows / $rows);
if (($tot_rows % $rows) > 0)
   $last_page++;

if ($page > 1)
{
   $numrec = $rows * ($page - 1);
   for ($i = 0; $i < $numrec; $i++)
   {
       if (!$items->MoveNext())
    break;
   }
}
...

Finally we go to modify the part that writes the table:

...
$counter = 0;
do
{
   if ($counter >= $rows)
 break;
   if ($items->GetData("Description") != NULL)
   {
 ....
 $counter++;
   }
}  while ($items->MoveNext());
...
<tr>
<td colspan="6" height="30" align="center" valign="middle"
        bgcolor="eeeeee" style="color:#444444;font-family:
        trebuchet,Tahoma,Verdana;font-size:12px;font-weight:
        normal">
<?php

   if ($counter == 1)
 echo "View $counter value in page
            $page of $last_page";
   else if ($counter > 1)
 echo "View $counter values in page
            $page of $last_page";
   else
 echo "There are no values in list";
?>
</td>
</tr>
...

Here is the result:

List of values

Try to increase the values ​​of rows to display, the pages in this case must decrease. As a task I ask you to place links to move down the list with the usual “previous”, “next”, “first” and “last” and of course implement them. If you followed my course and my articles on PHP, with diligence and attention, you will know definitely perform this simple task.

<< Previous LessonTutorial Init

Categories: Uncategorized
giampy107:
X

Cookies Policy

We use cookies to personalize content and ads, provide social media features and analyze our traffic. We also share information about your use of our site with our web analytics, advertising and social media partners who may combine it with other information you have provided to them or that they have collected based on your use of theirs. services.

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings