Jump to content


Photo

Creating links from a list


  • Please log in to reply
5 replies to this topic

#1 unklematt

unklematt
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 05 March 2006 - 11:08 PM

I have a question to ask -- as normal...

I have a row of pages in a list ie:


link1,link2,link3,link4


which are written to my database just using a $link variable

How do I get them to be like


<a href='http://link1'>link1</a>'
<a href='http://link2'>link2</a>'
<a href='http://link3'>link3</a>'
<a href='http://link4'>link4</a>'

I know how to do the str_replace etc but the getting them link is doing my head in LOL[quote]

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 05 March 2006 - 11:12 PM

If you are storing them in a database, like you say, this should work fine...
<?php
$result = mysql_query("SELECT `link` FROM `table`") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
    echo "<a href='http://$row[link]'>$row[link]</a><br/>";
}
?>


#3 unklematt

unklematt
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 05 March 2006 - 11:43 PM

[!--quoteo(post=351923:date=Mar 5 2006, 06:12 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Mar 5 2006, 06:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If you are storing them in a database, like you say, this should work fine...
<?php
$result = mysql_query("SELECT `link` FROM `table`") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
    echo "<a href='http://$row[link]'>$row[link]</a><br/>";
}
?>
[/quote]

That just seems to bring it out as one link

http://link1,link2,link3,link4


here is the chunk of code I am using..
echo "<tr><td>Link:</td><td>";
$name=$row['name'];
$result1 = mysql_query("SELECT `link` FROM `myjunk`where name like '$name'") or die(mysql_error());
while($row = mysql_fetch_assoc($result1)) {
echo "<a href='links/$row[link]'>$row[link]</a><br/>";
}
echo"</td></tr>";
}


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 March 2006 - 12:58 AM

Do you mean that you have one field in each row of your database that looks like url1,url2,url3 (all joined up but separated with commas)? If that's so you you need to retrieve the field and then use the explode() function on the comma separator to generate an array of links ...
Legend has it that reading the manual never killed anyone.
My site

#5 unklematt

unklematt
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 06 March 2006 - 08:20 AM

[!--quoteo(post=351943:date=Mar 5 2006, 07:58 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Mar 5 2006, 07:58 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Do you mean that you have one field in each row of your database that looks like url1,url2,url3 (all joined up but separated with commas)? If that's so you you need to retrieve the field and then use the explode() function on the comma separator to generate an array of links ...
[/quote]

Excellent thank you... worked a treat.

#6 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 06 March 2006 - 08:23 AM

Comma separated data is bad database design. Just so you know. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users