Jump to content

only return output if variable contains value - LONG column of variables


glennn.php

Recommended Posts

i have a LONG column of variable, only some of which contain a value, at different times, i.e.

	
$pagetext .= $o1."<br />";		
$pagetext .= $o2."<br />";		
$pagetext .= $o3."<br />";		
$pagetext .= $o4."<br />";	
$pagetext .= $o5."<br />";	
$pagetext .= $o6."<br />";	
$pagetext .= $o7."<br />";	
$pagetext .= $o8."<br />";	
$pagetext .= $o9."<br />";	
$pagetext .= $o10."<br />";	
$pagetext .= $o11."<br />";	
$pagetext .= $o12."<br />";	
$pagetext .= $o13."<br />";	
$pagetext .= $o14."<br />";	

 

(it's much longer) - i don't think i want to apply an if($o1 != "") statement to each one, or even a switch()... i'm sure there's a way to print these ONLY if they contain a value, but i'm not good enough to know how this might be done. can someone with some exp. show me how i might accomplish this? (they go o1 through o40)

 

i really appreciate it.

 

GN

 

glennn.php

 

I don't see any way around checking the variables for NULL...

 

However, if you gather your variables under the umbrella on an array, this is easily accomplished.

 

The following script does ( I think ) what you want.

 

Let us know.

 

Scot L. Diddle, Richmond VA

 


<?php

Header("Cache-control: private, no-cache");
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header("Pragma: no-cache");

$pagetext = "";

$o = array();

$o[0]  = '';
$o[1]  = "Hi Mom";
$o[2]  = '';
$o[3]  = '';
$o[4]  = '';
$o[5]  = '';
$o[6]  = '';
$o[7]  = '';
$o[8]  = '';
$o[9]  = '';
$o[10]  = '';
$o[11]  = '';
$o[12] = "Bye Mom";
$o[13]  = '';
$o[14]  = '';
$o[15]  = '';
$o[16]  = '';
$o[17]  = '';
$o[18]  = '';
$o[19]  = '';
$o[20]  = '';
$o[21]  = '';
$o[22]  = '';
$o[23]  = '';
$o[24]  = '';
$o[25]  = '';
$o[26]  = '';
$o[27]  = '';
$o[28]  = '';
$o[29]  = '';
$o[30]  = '';
$o[31]  = '';
$o[32]  = '';
$o[33]  = '';
$o[34]  = '';
$o[35]  = '';
$o[36]  = '';
$o[37]  = '';
$o[38]  = '';
$o[39]  = '';
$o[40]  = '';

foreach ($o as $IDX => $oOut) {

if ($oOut != NULL) {
	$pagetext .= "Item: $IDX = $oOut <br /> \n";
}

}

echo $pagetext;

?>

ok - $o* are data retrieved from a db, so i can do

 

$o = array();

 

$o[0]  = '$01';

$o[1]  = "$02";

...

 

?

 

looks like that's what i want instead of 40 if() statements...

 

i'll let you know...

 

thanks

I would use a variable variable (see: http://us2.php.net/manual/en/language.variables.variable.php) here:

<?php
$o1 = 'yes';
$o3 = 'why';
$o7 = 'no';
$o2 = '';
$o4 = '';
$o5 = '';
$o6 = '';
$o8 = '';
for ($i=1;$i<9;++$i) {
  if (${'o'.$i} != '') echo ${'o'.$i} . "<br>\n";
}
?>

 

Ken

brilliant on both of you -

 

this worked great

 

for ($i=1;$i<41;++$i) {
  if (${'p'.$i} != '') $pagetext .= ${'p'.$i} . "<br>\n";
}

 

lots less code. thanks both of you...

 

see Options and Price columns at the bottom here:

 

http://custom-shipping-cases.com/site5/item-details.php?pageid=234

 

http://custom-shipping-cases.com/site5/item-details.php?pageid=160

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.