Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create/format text string

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
I want to show used components in a line; I have this code

if ($d1>0) {
$d1line="D1 Single Wing Door";
}
else {
$d1line="";
}
if ($d2>0) {
$d2line="D2 Double Wing Door";
}
else {
$d2line="";
}
if ($d1e>0) {
$d1eline="Panic Exit for D1";
}
else {
$d1eline="";
}
if ($d2e>0) {
$d2eline="Panic Exit for D2";
}
else {
$d2eline="";
}
if ($d3>0) {
$d3line="D3 Single Wing Sliding Door";
}
else {
$d3line="";
}
if ($d5>0) {
$d5line="D5 Double Wing Sliding Door";
}
else {
$d5line="";
}
if ($d7>0) {
$d7line="D7 Single Wing, Side Sliding Door";
}
else {
$d7line="";
}
if ($w1>0) {
$w1line="W1 Sliding Window with Screen";
}
else {
$w1line="";
}
if ($hpd1>0) {
$hpd1line="Hydraulic Pump for D1";
}
else {
$hpd1line="";
}
if ($hpd2>0) {
$hpd2line="Hydraulic Pump for D2";
}
else {
$hpd2line="";
}
$accessories=$d1line.", ".$d2line.", ".$d1eline." ".$d2eline.", ".$d3line.", ".$d5line.", ".$d7line." ".$w1line.", ".$hpd1line.", ".$hpd2line;
if ( $accessories[0] == "," ) $accessories = substr($accessories, 1);
...
<p>
<?="$accessories"?>
</p>

is there any way how to format it nicely - so if some/all of them selected they are divided by a comma, if none is selected don't show any commas, etc.
 
If your accessories information (d1line, d2line, hpd2line, etc) were stored in an array. eg.

Code:
$accessories = null;

$accessoriesList = array($d1line, $d2line, $d1eline, $d2eline, $d3line, $d5line, $d7line, $w1line, $hpd1line, $hpd2line);

foreach($accessoriesList as $value) {
	if(!empty($value)) {
		if(!empty($accessories)) {
			$accessories .= ',';
		}
		
		$accessories .= $value;
	}
}

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
try this code
Code:
$text = array();
for ($i=1; $i<=7; $i++){
 $var = 'd'.$i .'line';
 if (!empty(${$var})) { $text[] = ${$var} };
}
echo implode (', ',$text);

do the same for hpd or even implement it directly within the first loop.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top