Forum Moderators: coopster
I made script that gets values from a db and outputs them with a pipe character ( ¦ ) in between the values like so:
$query = "SELECT DISTINCT genre FROM jokes";//and the rest you knowecho "<p align=\"center\">";
while ($row = mysql_fetch_array($result)) {
$genre_pipe = "<a href=\"?pageid=2\">". ucfirst($row['genre']) ."</a>";
$genre_pipe .= " ¦ ";
$genres = str_replace("¦ ¦", $replace, $genre_pipe);
/*test if it works:if($genres === true){
echo $genres;
}
else{
echo "bad one";
}*/
echo $genres
}
echo "</p>";
When I say echo $genre it prints out everyhting like it would be w/o str_replace().
And when I tried the if statement to see if it worked it always echoed "the bad one" over and over.
before i did the str_replace() i did substr($genre_pipe, 0, -1) which did not work for some reason
Thanks,
electricocean
Here's an alternate without str_replace.
///
$query = "SELECT DISTINCT genre FROM jokes";
echo "<p align=\"center\">";
$list='';$first=0;
while ($row = mysql_fetch_array($result)) {
if ($first==0){$pipe='';}else{$pipe=' ¦ ';}
$list.="$pipe"."<a href=\"?pageid=2\">". ucfirst($row['genre']) ."</a>";
$first++;
/*test if it works:
if($genres === true){echo $genres;}
else{echo "bad one";}
*/
echo $list}echo "</p>";
------------------------
This way you build a string rather than editing one. There is no pipe before the 1st item ,or after the last, since the pipe is placed before the link, but skips the first one. Pipes a have a once space pad on each side and are not included in the link.
Hope with works better.