Page 1 of 1

Database search function

Posted: Sat May 03, 2008 10:28 am
by bluewaves
I have a search box that is basically working, but it needs some tweaking.
I'd like the results page to echo, "Sorry, no results matched." or something
like that when there are no results.



This is the form for the text box:

Code: Select all

<form action="handle_keywords1.php" method="post">
          <b><p>Search Checks:</b> <input type="text" name="keyword" size="20" maxlength="40" /></p>
          <dd align="center"><input type="submit" name="submit" value="Search" /></dd>
        </form>
 
This is the handle_keywords1.php that handles it:

Code: Select all

 
<?php
 
 include('../../misc.inc');
  $cxn = mysql_connect($host,$user,$password)
         or die ("couldn't connect to server");
  mysql_select_db($database);
  $database = "voipvide_checks";
  $upperKeyword = strtoupper($keyword);  
  $query = "SELECT * FROM fourchecks WHERE upper(name) like '%$upperKeyword%' ORDER BY Name";
  $result = mysql_query($query)
            or die ("Couldn't execute query.");
 
 
 /* Display results in a table */
     echo "<center><table cellspacing='10'>";
  echo "<tr><td colspan='3'><hr /></td></tr>";
  while($row = mysql_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td width='300'><img src='$BigImage' border ='0' alt='$Name Bank Checks'><P><font size='2'>
            <a href='$Link'>$Name BANK CHECKS</a><br>
</td>\n
            <td width='100'></td>\n
                       </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table></center>\n";
 
?>
 
 
 

Re: Database search function

Posted: Sat May 03, 2008 10:49 am
by AXEmonster
hi

Code: Select all

 
 
if(! $result){
 
$message = "no results";
 
}
 
 
// now you have set a condition and a message to display you can echo that $message anywhere on the page

Code: Select all

 
 
echo $message;
 
 

Re: Database search function

Posted: Sat May 03, 2008 10:59 am
by bluewaves
I'm such a beginner here. Where in my script do I put this:

Code: Select all

if(! $result){
 
$message = "no results";
 
}
 
And do I use this anywhere I want the results to show?

Code: Select all

<?php
 
echo $message;
 
?>

Re: Database search function

Posted: Sat May 03, 2008 12:17 pm
by AXEmonster
the first bit can go anywhere after your query

the next bit can go any where inside your HTML to display the message

Re: Database search function

Posted: Sat May 03, 2008 1:32 pm
by bluewaves
Well...this is how i put the code:

Code: Select all

<table><tr><td><h1>Search Results For Bank Checks</h1><br>
<?php
 
echo $message;
 
?>
 
</td>
</tr></table>
<table>
<tr>
 
 
 
 
<td width="450"><font size="2" face="Arial">
 
 
<?php
 
 include('../../misc.inc');
  $cxn = mysql_connect($host,$user,$password)
         or die ("couldn't connect to server");
  mysql_select_db($database);
  $database = "voipvide_checks";
  $upperKeyword = strtoupper($keyword);  
  $query = "SELECT * FROM fourchecks WHERE upper(name) like '%$upperKeyword%' ORDER BY Name";
  $result = mysql_query($query)
            or die ("Couldn't execute query.");
  if(! $result){
 
  $message = "no results";
 
}
 
  $message = "Sorry, no matching results.";
 
 
 /* Display results in a table */
     echo "<center><table cellspacing='10'>";
  echo "<tr><td colspan='3'><hr /></td></tr>";
  while($row = mysql_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td width='300'><img src='$BigImage' border ='0' alt='$Name Bank Checks'><P><font size='2'>
            <a href='$Link'>$Name BANK CHECKS</a><br>
</td>\n
            <td width='100'></td>\n
                       </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table></center>\n";
 
 
 
 
 
?>
I still don't see the message though...by the way..thanks for helping me.

Re: Database search function

Posted: Sat May 03, 2008 6:45 pm
by AXEmonster
where is the first bit of the code i gave you

Re: Database search function

Posted: Sun May 04, 2008 9:13 am
by bluewaves
Line 30-32

Re: Database search function

Posted: Sun May 04, 2008 11:11 am
by AXEmonster
and your query is working returning results to the page

Re: Database search function

Posted: Sun May 04, 2008 6:04 pm
by bluewaves
yes...if there are results.

Re: Database search function

Posted: Mon May 05, 2008 5:27 am
by AXEmonster
post all the code as you have it

php bit

Re: Database search function

Posted: Tue May 06, 2008 10:24 am
by remyx187
You can also do something like this after the mysql_query() function

Code: Select all

 
$found = mysql_num_rows($result);
 
if($found == 0){
 
echo "Search returned no results.";
 
}
 

Re: Database search function

Posted: Tue May 06, 2008 10:58 am
by bluewaves
O.k. That one worked like a charm. :D Bless your heart!!!

Thanks for hanging in there with me!