Jump to content


Photo

How can i display this field?


  • Please log in to reply
5 replies to this topic

#1 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 24 October 2006 - 08:33 PM

Hello.

Currently i have a a field in a database which contains a link such as 'http://www.google.com' i then display this using:
    <td height="23" colspan="3"><strong>Link:</strong>      <?php if ($qry[link] == ""){
										print "Link not available yet";
										} else {
										print "<a href=$qry[link] target=_blank>$qry[link]</a>";
										} ?></td>

So basically just using $qry[link] to display it.

But i am looking to change it slightly so many links can be added seperated by commas so eg:
"http://www.google.co.../www.yahoo.com", although i am unsure how i can display these seperately, so it says:
Link 1: http://www.google.com
Link 2: http://www.msn.com
Link 3: http://www.yahoo.com
Etc.... depending on how many there are

I'm 95% sure i will never need more than 4 links.

Is it possible to do this, and how?

Any links/code/tutorials will be welcomed :)

Thanks.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 24 October 2006 - 08:43 PM

You should use the function explode() :)

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 24 October 2006 - 09:26 PM

Thanks.

Hmmm, i'm close now, i have this:

    <?php if ($qry[link] == ""){
										print "Link not available yet";
										} else {
									$pieces = explode(",", $qry[link]);
									echo "Link 1: <a href=$pieces[0]>$pieces[0]</a> <br>"; // piece1
									echo "Link 2: <a href=$pieces[1]>$pieces[1]</a> <br>"; // piece2
									echo "Link 3: <a href=$pieces[2]>$pieces[2]</a> <br>"; // piece3
									echo "Link 4: <a href=$pieces[3]>$pieces[3]</a> <br>"; // piece4
										//print "<a href=$qry[link] target=_blank>$qry[link]</a>";
										} 
										?>

Although it will say 'link 3' and 'link 4' even if there isn't one. How can i get around this?

Thanks.

#4 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 25 October 2006 - 12:50 PM

bump :)

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 02:18 PM

Use a foreach loop...

<?php
if ($qry['link'] == ""){ // If the link is empty, output message
   echo "Link not available yet";
}
else {
   $links = explode(",", $qry['link']); // Put the links into an array split on a comma
   $count = 1;
   foreach ($links as $link){
      echo "Link $count: <a href=$link>$link</a><br>"; // Output the link including the link number
      $count++;
   }
} 
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 25 October 2006 - 04:41 PM

Spot on. Thanks! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users