Solved

Adding pagination to php page

Posted on 2015-01-22
2
116 Views
Last Modified: 2015-01-30
Hi,
    I have a php page which polls a database and then creates a table based on the results, however I need to add pagination.  Ideally showing the page number, and also a dropdown to select the number of rows, something like 5, 10, 15 or 20 at a time?

This is my code so far:

<?php



$db="database";

$link = mysql_connect("localhost","user","pass");

if (! $link)

die("Couldn't connect to MySQL");



mysql_select_db($db)

or die("Couldn't open $db: ".mysql_error());


$searchdata = $_GET['field'];

$data = $_GET['DoID'];    

$resultall = mysql_query("SELECT * FROM maindata WHERE gpname LIKE '%".$_GET['field']."%' OR postcode LIKE '%".$_GET['field']."%' OR area LIKE '%".$_GET['field']."%' OR Do LIKE '%".$_GET['DoID']."%'")or die("SELECT Error: ".mysql_error());

$num_rows = mysql_num_rows($resultall);



?>



<html>



<head>

<meta http-equiv="Content-Language" content="en-gb">

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>Search Results</title>

<style>
.whiteBackground { background-color: #9DC3E6; }
.grayBackground { background-color: #DBE8F6; }
</style>

</head>



<body>



<div align="center">

<br>

<font face="Arial" size="2" color="#CC0000"><a href="./">

<font color="#CC0000">Back to search option</font></a></font><br>



      &nbsp;<p><b><font face="Tahoma" size="6">Results</font></b></p>

      <p>&nbsp;</p>

      <div align="center">

      <br>

      <font face="arial" size="4" color="'FF0000"><strong><?php echo $num_rows; ?></strong> Results Returned</font><br><br>

<div align="center">

      <table border="0" width="1000" cellspacing="0" cellpadding="0">

            <tr>

                  <td width="23%" height="100" valign="top"><b><font face="Arial"><u>Practice Name</u></font></b></td>

                  <td width="5%" height="100" valign="bottom">

                  <p align="center"><font face="Arial" size="1">Tick to Compare</font></td>

                  <td width="18%" height="100" background="http://www.h.co.uk.co.uk/gpw-admin/images/filler1.png">

                  <b><font face="Arial">Booking &amp; </font></b>
                  <p><b><font face="Arial">Contact</font></b></td>

                  <td width="18%" height="100" background="http://gpwatch.healthwatchsandwell.co.uk/gpw-admin/images/filler2.png">&nbsp;</td>

                  <td width="18%" height="100" background="http://www.h.co.uk.co.uk/gpw-admin/images/filler3.png">&nbsp;</td>

                  <td width="18%" height="100" background="http://www.h.co.uk/gpw-admin/images/filler4.png">&nbsp;</td>



            </tr>

      </table>

</div><br><br>

<?php

 while ($rows1 = mysql_fetch_array($resultall)) {  

        $x++;

 $class = ($x%2 == 0)? 'whiteBackground': 'graybackground';

 ?>

          <div align="center">

      <table border="0" width="1000" cellspacing="0" cellpadding="0">

            <tr class=<?php echo $class; ?>>

                  <td width="23%" height="40"><b><font face="Arial" size="2">&nbsp;&nbsp;<?php echo $rows1['gpname']; ?></font></b></td>

            <td width="5%">

            <p align="center"><b><font face="Arial"><input type="checkbox" name="comparecheck" value="OFF"></font></b></td>

                  <td width="18%"><b><font face="Arial"><u></u></font></b></td>

                  <td width="18%"><b><font face="Arial"><u></u></font></b></td>

                  <td width="18%"><b><font face="Arial"><u></u></font></b></td>

                  <td width="18%"><b><font face="Arial"><u></u></font></b></td>

            </tr>

      </table>

</div>

  </div>

  <?php

  }

?>

</body>

</html>
0
Comment
Question by:damianb123
2 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 40564783
0
 

Author Closing Comment

by:damianb123
ID: 40580133
That was a great article, very easy to follow and pagination working a treat - thanks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to set the Tinymce Editor image path 4 22
Correct syntax for subdirectories 9 18
Copying table data to one another 15 29
I'm a Human captcha checkbox 13 19
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question