Jump to content

Nesting an if statement


facarroll

Recommended Posts

This code returns part but not all of what I'm asking it to do.

Variable $av1 is drawn from the database and always has a value of 1 or 0 (tinyint).

If value is 1 a green ON is displayed and if 0 a red OFF is displayed. That works.

The variable $group1 exists or not, and if exists the code should proceed and if not exist there should be a display of "".

In other words, I do not want to see any display if $group1 does not exist, but this is not working. There is always a display.

Can anyone point out my mistake?

 

<?php 
if($group1 != "") {
{
	echo "<input type='radio' name='1' value='$group1' />"; ?>  <?php echo $group1; 
	}
	?> 
        is switched 
	<?php 
	if($group1 != "") if($av1 !== 0) echo ("<span style=\"color:#080\">ON</span>"); 
	else echo ("<span style=\"color:#F00\">OFF</span>");
	}else{
		echo "";
		}
		?>

Link to comment
Share on other sites

Try...

<?php
if(strlen(trim($group1))<1){
/* display nothing */
}else{
echo "<input type='radio' name='1' value='$group1' />"; ?>  <?php echo $group1; 
?> is switched <?php
if($av1 == 1){
	echo ("<span style=\"color:#080\">ON</span>");
}else{
	echo ("<span style=\"color:#F00\">OFF</span>");
}
}
?>

Link to comment
Share on other sites

It seems like you have an extra curly bracket in the beginning:

 

<?php
if($group1 != "") {
    {  //<--- REMOVE THIS BRACKET

...
?>

 

 

But that doesn't seem to be an issue. I also think you need to add a curly bracket:

 

<?php
...

if($group1 != "") { //<--- ADD THE BRACKET HERE

     if($av1 !== 0) echo ("<span style=\"color:#080\">ON</span>");
     else echo ("<span style=\"color:#F00\">OFF</span>");
}else{
     echo "";
}
?>

 

 

Note sure if that will fix the problem, but it seems to work on my end.

Link to comment
Share on other sites

Thanks for your contributions everyone, but nothing works so far.

 

Litebearer, FYI $group1 is one of a selection of six variables that are simply strings (varchar) contributed by a subscriber, however it is only necessary that $group1 is set, because the subscriber may only want to use one group. I don't think that is an issue, but I have been struggling to get the curly braces correct.

 

If I restate the problem with an example it might help.

I have an output that is displayed six times according to the variables $group1 ... $group 6 and variables $av1 ... $av6 will always have a variable of 1 (representing ON) or 0 (representing OFF).

 

If the group represented by $group? does not exist, then there should be no display for that line.

When the $group? does exist, then I want to display a line like this for each existing group, depending on whether $av? is a 1 or a 0. (ON or OFF).

 

"radio button image" Group name is switched ON

or

"radio button image" Group name is switched OFF

 

The display of the above coloured text works fine, but the whole display always exists when it should sometimes actually be absent if there is no value (does not exist) for the $group? variable in the database.

 

Now you know why my head hurts.

Link to comment
Share on other sites

 

I got it with this;

<?php if($group1 != "") {{echo "<input type='radio' name='1' value='$group1' />"; ?>
                          <?php echo $group1; }?> is switched <?php if($group1 != "") if($av1 !== 0) echo ("<span style=\"color:#080\">ON</span>"); else echo ("<span style=\"color:#F00\">OFF</span>");}else{echo"";}?>

Thanks everyone.

Link to comment
Share on other sites

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.