PHP - Moved: Ajax Search
This topic has been moved to Ajax Help.
http://www.phpfreaks.com/forums/index.php?topic=320161.0 Similar TutorialsHi folks, i am trying to impliment a ajax php search function to my site, but just stuck on how to prevent a undefined variable Here is thee ofending code: <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('<p>There seems to be a problem, please try again soon.</p>'); } $db_selected = mysql_select_db("database",$con); //get the q parameter from URL $q=$_GET["q"]; //lookup all links from the xml file if length of q>0 $sql="SELECT * FROM simp_search WHERE `sbody` LIKE '%{$q}%' OR `varia` LIKE '%{$q}%' OR `stitle` LIKE '%{$q}%' ORDER BY `stitle`"; $result = mysql_query($sql); if (mysql_num_rows($result) < 1) { $error[] = "<p>Please try another search..."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($result)) { $results[] = "{$i}:<a href=\"http://www.mysite/{$row['url']}\">{$row['stitle']}</a><br />{$row['sbody']}<br /><br />"; $i++; } } function removeEmpty($var) { return (!empty($var)); } if ($results > 0){$output = "<p>Your search term: " . $q . " returned:<br><br>" . implode("", $results); } else {$output= "No results found!";} echo "$output"; ?> At the moment its a mishmash of code from other applications. Works fine if you insert a search term that is in the database, however if you enter something that doesnt match, the output is: Notice: undefined variable: results in C:\www\website\livesearch.php on line 35 No results found! Any ideas on what to change to prevent this? Also i know there is some code in there that is not needed. Ok, this is stumping my mind. I have a PHP/AJAX script which works almost perfectly. But its lacking one detail I absolutely need. Every AJAX demo I have seen is missing it or doesn't work right. Here is where the problem is. I have a list of countries, I want the user to spell it right when using my search, so you put in say "Virgin Islands" It should pull up the following in the list: Virgin Islands (British) St. Thomas (U.S. Virgin Islands) However all I get is: Virgin Islands (British) It has to do with the MySQL Query... Here is what came with the demo: $query = sprintf("SELECT ctry_name FROM international WHERE ctry_name '%s%%' GROUP BY ctry_name", $ctry_name); That's what pulls up only Virgin Islands I tried to mod the query to be "SELECT ctry_name FROM interntaional WHERE ctry_name LIKE '%" . $ctry_name . "%'" But it breaks the script, in other AJAX Examples, it doesn't even look it up and returns all entries in the database. Like I said the above example works fine, it just returns just the one value, I want ti to return all variables. I am using ajax live search to retrieve data from MySQL table. It works fine. However I noticed that it only returns a single result that matches the entire word instead of multiple results that match only the first couple letters. For e.g. DB Table ---------------- 1. Foot 2. Foot Path 3. Football ---------------- If I type "foo" in the ajax search field, it won't return any results. But if I type "foot", it'll return "Foot". It won't return the other two results that contain the letters "Foot". Here's my query $param_term = $_POST["term"] . '%'; $get_data = $db->prepare("SELECT name FROM table WHERE title LIKE :param"); $get_data->bindParam(':param', $param_term); $get_data->execute();
I was wondering how can I improve the above query so that it looks for all potential results based on partial matching of letters? This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=351379.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=325858.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=333348.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=312841.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=350592.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=348693.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=319274.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=314117.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=309753.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=314220.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=308756.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=332473.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=323434.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=347835.0 Stop posting non-PHP questions in the PHP Coding Help section. We have many sub-forums, including both a JavaScript forum and one dedicated entirely to Ajax. This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=327454.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=319471.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=343073.0 |