Skip to main content
January 5, 2010
Answered

how to print a comma-separated entry

  • January 5, 2010
  • 1 reply
  • 724 views

I would like to print a comma-separated entry into separated entries:

Like when I put like <?php echo $row_rsContent['artists']; ?> I get: "Picasso,Chagall,Matisse" but I want it like "Picasso<br>Chagall<br>Matisse".

And also I want to link "Picasso" to another website.

Thanks for any furthre help.

This topic has been closed for replies.
Correct answer David_Powers

Pass your variable to explode() to turn it into an array. Then do what you want with each element.

$artists = explode(',', $row_rsContent['artists']);

echo '<a href="link">' . $artists[0] . </a><br>';

echo $artists[1]. '<br>' . $artists[2] . '<br>';

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
January 5, 2010

Pass your variable to explode() to turn it into an array. Then do what you want with each element.

$artists = explode(',', $row_rsContent['artists']);

echo '<a href="link">' . $artists[0] . </a><br>';

echo $artists[1]. '<br>' . $artists[2] . '<br>';

January 6, 2010

Thanks a lot again.
I put like that (see below) .. because I have maybe even 10 or more "artists" I wonder if there is a possibility to do this in a kind of "repeated region"?

   <?php $artists = explode(',', $row_rsContent['nameArtist']); ?>
   <a href="artist2.php?cat=<?php echo $row_rsContent['casco_cat_id']; ?>&artist=<?php echo $artists[0]; ?>"><?php echo $artists[0]; ?></a><br>
   <a href="artist2.php?cat=<?php echo $row_rsContent['casco_cat_id']; ?>&artist=<?php echo $artists[1]; ?>"><?php echo $artists[1]; ?></a><br>
   <a href="artist2.php?cat=<?php echo $row_rsContent['casco_cat_id']; ?>&artist=<?php echo $artists[2]; ?>"><?php echo $artists[2]; ?></a><br>
   <a href="artist2.php?cat=<?php echo $row_rsContent['casco_cat_id']; ?>&artist=<?php echo $artists[3]; ?>"><?php echo $artists[3]; ?></a><br>

David_Powers
Inspiring
January 6, 2010

Oh, you want them all in links? That makes it a lot easier. Your original message said just the first one would be in a link. The following should do it:

<?php $artists = explode(',', $row_rsContent['nameArtist']);
  foreach($artists as $artist) { ?>
   <a href="artist2.php?cat=<?php echo $row_rsContent['casco_cat_id']; ?>&artist=<?php echo $artist; ?>"><?php echo $artist; ?></a><br>
<?php } ?>