Solved

Adding pagination to php page

Posted on 2015-01-22
2
115 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

831 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