View Full Version : creating new version of page each time they click on a link

01-18-2012, 02:14 AM
Ok. So, I've built some tables in MySQL, a list of mammals, amphibians and reptiles.

When people visit the Mammals page (mammals.php), I want there to be a list of Mammal common names (common_name), and if they click on that common_name, a new version of mammals.php is displayed that writes all the info we have for that specific mammal.

I know how to write the list of mammal common names. I know how to write all the info for that mammal to a static page.

However, I can't figure out how to make clicking the echo'd common_name a link to a new instance of mammal.php.

I really don't want to make 100+ pages, one for each mammal, amphibian and reptile. I want to create a function that can be used for all three.

while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['common_name'] . "</td>";
echo "</tr>";

Where do I put in the POST, the Variable, the link, the function?

There must be a simple way to use 'common_name' in the naming paradigm for the on-click variable, such as $Common_Name = $result, but one that creates a unique instance of that variable on-click, so that the function can use that info to compare it to the table values and WHERE they are equal, write a new instance of the page.

No, this is not for an assignment. This is for a professor's side project website. Yeay me ;)

01-18-2012, 02:30 AM
echo "<td><a href='show.php?name={$row['common_name']}&type=mammal'>{$row['common_name']}</td>";

Then you have a php script called "show.php" that gets the name.

// connect to database .... at the top of script


// do the query
$query="SELECT * FROM $type WHERE common_name='$name' ";

// blah blah

// loop through the result (you should only have 1 row) and display the information.


01-18-2012, 02:34 AM
Awesome! Give me a minute to test it and I'll post if I have a clue or not ;)

01-18-2012, 03:32 AM

At least in part.

Now it doesn't recognize the comparison of common_name to $name in show.php, because it wants to change all spaces in that name to "%20", like "Box Turtle" becomes "Box%20Turtle", so when we select where common_name is equal to $name, they are different, so it doesn't list anything.

Anyway to fix this?

01-18-2012, 03:48 AM
Well, I tested the value of $name by echoing it before writing the table, and $name does not contain the %20, just regular spaces, so no worries there. However, it still is pulling no value for the comparison of common_name and $name. I'll keep puttering.

01-18-2012, 04:17 AM

Here's the place where it's hanging up on show.php....

echo $name;
echo $type;

// do the query
$query="SELECT * FROM $type WHERE common_name='" . $name . "'";

echo $name . "<br>";

Everything before the query works, so we have solid usable values for $name and $type, so it should work in the query.

But, everything after the query doesn't work. It selects nothing, thinking there are no instances where $name and common_name are the same.

Any suggestions?

01-18-2012, 06:23 AM

Hubby Jim helped me to see that I'd forgotten the mysql_query in my query :)

It works!

01-18-2012, 12:19 PM
You should also know that you can combine, or JOIN tables when doing queries.
I didn't mention that before because it seems like you're a total beginner. What
you're doing now with separate queries is fine. You'll discover more advanced things
as time goes by.

Google will be your best source of examples and tutorials.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum