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

 

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.