Jump to content

Archived

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

headcutter

links within database

Recommended Posts

Hi, I am using this code to create a table:

CREATE TABLE friends (name VARCHAR(30), fav_color VARCHAR(30), fav_food VARCHAR(30), pet VARCHAR(30));
INSERT INTO friends VALUES ( "Rose", "Pink", "Tacos", "Cat" ), ( "Bradley", "Blue", "Potatoes", "Frog" ), ( "Marie", "Black", "Popcorn", "Dog" ), ( "Ann", "Orange", "Soup", "Cat" )

I display the table by this code:

Print "<table border cellpadding=0>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>";
}
Print "</table>";
?>

What should I do if I want all of the pets to be clickable (links)? Is there some way to integrate
<a href="pets.html"> into the code? I want the users to be able to click on the pets name for example frog and I want them to be redirected to pets.html
Thanks for any help- I cant find anything on the internet.

Share this post


Link to post
Share on other sites
change the following:
[code]Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>";[/code]
to:
[code]Print "<th>Pet:</th> <td><a href=\"pets.html\">".$info['pet'] . "</a></td></tr>";[/code]

Share this post


Link to post
Share on other sites
Thanks, that worked.
I was also wondering if there is an easy way to link each animal to webpages of each animal (frog.html, dog.html and cat.html) instead of linking all of the animals to one file. Any ideas?


Additionaly I have a question about displaying data in colums instead of rows.
It looks like this right now:
Name:Rose Pet: Cat
Name:Bradley Pet: Frog
Name:Marie Pet: Dog
Name:Ann Pet: Cat
Name:Rose Pet: Cat
Name:Bradley Pet: Frog
Name:Marie Pet: Dog
Name:Ann Pet: Cat

How can I modify it to this?:
Name: Pet:
Rose Cat
Bradley Frog
Marie Dog
Ann Cat
Rose Cat
Bradley Frog
Marie Dog
Ann Cat

I know I probably have to modify this piece of code:
Print "<table border cellpadding=0>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Pet:</th> <td><a href=\"pets.html\">".$info['pet'] . "</a></td></tr>";
}
Print "</table>";

Again thanks for any help.

Share this post


Link to post
Share on other sites
Displaying in colums is working now.

I get this error:
parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING, this line: Print "<tr><td><a href=\".$row['link'] . "\>".$row['name'] . "</td> ";

when I want each of the entries to have seperate links. I created a "link" variable to store the link address.

Here is the code:

$data = mysql_query("SELECT * FROM friends ORDER BY name")
or die(mysql_error());
Print "<table border=0 width=100%>";
Print "<tr>";
Print "<th>Name:</th>";
Print "<th>Pet:</th> </tr>";
while($row = mysql_fetch_array( $data ))
{
Print "<tr><td><a href=\".$row['link'] . "\>".$row['name'] . "</td> ";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
}
Print "</table>";
mysql_close();
?>

Share this post


Link to post
Share on other sites
Sure. Change this bit:
[code]Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";[/code]to
[code]Print "<td><a href=\"" . $row['pet'] . ".html . "\">".$row['pet'] . "</a></td></tr>";[/code]

Share this post


Link to post
Share on other sites
I am getting an error:
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING
(Print "<td><a href=\" . $row['pet'] . ".html . "\">".$row['pet'] . "</a></td>";)

With this code:
while($row = mysql_fetch_array( $data ))
{
Print "<td><a href=\" . $row['pet'] . ".html . "\">".$row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
}

And 2 errors if I use this code:
Warning: Unexpected character in input: '\' (ASCII=92) state=1
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING


while($row = mysql_fetch_array( $data ))
{
Print "<td><a href=\"" . $row['pet'] . ".html . "\">".$row['pet'] . "</a></td>";
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
}


Share this post


Link to post
Share on other sites
Oops. I think that might be my fault there. Change this:
[code]Print "<td><a href=\" . $row['pet'] . ".html . "\">".$row['pet'] . "</a></td>";[/code]
to the following:
[code]Print "<td><a href=\"" . $row['pet'] . ".html\">" . $row['pet'] . "</a></td>";[/code]
And your error should be sorted out.

Share this post


Link to post
Share on other sites

×

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.