Jump to content

[SOLVED] Changing a hard coded array to working with MYSQL results.


ineedhelpbigtime

Recommended Posts

This could be seen as an AJAX function in whole, but this specific problem i am having is that the php part isn't working.

 

I am struggling to modify a nifty auto suggest function. It came hardcoded with an array, and i need to modify it to work form my DB. Could someone have a look for me?

 

The php is as follows:

 

<?php
include(inc/connect.php);

// assign search input and set limit.

$input = strtolower( $_GET['input'] );
   $len = strlen($input);
   $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;

// results
   
$getName_sql   =   "SELECT username FROM users WHERE username LIKE '%$input%' OR names LIKE '%$input%'";
$getName      =   mysql_query($getName_sql);


// I have tried a number of things to convert the output of the above query into something that the script can handle, but probably incorrect methods...

   $aUsers = array(
      "Ädams, Egbert",
      "Altman, Alisha",
      "Luke, Stagg"
   );
   
   $aResults = array();
   $count = 0;

   
// If stringlength...

   if ($len)
   {
      for ($i=0;$i<count($aUsers);$i++)   // counts...
      {
         // had to use utf_decode, here
         // not necessary if the results are coming from mysql
         //
         if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
         {
            $count++;
            $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]) );
         }
         
         if ($limit && $count==$limit)
            break;
      }
   }
   
   if (isset($_REQUEST['json']))
   {
      header("Content-Type: application/json");
   
      echo "{\"results\": [";
      $arr = array();
      for ($i=0;$i<count($aResults);$i++)
      {
         $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
      }
      echo implode(", ", $arr);
      echo "]}";
   }
   
?>

 

I have added my search query but i am unable to output the query results in a format that the rest of the code will accept (and i'm assuming the rest of the code will accept it...). The code above currently is still working from the 3 names specified in the manual array.

 

As always your help is greatly appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.