ineedhelpbigtime Posted January 10, 2009 Share Posted January 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 10, 2009 Share Posted January 10, 2009 Try something like: $getName_sql = "SELECT username FROM users WHERE username LIKE '%$input%' OR names LIKE '%$input%'"; $getName = mysql_query($getName_sql); $aUsers = array(); while($row = mysql_fetch_assoc($getName)){ $aUsers = $row['username']; } Quote Link to comment Share on other sites More sharing options...
ineedhelpbigtime Posted January 11, 2009 Author Share Posted January 11, 2009 Well that seems to be working, so thankyou for helping me progress, but its not returning any rows in the output. Can anyone advise me on how to update the rest of the script to output the rows? Quote Link to comment Share on other sites More sharing options...
.josh Posted January 11, 2009 Share Posted January 11, 2009 should actually be $aUsers[] = $row['username']; Quote Link to comment Share on other sites More sharing options...
ineedhelpbigtime Posted January 11, 2009 Author Share Posted January 11, 2009 Thats done it! Thank you so much... i am indebted to you both. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.