larry29936 Posted June 19, 2020 Share Posted June 19, 2020 (edited) 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 June 19, 2020 by larry29936 additioal info Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 19, 2020 Share Posted June 19, 2020 (edited) 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 June 19, 2020 by gw1500se Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 19, 2020 Share Posted June 19, 2020 Correction: Add \ in front of the " that do not start or end the echo. Quote Link to comment Share on other sites More sharing options...
larry29936 Posted June 19, 2020 Author Share Posted June 19, 2020 @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 Quote Link to comment Share on other sites More sharing options...
larry29936 Posted June 19, 2020 Author Share Posted June 19, 2020 I found a solution as follows: <?php if($chk == 0): ?> <div>stuff....</div> <?php else: ?> <div>other stuff...</div> <?php endif; ?> Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 19, 2020 Share Posted June 19, 2020 You needed to escape the " as I said in my correction. 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.