Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    44
    Thanks
    1
    Thanked 0 Times in 0 Posts

    AJAX, how to limit result from suggestion box?

    Hi, I have this code to get hints (PHP file):
    Code:
    <?php
    
    include("include/session.php");
    
    $GetNames = "SELECT firstname, lastname, username FROM users";
    $GetNamesConnect = $database->query($GetNames);
    
    While($row = mysql_fetch_array($GetNamesConnect))
    {
       $a[$row['username']] = $row['firstname'] . " " . $row['lastname'];
    }
    
    //get the q parameter from URL
    $q=$_GET["q"];
    
    //lookup all hints from array if length of q>0
    if (strlen($q) > 0)
      {
      $hint="";
      for($i=0; $i<count($a); $i++)
        {
        if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
          {
          if ($hint=="")
            {
            $hint=$a[$i];
            }
          else
            {
            $hint=$hint.", ".$a[$i];
            }
          }
        }
      }
    
    // Set output to "no suggestion" if no hint were found
    // or to the correct values
    if ($hint == "")
      {
      $response="no suggestion";
      }
    else
      {
     $id = $row['username'];
     $response= "<a href='profile.php?user=$row['username']'>$hint</a>";
    
      }
    
    //output the response
    echo $response;
    ?>
    This is the display part (Javascript/Ajax)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <script type="text/javascript">
    function showHint(str)
    {
    if (str.length==0)
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","gethint.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </body>
    </html>
    What I want to do it to limit the names suggested to 10, how would I do this?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    LIMIT your SQL result set to 10 entries.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    This is not actually a Javascript question. You need to limit the hints on the PHP side.

    I'm not a PHP developer so the syntax might be wrong. But you'll get the idea.

    PHP Code:
     $hint="";
      
    $total=0;
      for(
    $i=0$i<count($a); $i++)
        {

        if (
    $total>10) break;

        if (
    strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
          {
          if (
    $hint=="")
            {
            
    $hint=$a[$i];
            }
          else
            {
            
    $hint=$hint.", ".$a[$i];
            }

            
    $total++;

          }
        }
      } 
    Edit:Looking at your code again, I think you should let SQL search for the match by adding a WHERE clause instead of searching for it manually.


    PHP Code:
    //get the q parameter from URL
    $q=$_GET["q"];
    $GetNames "SELECT TOP 10 firstname, lastname from users WHERE firstname like '%.$q.%' OR lastname like '%.$q.%'"
    Again the PHP syntax may be wrong.

    I don't know your SQL server so TOP may not be supported. You may have to change to LIMIT or something else and the syntax will be different from TOP.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •