Th3Boss Posted March 23, 2013 Share Posted March 23, 2013 (edited) This is how I currently have it setup <?php if ($high1name == "open") { ?> <div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div> <?php } elseif ($high1name == "none") { ?> <div class="module high inactive"> </div> <?php } else { ?> <div class="module high"> <img src="http://image.website.com/Type/<?php print($high1typeID);?>_64.png"> </div> <? } ?> But I would like to have it something like this <?php if ($high1name == "open") { ?> <div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div> <?php } elseif ($high1name == "ANY VALUE HERE") { ?> <div class="module high"> <img src="http://image.website.com/Type/<?php print($high1typeID);?>_64.png"> </div> <?php } else { ?> <div class="module high inactive"> </div> <? } ?> How do I get the elseif to work for any value $high1name has? Edited March 23, 2013 by Th3Boss Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/ Share on other sites More sharing options...
Barand Posted March 23, 2013 Share Posted March 23, 2013 What is the final "else" for? I presume it's for any other value that's not already included in "any value here" Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420481 Share on other sites More sharing options...
Th3Boss Posted March 23, 2013 Author Share Posted March 23, 2013 I want it if $high1name == "open" then it shows div 1 if $high1name has any other value show div 2 otherwise show div 3 Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420482 Share on other sites More sharing options...
moltm4785 Posted March 23, 2013 Share Posted March 23, 2013 Wait thats confusing ok, you want $high1name == "open" $high1name == "ANYTHING ELSE" $high1name == "" if the second one is going to be anything else what is the logic situation that would occur to reach the else statement? Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420486 Share on other sites More sharing options...
Th3Boss Posted March 23, 2013 Author Share Posted March 23, 2013 (edited) Actually I just found out about switch statements and this seems to do what I want <?php if ($high7name == "") { ?> <div class="module high inactive"> </div> <? } else { ?> <? switch ($high7name) { case "none": ?> <div class="module high inactive"> </div> <? break;?> <? case "open": ?> <div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div> <? break;?> <? default: ?> <div class="module high"> <img src="http://image.website.com/Type/<?php print($high7typeID);?>_64.png"> </div> <? } ?> <? } ?> Not sure if that is the best way to write it or not but currently it is working as I want it to. Edited March 23, 2013 by Th3Boss Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420487 Share on other sites More sharing options...
Th3Boss Posted March 23, 2013 Author Share Posted March 23, 2013 ok and it seems this does the same thing but as an if statement <?php if ($high1name == "open") { ?> <div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div> <?php } elseif ($high1name == "") { ?> <div class="module high inactive" > </div> <?php } else { ?> <div class="module high" data-typeid=$high1typeID data-name=$high1name> <img src="http://image.website.com/Type/<?php print($high1typeID);?>_64.png"> </div> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420488 Share on other sites More sharing options...
Th3Boss Posted March 23, 2013 Author Share Posted March 23, 2013 Would it be best to use the if statements or switch in this situation? Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420489 Share on other sites More sharing options...
Solution PaulRyan Posted March 23, 2013 Solution Share Posted March 23, 2013 You shouldn't jump in and out of PHP tags like that, it makes for messy code. Try this: <?PHP if($highname1 == 'open') { $output = '<div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div>'; } else if(strlen(trim($highname1))) { $output = '<div class="module high inactive"> </div>'; } else { $output = '<div class="module high" data-typeid=$high1typeID data-name=$high1name> <img src="http://image.website.com/Type/'. $high1typeID .'_64.png"> </div>'; } echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420492 Share on other sites More sharing options...
Th3Boss Posted March 23, 2013 Author Share Posted March 23, 2013 You shouldn't jump in and out of PHP tags like that, it makes for messy code. Try this: <?PHP if($highname1 == 'open') { $output = '<div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div>'; } else if(strlen(trim($highname1))) { $output = '<div class="module high inactive"> </div>'; } else { $output = '<div class="module high" data-typeid=$high1typeID data-name=$high1name> <img src="http://image.website.com/Type/'. $high1typeID .'_64.png"> </div>'; } echo $output; ?> That worked though they were switched around. Works as wanted like this: <?PHP if($high2name == 'open') { $output = '<div class="module high open"> <img src="Icon_hi_slot.png" width="46" height="46"> </div>'; } else if(strlen(trim($high2name))) { $output = '<div class="module high" data-typeid=$high2typeID data-name=$high2name> <img src="http://image.website.com/Type/'. $high2typeID .'_64.png"> </div>'; } else { $output = '<div class="module high inactive"> </div>'; } echo $output; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420496 Share on other sites More sharing options...
PaulRyan Posted March 23, 2013 Share Posted March 23, 2013 My mistake, pasted into the wrong sections. Quote Link to comment https://forums.phpfreaks.com/topic/276045-if-statements/#findComment-1420497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.