Search engine results page problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boba fett
Forum Newbie
Posts: 15
Joined: Fri Jul 09, 2010 2:48 pm

Search engine results page problem

Post by boba fett »

My search engine is working beautifully to find the things that we want it to find. Problem is that when the results are displayed it displays the name, scientific name and common name for each returned entry. I would like to know how to get the titles to be displayed only once on the top of the page.

Code: Select all

<HTML>
      <title>Search Engine</title>
<form action='search.php' method='GET'>
           <font face='sans-serif' size='5'>
           <center>
                   My Search Engine.<br />
                   <input type='text' size='50' name='search' /><input type='submit' name='submit' value='Search' /><br />

           </center>
           </font>
         
     </form>
</HTML>



<?php

//get data
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
echo "You didn't submit a keyword.";
else
{
if (strlen($search)<=2)

echo "Search not valid. Search term too short.";
else
{
echo "You searched for <b>$search</b> <hr size='1'>";

//connect to our database
mysql_connect("localhost:8888","root","root");
mysql_select_db("snakebook");


{
//explode our search term
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
{

//construct query
$x++;
if ($x==1)
$construct .="Scientific_Name LIKE '%$search_each%'
OR Common_Name LIKE '%$search_each%'
OR Physical_Characteristics LIKE '%$search_each%'
OR Geographic_Range LIKE '%$search_each%'
OR Diet LIKE '%$search_each%'
OR Venom LIKE '%$search_each%'
OR Habitat LIKE '%$search_each%'
OR Notes LIKE '%$search_each%'
";

else
$construct .="  OR Scientific_Name '%$search_each%'
OR Common_Name LIKE '%$search_each%'
OR Physical_Characteristics LIKE '%$search_each%'
OR Geographic_Range LIKE '%$search_each%'
OR Diet LIKE '%$search_each%'
OR Venom LIKE '%$search_each%'
OR Habitat LIKE '%$search_each%'
OR Notes LIKE '%$search_each%'
";

}

//echo out construct

$construct = "SELECT * FROM specieslist WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "No results found.";
else
{

echo "<b>$foundnum</b> result(s) found.<p><hr size='1'>";
while ($runrows = mysql_fetch_assoc($run))


{
//get data to display

echo "<center><table border='0' cellpadding='5' cellspacing='0'>
<tr>
<td><strong>Thumbnail</strong></td>
<td><strong>Scientific Name</strong></td>
<td><strong>Common Name</strong></td>
<td><strong>View/Edit</strong></td>
</tr>";

$common = $runrows['Common_Name'];
$scientific = $runrows['Scientific_Name'];
$thumbnail = $runrows['Thumbnail'];

//display data

echo "<tr>
<td><strong>$thumbnail</strong></td>
<td><strong><i>$scientific</i></strong></td>
<td><strong>$common</td></strong>
<td><strong>
</tr>";

}

}
}

}

}
?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Search engine results page problem

Post by Jade »

You need to pull the titles outside of your loop like this:

Code: Select all

<HTML>
      <title>Search Engine</title>
<form action='search.php' method='GET'>
           <font face='sans-serif' size='5'>
           <center>
                   My Search Engine.<br />
                   <input type='text' size='50' name='search' /><input type='submit' name='submit' value='Search' /><br />

           </center>
           </font>
         
     </form>
</HTML>



<?php

//get data
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
echo "You didn't submit a keyword.";
else
{
if (strlen($search)<=2)

echo "Search not valid. Search term too short.";
else
{
echo "You searched for <b>$search</b> <hr size='1'>";

//connect to our database
mysql_connect("localhost:8888","root","root");
mysql_select_db("snakebook");


{
//explode our search term
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
{

//construct query
$x++;
if ($x==1)
$construct .="Scientific_Name LIKE '%$search_each%'
OR Common_Name LIKE '%$search_each%'
OR Physical_Characteristics LIKE '%$search_each%'
OR Geographic_Range LIKE '%$search_each%'
OR Diet LIKE '%$search_each%'
OR Venom LIKE '%$search_each%'
OR Habitat LIKE '%$search_each%'
OR Notes LIKE '%$search_each%'
";

else
$construct .="  OR Scientific_Name '%$search_each%'
OR Common_Name LIKE '%$search_each%'
OR Physical_Characteristics LIKE '%$search_each%'
OR Geographic_Range LIKE '%$search_each%'
OR Diet LIKE '%$search_each%'
OR Venom LIKE '%$search_each%'
OR Habitat LIKE '%$search_each%'
OR Notes LIKE '%$search_each%'
";

}

//echo out construct

$construct = "SELECT * FROM specieslist WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "No results found.";
else
{

echo "<center><table border='0' cellpadding='5' cellspacing='0'>
<tr>
<td><strong>Thumbnail</strong></td>
<td><strong>Scientific Name</strong></td>
<td><strong>Common Name</strong></td>
<td><strong>View/Edit</strong></td>
</tr>";

echo "<b>$foundnum</b> result(s) found.<p><hr size='1'>";
while ($runrows = mysql_fetch_assoc($run))


{
//get data to display

$common = $runrows['Common_Name'];
$scientific = $runrows['Scientific_Name'];
$thumbnail = $runrows['Thumbnail'];

//display data

echo "<tr>
<td><strong>$thumbnail</strong></td>
<td><strong><i>$scientific</i></strong></td>
<td><strong>$common</td></strong>
<td><strong>
</tr>";

}

}
}

}

}
?>
Post Reply