IF statement based on PHP true or false

Is it possible to have a if statement which is based on if a PHP /mySql value is true or false?

Thank you in advance

Well PHP is certainly capable of writing the word ‘true’ or ‘false’ into javascript…

if(<?php echo "true"; ?>) { ...

(but more likely the case that PHP would do its own if, and then simply write/not write the code block as necessary)

@m_hutley. Thank you for the reply. This is about showing a Bootstrap modal that based on if a certain cookie exist and next to that if a special promotion is available (query from database):

SELECT *
  FROM `promotions`
 WHERE `isActive` = 1
 LIMIT 1

So if the Cookie doesn’t exists and a promotion is available the modal should appear

PHP is capable of receiving/inspecting cookies, and would have the information available.

You’ve mentioned having other modal popups before, so what i’d suggest is that PHP control the existance of the modal itself.
(spitballing code, untested and probably not correct)

<?php if($_COOKIE['imateacup'] && $promotions == 1) { 
  //Output promo modal, and arm it to display.
?>
 <div id='modalpopup' > ....</div>
 <script>setTimeout(document.getElementById('modalpopup').show(),3000);</script>
<?php
} else if(....) { ....}
....

@m_hutley Thank you again for the reply, very much appreciated. I have a problem with checking for a Cookie using PHP as in your example. The modal is showing but I get a Notice: Undefined index: newsletter_popup messages; This is what I have:

<?php if ($_COOKIE['newsletter_popup'] == null): ?>

That means that this cookie value does not exist in the submitted cookie data.

@Dormilich Thank you for the reply. I changed the if statement to:

<?php if(!isset($_COOKIE['newsletter_popup'])) : ?>

and it is working ok now.

1 Like

Just something to be aware of when dealing with cookies - when you update the cookie that change is not visible until after you reload the page.

That’s a problem that I don’t see happening here, but is helpful to know when developing things later on.

@Paul_Wilkins. Thank you for the reply and advice

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.