Jump to content


Photo

links within database


  • Please log in to reply
6 replies to this topic

#1 headcutter

headcutter
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 May 2006 - 03:38 PM

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.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 May 2006 - 03:44 PM

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


#3 headcutter

headcutter
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 May 2006 - 06:04 PM

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.


#4 headcutter

headcutter
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 08 May 2006 - 01:26 PM

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();
?>

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 May 2006 - 04:22 PM

Sure. Change this bit:
Print "<td><a href=\"pets.html\">".$row['pet'] . "</a></td></tr>";
to
Print "<td><a href=\"" . $row['pet'] . ".html . "\">".$row['pet'] . "</a></td></tr>";


#6 headcutter

headcutter
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 08 May 2006 - 06:41 PM

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




#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 May 2006 - 07:09 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users