Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/9245-links-within-database/
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.
Link to comment
https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34097
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();
?>
Link to comment
https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34272
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>";
}


Link to comment
https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34373
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.
Link to comment
https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34382
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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