Jump to content

Recommended Posts

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:
[code=php:0]    <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>[/code]

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.com,http://www.msn.com,http://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.
Link to comment
https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/
Share on other sites

Thanks.

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

[code=php:0]    <?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>";
}
?>[/code]

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

Thanks.
Use a foreach loop...

[code]<?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++;
  }
}
?>[/code]

Regards
Huggie
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.