PHP: If more than 1 match found then list up to 10 as links

Posted on 2011-05-04
Last Modified: 2012-08-14

Given the code below, how can I display the content of mysql_fetch_assoc array?  What I would like to present is a list up to say 10 or 20 or all at a time of the matches that were found then display them as link where the user can simply click on it to display the content of the record.

Let's use a good existing code given by cxr for example:

Thanks in advance
if($_POST['user']=='Search') {
  $res = mysql_query(
    "select * from $tablename where
     firstname like '$firstname%' and
     lastname like '$lastname%' and
     department like '$department%'
     order by id");
  if($res) {
    $row = mysql_fetch_assoc($res);
    if($row) {
      $row_count = mysql_num_rows($res);
      if($row_count > 1) $msg = $row_count.' rows found, showing the first';
    } else $msg = 'No rows found';
  } else $msg = 'query failed: '.mysql_error();

Open in new window

Question by:Wayne88
    LVL 1

    Accepted Solution

    I would start by adding LIMIT 20 to your sql query.

    By adding LIMIT 20 (or alternatively LIMIT 0,20), you will only get the first 20 results. If you want the following 20, you would add LIMIT 20,20. Want the following 20, you'd get LIMIT 40,20 etc...

    You can then simply parse all the rows returned. Not get all 20, then you will simply get less rows. A last check to see if you got any results at all to not get an empty table, and you're set.

    See also:
    LVL 107

    Assisted Solution

    by:Ray Paseur
    This article may be helpful:

    The general way I would design something like this would be to set up a "see details" script that displays information from a single row.  It would have the key of that row in the URL arguments.  Then in the script that displays lots of rows, I would add a "see details" link with the key of each row.  Does that make sense to you?
    LVL 13

    Author Closing Comment

    hi guys, thanks for your help.  what i was looking for is more inline with this mysql_fetch_assoc array output.

    however, both of you did gave me partial ideas that i will be using to accomplish my task.  cheers!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    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…
    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…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now