Page 1 of 1

Search engine results page problem

Posted: Mon Jul 12, 2010 3:25 pm
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>";

}

}
}

}

}
?>

Re: Search engine results page problem

Posted: Tue Jul 13, 2010 9:44 am
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>";

}

}
}

}

}
?>