Jump to content

Draw on screen with an if statement


Revolutsio
Go to solution Solved by dodgeitorelse3,

Recommended Posts

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 }?>

 

Link to comment
Share on other sites

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 '' ?

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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>";

 

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.