Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2012, 02:14 AM   PM User | #1
evestudent
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
evestudent is an unknown quantity at this point
creating new version of page each time they click on a link

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>";
}
echo

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

Last edited by evestudent; 01-18-2012 at 02:31 AM..
evestudent is offline   Reply With Quote
Old 01-18-2012, 02:30 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.

PHP Code:
<?php
// connect to database .... at the top of script

$name=mysql_real_escape_string($_GET['name']);
$type=mysql_real_escape_string($_GET['type']);


// 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.

.
mlseim is offline   Reply With Quote
Old 01-18-2012, 02:34 AM   PM User | #3
evestudent
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
evestudent is an unknown quantity at this point
Awesome! Give me a minute to test it and I'll post if I have a clue or not
evestudent is offline   Reply With Quote
Old 01-18-2012, 03:32 AM   PM User | #4
evestudent
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
evestudent is an unknown quantity at this point
did not work

Success!

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?
evestudent is offline   Reply With Quote
Old 01-18-2012, 03:48 AM   PM User | #5
evestudent
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
evestudent is an unknown quantity at this point
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.
evestudent is offline   Reply With Quote
Old 01-18-2012, 04:17 AM   PM User | #6
evestudent
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
evestudent is an unknown quantity at this point
Argh!

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?
evestudent is offline   Reply With Quote
Old 01-18-2012, 06:23 AM   PM User | #7
evestudent
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
evestudent is an unknown quantity at this point
YEAY!

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

It works!
evestudent is offline   Reply With Quote
Old 01-18-2012, 12:19 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Tags
function, link, post, refresh, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:18 PM.


Advertisement
Log in to turn off these ads.