Solved

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

Posted on 2004-04-30
4
2,653 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

730 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