Jump to content

Display <div> based on php variable


larry29936

Recommended Posts

I'm trying to display an html <div> based on the state of a variable set during php execution . The variable is $chk and is set to either 0 or 1 with 0 meaning failed and 1 meaning  pass. Here's the code:

if <?php echo "{$chk}";?> == 0
  <div class="container">
      <div class="row" style="color:red">
        <br><br><br><br><br><br>  
        <center>Database Update Failed</center>
      </div>
  </div>

else
  <div class="container">
      <div class="row">
        <br><br><br><br><br><br>  
        <center>Database Updated</center>
      </div>
  </div>


 Both of the <div>'s display after the completion of the php instead of the one based on the $chk variable. I've tried several ways to get the value of $chk but neither print_r nor echo seem to work. $chk is set to 0 at the start of the php execution.

Thanks in advance, Larry

Edited by larry29936
additioal info
Link to comment
Share on other sites

Your 'if' makes no sense. 'if' is a PHP statement yet you are starting PHP after the if. Also you are testing the result of 'echo' not the value of $chk. I am guessing but I think you want:

<?php if ($chk == 0) { ?>
.
.
.
<?php } else { ?>
.
.
.
<?php } ?>

That having been said, it is a poor way to program. You should do something more like:

<?php
if ($chk} == 0) {
echo "
  <div class="container">
      <div class="row" style="color:red">
        <br><br><br><br><br><br>  
        <center>Database Update Failed</center>
      </div>
  </div>"
}
else {
echo "
  <div class="container">
      <div class="row">
        <br><br><br><br><br><br>  
        <center>Database Updated</center>
      </div>
  </div>"
}
?>

 

Edited by gw1500se
Link to comment
Share on other sites

@gw1500se  OK, using your code as follows:

 

       $stmt = $pdo->prepare("UPDATE files SET filename = '$srcname', logtime=now() WHERE id = 4");
        $stmt->execute() ;    
	$chk=1;
 }


if ($chk == 0) {
echo \"
  <div class="container">
      <div class="row" style="color:red">
        <br><br><br><br><br><br>  
        <center>Database Update Failed</center>
      </div>
  </div>"
}

else {
echo \"
  <div class="container">
      <div class="row">
        <br><br><br><br><br><br>  
        <center>Database Updated</center>
      </div>
  </div>"
}
?>

I  get the following error on the line with the echo:

Parse error: syntax error, unexpected '"' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) in /home/larry/web/test/public_html/update.php on line 141

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.