Forum Moderators: coopster

Message Too Old, No Replies

help with displaying comma separted data with php and mysql

php, function, data

         

hanyaz

5:04 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



Hello,
i got some keywords in my mysql database and i want to display them separately as links separated by a "¦"

i am using the following code to so :

<?php 

$Keywords ="<a href=\"cat_sell.php?cid=" .$id. "\">" .$sbrow_off["sb_keywords"]."</a>";

echo str_replace(","," ¦ ", $Keywords );

?>

i got the keywords displayed as i wish them to be, but they appear as a single link.
what should i do to display them as separate links?
Thanks in advance for your help
yaz

coopster

5:36 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to create a loop control structure [php.net] to apply the link to each value in your keyword list.

hanyaz

5:51 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



Thanks for your reply, but please how to use it exactly? I am a real beginner...i have no clue on how to use that.
Thanks
hanyaz

coopster

6:00 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, it looks like you are using two variables
$id
and
$sbrow_off["sb_keywords"]
. I have no idea where
$id
is coming from, but I am guessing the latter variable is coming from the database as you mentioned. And it also looks as if the keywords are comma separated within that variable. So, explode [php.net] on the keywords first to get an array of keywords, then you could use the foreach [php.net] control structure to read each one and build your links accordingly.

hanyaz

8:43 am on Jan 4, 2008 (gmt 0)

10+ Year Member



hello,
i have done the following :

<?
$str = $sbrow_off["sb_keywords"];

$keyword= explode(',', $str, -1);

$arr= print_r($keyword);

?>

The keywords stored in my database in the following way :
keyword1, keyword2, keyword3, keyword4
are now displayed in my browser like this
Array ( [0] => keyword1 [1] => keyword2[2] => keyword3 [3] => keyword4)
when i use the foreach :

<? 
foreach ($arr as $k => $v ) {
echo "\$arr[$k] => $v.\n";

?>


I get an error Warning: Invalid argument supplied for foreach()...

What can i do to solve the problem
thanks
yaz

hanyaz

8:58 am on Jan 4, 2008 (gmt 0)

10+ Year Member



ok i have done it in the following way:

<?
$str = $sbrow_off["sb_keywords"];

$keyword= explode(',', $str, -1);

foreach ($keyword as $k ) {
echo "<a href=\"view_offer.php?id=" .$id. "\">".$k."</a>¦";
}

?>

it seems to work that way

thanks for your help
hanyaz