Revolutsio Posted May 17, 2022 Share Posted May 17, 2022 How can i get this code to work? What I want to do is if the field platform2 has text in it then it will put some divs on the screen <?php if($game['platform2'] ==" "){?> <div class="field-container"> <?php }else {?> <div class="field-container"> <div class="title"> Platform </div> <div class="information"> <?php echo $game['platform2']; ?> </div> <div class="title"> Launcher </div> <div class="information"> <?php echo $game['launcher2']; ?> </div> </div> <?php }?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 17, 2022 Share Posted May 17, 2022 This is how the code could look: if($game['platform2'] ==" ") echo "<div class='field-container'>"; else echo "<div class='field-container'> <div class='title'>Platform</div> <div class='information'>{$game['platform2']}</div> <div class='title'>Launcher</div> <div class='information'>{$game['launcher2']}</div> </div> "; Problem is - you say you want to check if 'platform2' has text in it but you are testing if it has a space in it. Perhaps you want to check if it is not equal to '' ? Quote Link to comment Share on other sites More sharing options...
Revolutsio Posted May 17, 2022 Author Share Posted May 17, 2022 8 minutes ago, ginerjm said: This is how the code could look: if($game['platform2'] ==" ") echo "<div class='field-container'>"; else echo "<div class='field-container'> <div class='title'>Platform</div> <div class='information'>{$game['platform2']}</div> <div class='title'>Launcher</div> <div class='information'>{$game['launcher2']}</div> </div> "; Problem is - you say you want to check if 'platform2' has text in it but you are testing if it has a space in it. Perhaps you want to check if it is not equal to '' ? Yes that is what i would like to do Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 17, 2022 Share Posted May 17, 2022 (edited) So change the if to match what output you want. If plafrorm2 is empty (ie, == '') and you want to only show the one div then make it so. If it is the other way change the if to <> '' More importantly note how I altered your output style. Entering and leaving php mode to do output is such a tedious method of coding when you can avoid it by STAYING in php mode and using the echo. Or by reading up on php's "heredocs" command that makes it much easier to output html and non-php code as well as php code. Edited May 17, 2022 by ginerjm Quote Link to comment Share on other sites More sharing options...
Revolutsio Posted May 17, 2022 Author Share Posted May 17, 2022 I only want to show the two divs if there is something in the platform2 I have run the code the way you said but get the same result with == & <> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 17, 2022 Share Posted May 17, 2022 (edited) Show me your new code . And what are the "two divs"? One choice is outputting a single incomplete div and the other is outputting 5 divs. Edited May 17, 2022 by ginerjm Quote Link to comment Share on other sites More sharing options...
Revolutsio Posted May 17, 2022 Author Share Posted May 17, 2022 So to give a bit of the story it should only show the divs if there is text in the platform2 field. And not show if there is nothing in the field. if($game['platform2'] >"") echo "<div class='field-container')"; else echo "<div class='title'>Platform</div> <div class='information'>{$game['platform2']}</div> <div class='title'>Launcher</div> <div class='information'>{$game['launcher2']}</div> </div>"; ?> Also could i put another div class='field-container' after the last div to add more later? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 17, 2022 Share Posted May 17, 2022 Ok - this code says to show the field-container div if your field is not empty. And to show the 4 other divs if it is empty. Plus you have an extra </div> tag in the else part that is not necessary. PS - not a good idea to use the dash character in names, just use the underscore. As for adding things, of course you can. Just code them. Quote Link to comment Share on other sites More sharing options...
Solution dodgeitorelse3 Posted May 17, 2022 Solution Share Posted May 17, 2022 if I understand correctly this should work. echo "<div class='field-container'>"; if($game['platform2'] !== ""){ echo "<div class='title'>Platform</div> <div class='information'>{$game['platform2']}</div> <div class='title'>Launcher</div> <div class='information'>{$game['launcher2']}</div>"; } echo "</div>"; Quote Link to comment 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.