Jump to content

How to add a dynamic link generated by PHP/MySQL DB search results


Alura

Recommended Posts

Hello - I'm new to PHP and I am working on a database to display information about various species of snakes, and eventually want to progress to information about each individual snake in our collection at my workplace.  As I'm getting used to PHP, I've decided to concentrate on just simple species information before tackling the larger project.

 

I set up the DB in MAMP and have created a simple PHP navigation page with a table linked to the DB that works fine for people browsing species, and it is set up to take them to a display page set up to show the specific species clicked from the table with this code on the "VIEW" link:

 

<a href="species_display.php?Scientific=<?php echo $row_specieslist['Scientific_Name']; ?>">view</a>

 

My table is called "specieslist", and my URL parameter is "Scientific", and the results page is "species_display.php".  I set this up via Dreamweaver's toolboxes.

 

Since we have over 35 species (and can go up to 300), I have been working on a search option for my site as well, and while it will return the desired results in a table, I am unsure as to how to add the above 'VIEW" Link to the tabled results.  I've tried modifications of the above link code, but have been unsuccessful as yet in getting it to work.  Am I even on the right track?

 

Here is my search page, created after following some online tutorials and tweaked with advice from this forum:

 

<HTML>
<head>
<title>Snakebook Search Engine</title>

</head>
<body>
<form action='search.php' method='GET'>


<p><img src="images/Snakebook-Logo.png" width="311" height="83" />

<input type='text' size='50' name='search' />
</p>
<p>
<input type='submit' name='submit' value='Search' />

</p>
</center>
</font>
</form>
</body>
</HTML>
[code=php:0]
<?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:8889","root","root") or die ('Error connecting');
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'>
"
;


$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</strong></td>
<td><strong>$view</strong></td>
</tr>"
;


}

}
}

}

}
?>

[/code]

 

I apologize for any weird spacing or sloppy coding - I'm still learning the correct way to write all of this.  Any help is appreciated, Thanks!

Try this:

 


//display data
echo "<tr>
<td><strong>$thumbnail</strong></td>
<td><strong><i>$scientific</i></strong></td>
<td><strong>$common</strong></td>
<td><a href='species_display.php?Scientific=$scientific'><strong>view</strong></a></td>
</tr>"

 

Thanks.  That seemed to be the bit I needed to fix up.  :D

 

Unfortunately, my results display page is acting up now.  :shrug:

 

Win one, lose one.  At least it's Friday, and I'll probably figure it out over the weekend.

 

Thanks for the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.