Solved

Adding pagination to php page

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now