Jump to content

Archived

This topic is now archived and is closed to further replies.

wwfc_barmy_army

How can i display this field?

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.

Share this post


Link to post
Share on other sites
You should use the function [url=http://www.php.net/manual/en/function.explode.php]explode()[/url] :)

Orio.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.