Jump to content

Ajax/php search app


merylvingien

Recommended Posts

Hi 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.

 

Link to comment
Share on other sites

I think i found it : )

 

Your using $output without initializing it first outside the if statement.

try this :) lol i hope this works

 

$output='monkeys'; // just as an example
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";

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.