Help With Appending '>' In Between Some Text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Help With Appending '>' In Between Some Text

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

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

[php_man]foreach[/php_man]()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how about just [php_man]implode[/php_man]():

Code: Select all

<?php

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

echo implode(' > ', $arr);

?>
Post Reply