Solved

PHP/MySQL search results w/ pagination & results as hyperlinks

Posted on 2004-04-30
4
2,651 Views
Last Modified: 2013-12-12
I'm working on a project for a medical practice. I have a patient database and am trying to setup a search that displays a listing of patient records in a MySQL database. (Ex. lastname="Smith") The results would then display all of the "Smiths" listed in the database and would link to each specific "Smith" record. I also need to have the results display pages, if there are 500 "Smiths" then have 25 per page w/ links to the remaining pages.

1) Results as links

2) pagination of results

System running apache 1.3 on WIN2000 SP4, PHP 4.3.6, Mysql 4.0.18
0
Comment
Question by:dsgfw
  • 2
4 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10964698
This is untested, but should point you in the right direction.

// searchpage.php
if(!isset($_GET['pageValue'])
    $pageValue = 0;
else
    $pageValue = $_GET['pageValue'];
$result = mysql_query("SELECT * FROM table WHERE lastname = 'Smith' LIMIT $pageValue, 25");

// while row = each result...
    echo "<a href='lookuppage.php?id=" . $row['id'] . "'>" . $row['firstname'] . "</a>";
echo "<a href='searchpage.php?pageValue=" . $pageValue+25 . "'>Next page</a>";

// lookuppage.php
SELECT * FROM table WHERE id = $_GET[id]
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 10965730

i assume that you have coding for entering the text string to be searched....Or else, use something like the following:

.....................
$fldname = $_GET['name']
 <form method="GET" action="<?= thepage.php ?>">
    <table >
     <tr>
      <td>Name</td>
      <td>
         <input type="text" name="name" maxlength="30" value="<?= $fldname ?>" size="20" ></td>
     <td ><input type="submit" value="Search">
     </td>    
    </tr>
     
   </table>
   </form>
.......................................
// Then, comes our DISPLAY and PAGINATION part.....

<?php
    // get the pager input values
    $page = $_GET['page'];
    $limit = 25;   // 25 records per page...
    $result = mysql_query("select count(lastname) from myTable");
    $total = mysql_result($result, 0, 0);

    // Get the value of search string....
   $search_string = $_GET['name']

    // work out the pager values
    $pager  = Pager::getPagerData($total, $limit, $page);  // See the function defined seperately below
    $offset = $pager->offset;
    $limit  = $pager->limit;
    $page   = $pager->page;

    // use pager values to fetch data
    $query = "select lastname from myTable where lastname like '".$search_string%."' order by someField limit $offset, $limit";
    $result = mysql_query($query);

    // use $result here to output page content

    // while $result = each result...
    echo "<a href='personpage.php?id=" . $result['id'] . "'>" . $result['lastname'] . "</a>";

    // output paging system (could also do it before we output the page content)
    if ($page == 1) // this is the first page - there is no previous page
        echo "Previous";
    else            // not the first page, link to the previous page
        echo "<a href=\"thepage.php?page=" . ($page - 1) . "\">Previous</a>";

    for ($i = 1; $i <= $pager->numPages; $i++) {
        echo " | ";
        if ($i == $pager->page)
            echo "Page $i";
        else
            echo "<a href=\"thepage.php?page=$i\">$i</a>";
    }

    if ($page == $pager->numPages) // this is the last page - there is no next page
        echo "Next";
    else            // not the last page, link to the next page
        echo "<a href=\"thepage.php?page=" . ($page + 1) . "\">Next</a>";
?>
========================================================================================

The Helper class:

<?php
   class Pager
   {
       function getPagerData($numHits, $limit, $page)
       {
           $numHits  = (int) $numHits;
           $limit    = max((int) $limit, 1);
           $page     = (int) $page;
           $numPages = ceil($numHits / $limit);

           $page = max($page, 1);
           $page = min($page, $numPages);

           $offset = ($page - 1) * $limit;

           $ret = new stdClass;

           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;

           return $ret;
       }
   }
?>
=================================================================================================

I have modified the PHP Pagination code from
http://www.phpnoise.com/tutorials/9/3
according to your requirements...If you have questions regarding the code, u can refer the site and see the tutorial given there ...

=================================================================================================

Also, there is a very nice code + tutorial in
http://www.webdevtips.com/webdevtips/php/paging.php
where you can find your requirement....

=================================================================================================
Hope this helps you...For further questions, Dont hesitate to contact me....

All the Best.
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 500 total points
ID: 10965799
>> $limit = 25;   // 25 records per page...
>> $result = mysql_query("select count(lastname) from myTable");
>> $total = mysql_result($result, 0, 0);

>> // Get the value of search string....
>> $search_string = $_GET['name']  

must be:

// Get the value of search string....
$search_string = $_GET['name'];
$limit = 25;   // 25 records per page...

$result = mysql_query("select count(lastname) from myTable where lastname like '".$lastname."%'"); // pls. take care of quotes, am not sure.
$total = mysql_result($result, 0, 0);
   
0
 

Author Comment

by:dsgfw
ID: 10974707
ldbkutty - thanks for the help. I'll check it out.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ebay api to see some data about my customers 2 31
error log using ftp 7 41
showing loader for php/mysql/ajax live search 13 26
How is this connection happening? 3 15
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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