headcutter Posted May 7, 2006 Share Posted May 7, 2006 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.htmlThanks for any help- I cant find anything on the internet. Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/ Share on other sites More sharing options...
wildteen88 Posted May 7, 2006 Share Posted May 7, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34054 Share on other sites More sharing options...
headcutter Posted May 7, 2006 Author Share Posted May 7, 2006 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: CatHow can I modify it to this?:Name: Pet:Rose CatBradley FrogMarie DogAnn CatRose CatBradley FrogMarie DogAnn CatI 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. Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34097 Share on other sites More sharing options...
headcutter Posted May 8, 2006 Author Share Posted May 8, 2006 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();?> Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34272 Share on other sites More sharing options...
wildteen88 Posted May 8, 2006 Share Posted May 8, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34327 Share on other sites More sharing options...
headcutter Posted May 8, 2006 Author Share Posted May 8, 2006 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=1Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRINGwhile($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>";} Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34373 Share on other sites More sharing options...
wildteen88 Posted May 8, 2006 Share Posted May 8, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9245-links-within-database/#findComment-34382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.