Solved

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

Posted on 2004-04-30
4
2,656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…

688 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