Jump to content

Recommended Posts

Ok I found a tutorial on another site on how to create a basic search option on my site.  I have added the code and changed it a bit to work the way I need it to, but have one problem.  If a user enters nothing into the search field but hits search it prints the text 'You forgot to enter a search term'.  The problem is it stops doing the rest of the code for the web site.  So the right column and past that is missing.

 

Here is the code...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>myevvspace.com | premium local listings</title>

<link href="myevvspace_style.css" rel="stylesheet" type="text/css" />
<script src="navigation.js" type="text/javascript" language="javascript"></script>

<?php
//database connection

$keyword = ($_POST['keyword']);
$category_num = (int)$_GET['category'];
$sub_category_num = (int)$_GET['sub_category'];
$searching= ($_POST['searching']);

$keyword = strip_tags($keyword);

// Collects data from "friends" table
$search = mysql_query("SELECT client.id, client.paid, client.sub_category, client.category, client.name, client.address, client.phone, sub_category.subcat_title, sub_category.id, category.cat_title, category.id FROM client, sub_category, category WHERE client.name LIKE '%$keyword%' AND client.sub_category = sub_category.id AND client.category = category.id order by name")
or die(mysql_error());

// mysql_query("SELECT name, address, phone FROM client WHERE name LIKE '%$keyword%'")
// or die(mysql_error());

$categorycrumb = mysql_query("SELECT cat_title FROM category WHERE id = '$category_num'")
or die(mysql_error());

mysql_close ($conn);
?>

</head>

<body>
<div id="header"><?php include("includes/header.php"); ?></div>
<div id="navigation"><?php include("includes/navigation.php"); ?></div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="126" rowspan="2" align="left" valign="top" id="leftcontent">
<br /><?php //And we remind them what they searched for
echo "<font class=searchedfor><b>You searched for:</b><br />" . $keyword . "</font>"; ?></td>
    <td rowspan="2" align="left" valign="top" id="centercontent">
<?php
//This is only displayed if they have submitted the form
if ($searching == "yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($keyword == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

$anymatches=mysql_num_rows($search);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
else {
echo '<table class=results cellpadding=3 cellspacing=0><tr height=18><td width=31% align=center bgcolor=#D5CB9A background=images/table_gradient.jpg><font class=result><b>Business Name</b></font></td><td width=29% align=center bgcolor=#D5CB9A background=images/table_gradient.jpg><font class=result><b>Address</b></font></td><td width=20% align=center bgcolor=#D5CB9A background=images/table_gradient.jpg><font class=result><b>Phone Number</b></font></td><td width=20% align=center bgcolor=#D5CB9A background=images/table_gradient.jpg><font class=result><b>Type</b></font></td></tr>';
  while ($info = mysql_fetch_array( $search )) {
      // get the first letter of the name. let's convert
      // it to uppercase to make sure that 'A' and 'a' get
      // put into the same group. 
      $letter = strtoupper(substr($info['name'],0,1));
      
      // now we will check to see if the first letter of the
      // current name is the same as the first letter of the
      // previous name. If it is not, then we make a new
      // group 'header' with the letter. Since there is no 
      // previous row on the first pass, we will automatically
      // have an underlined 'A' made (or whatever your list starts with) 
      if ($letter != $prev_row) {
         echo '<tr><td height=18 colspan=4 bgcolor=#eeeeee><font class=result color=#585858><b>',$letter,'</b></font></td></tr>';
      } // end if
       echo '<tr>';
      // here we just echo out the name
      if ($info['paid'] == 1)
    echo "<td class=results><font class=result><a href=\"client_detail.php?category={$info['category']}&sub_category={$info['sub_category']}&client={$info[0]}\"><b>", $info['name'], '</b></a></font></td><td class=results><font class=result>', $info['address'], '</font></td><td class=results align=center><font class=result>', $info['phone'], "</font></td><td class=results align=center><font class=result_type>:: <a href=client_list.php?category={$info[10]}>", $info['cat_title'], "</a> ::<br /><a href=client_list.php?category={$info[10]}&sub_category={$info[8]}>", $info['subcat_title'], '</a></font></td>';
else
    echo '<td class=results><font class=result>', $info['name'], '</font></td><td class=results><font class=result>', $info['address'], '</font></td><td class=results align=center><font class=result>', $info['phone'], '</font></td><td class=results align=center><font class=result_type>:: ', $info['cat_title'], ' ::<br />', $info['subcat_title'], '</font></td>';
      
      // and here we assign the current letter to $prev_row so 
      // that on the next iteration of the loop, we will have 
      // a previous row letter to compare
      $prev_row = $letter;
   } // end while
   
   echo '</tr></table>';
   }
   }
?>
<p> </p></td>
    <td height="34" align="center" valign="middle" id="searchbox"><form method="post" action="search.php" style="display:inline"><input type="text" id="keyword" name="keyword" value="search" class="search" /><input type="hidden" name="searching" value="yes" /></form></td>
  </tr>
  <tr>
    <td width="126" align="center" valign="top" id="rightcontent">right content for ads</td>
  </tr>
</table>
</body>
</html>

 

 

I know the problem is with this line:

if ($keyword == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

 

When it hits that exit it stops doing the code.  What I do not know is how to fix it so that when a user does enter nothing into the database it does not pull all results from the database.  The end database is going to be very big and this would cause a huge problem.  Thanks for any help you can give me.

 

Link to comment
https://forums.phpfreaks.com/topic/67102-creating-a-search-on-my-site/
Share on other sites

OK, I think I got it.

 

I changed this:

if ($keyword == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

 

 

To this:

if ($keyword == "")
{
echo "<p>You forgot to enter a search term";
}
else {

 

If anyone sees where that is going to cause some problem let me know.  I have tested it but do not see any errors.

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.