Page 1 of 1

Help With Appending '>' In Between Some Text

Posted: Tue Aug 17, 2004 11:57 pm
by Subliminal
Hey,

I am sure this is a lot simpler then I have made it so please tear me to shreds with horrible comments.

I have 10 columns of text stored in a MySQL database i would like to append a '>' between each set of text making one string.

For example:

5 4 3 2 1

Would then become

5 > 4 > 3 > 2 > 1

however if one of the columns was empty

_ 4 3 2 1

it would NOT appear as

> 4 > 3 > 2 > 1

But

4 > 3 > 2 > 1

Here is what I did.... Please been gentle!!!

Code: Select all

<?php

$path1 = "{$row["path1"]}";
$path2 = "{$row["path2"]}";
$path3 = "{$row["path3"]}";
$path4 = "{$row["path4"]}";
$path5 = "{$row["path5"]}";
$path6 = "{$row["path6"]}";
$path7 = "{$row["path7"]}";
$path8 = "{$row["path8"]}";
$path9 = "{$row["path9"]}";
$path10 = "{$row["path10"]}";


if(!empty($path10)){echo $path10. " > ";}
if(!empty($path9)){echo $path9 . " > ";}
if(!empty($path8)){echo $path8 . " > ";}
if(!empty($path7)){echo $path7 . " > ";}
if(!empty($path6)){echo $path6 . " > ";}
if(!empty($path5)){echo $path5 . " > ";}
if(!empty($path4)){echo $path4 . " > ";}
if(!empty($path3)){echo $path3 . " > ";}
if(!empty($path2)){echo $path2 . " > ";}
if(!empty($path1)){echo $path1;}	



?>
So ya that kinda works..

Thanks guyz!

Sometime other peoples stupidity is funny

Posted: Wed Aug 18, 2004 12:00 am
by d3ad1ysp0rk

Code: Select all

<?php
$string = "";
foreach($row as $value){
  if(!empty($value)){
    $string .= $value . " > ";
  }
}
?>
How's that?

[php_man]foreach[/php_man]()

Posted: Wed Aug 18, 2004 12:20 am
by feyd
how about just [php_man]implode[/php_man]():

Code: Select all

<?php

$arr = array(5,4,3,2,1);

echo implode(' > ', $arr);

?>