admin管理员组文章数量:1331641
So I have a function which counts the amount of customer orders.
I want to show a congratulations modal that appears when a customer hits a specific amount of orders.
All of my code is working fine, except when I add this part to the IF statement:
|| in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count )
The cookie is being saved properly, and when saved the number is saved as the current value of count
. The idea is that the modal will only show if:
A) their current count is in $the_digits
array & the cookie is not set.
B) their current count is in $the_digits
array & the cookie IS set, however the cookies value does not equal their current order $count
amount.
This is to prevent the modal showing after the initial display, but still allow it to show again if they hit the next $count
amount to move to the next tier.
Can't figure out what is wrong. Or perhaps there is a better way to do this. All help appreciated.
$the_digits = ['3','5','15','30','50'];
if ( in_array($count, $the_digits) && !isset( $_COOKIE[ 'tier_advance' ] ) || in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count ) {
echo'
<script>
jQuery(window).on("load",function(){
jQuery("#tier_advance").modal("show");
});
jQuery(document).on("click", "#tier_confirm", function(){
// Set a cookie
Cookies.set("tier_advance", "' . $count . '", { expires: 356 });
});
</script>
<!-- BEGIN Share Modal -->
<div class="modal fade" id="tier_advance" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h5>CONGRATULATIONS!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body text-center">
<p>You are now a <strong class="all-caps"> ' . $tier_name . ' </strong>tier customer!</p>
<p>Expect some dope rewards to start coming your way!</p>
</div>
<div class="modal-footer modal-footer-centered">
<button id="tier_confirm" type="button" class="btn btn-secondary" data-dismiss="modal">Awesome, thanks!</button>
</div>
</div>
</div>
</div>
<!-- END Share Modal -->';
}
So I have a function which counts the amount of customer orders.
I want to show a congratulations modal that appears when a customer hits a specific amount of orders.
All of my code is working fine, except when I add this part to the IF statement:
|| in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count )
The cookie is being saved properly, and when saved the number is saved as the current value of count
. The idea is that the modal will only show if:
A) their current count is in $the_digits
array & the cookie is not set.
B) their current count is in $the_digits
array & the cookie IS set, however the cookies value does not equal their current order $count
amount.
This is to prevent the modal showing after the initial display, but still allow it to show again if they hit the next $count
amount to move to the next tier.
Can't figure out what is wrong. Or perhaps there is a better way to do this. All help appreciated.
$the_digits = ['3','5','15','30','50'];
if ( in_array($count, $the_digits) && !isset( $_COOKIE[ 'tier_advance' ] ) || in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count ) {
echo'
<script>
jQuery(window).on("load",function(){
jQuery("#tier_advance").modal("show");
});
jQuery(document).on("click", "#tier_confirm", function(){
// Set a cookie
Cookies.set("tier_advance", "' . $count . '", { expires: 356 });
});
</script>
<!-- BEGIN Share Modal -->
<div class="modal fade" id="tier_advance" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h5>CONGRATULATIONS!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body text-center">
<p>You are now a <strong class="all-caps"> ' . $tier_name . ' </strong>tier customer!</p>
<p>Expect some dope rewards to start coming your way!</p>
</div>
<div class="modal-footer modal-footer-centered">
<button id="tier_confirm" type="button" class="btn btn-secondary" data-dismiss="modal">Awesome, thanks!</button>
</div>
</div>
</div>
</div>
<!-- END Share Modal -->';
}
Share
Improve this question
asked Jul 11, 2020 at 0:20
Colin GColin G
1
2 Answers
Reset to default 0ANSWER
$_COOKIE["tier_advance"] !== "$count")
Note the quotations on the variable. Cookies are saved as strings, therefore without the quotations it was ringing true.
Thanks to @mozboz for cleaning it up and making this more apparent for me.
If the problem is with that huge statement with lots of && and || then it's a good idea to simplify it. To refactor your code a bit to make it more understandable (and so you can figure it out you when you look at it in 2 years) you could do something like this:
Before:
if ( in_array($count, $the_digits) && !isset( $_COOKIE[ 'tier_advance' ] ) || in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count )
After:
$showModal = false;
if (in_array($count, $the_digits)) {
if (!isset( $_COOKIE[ 'tier_advance' ] )) {
$showModal= true;
} else {
if ($_COOKIE['tier_advance'] !== $count) {
$showModal = true;
} else {
// echo "false because tier_advance !== count
}
}
} else {
// echo "false because not in the_digits";
}
if ($showModal) {
// do the thing
}
Although this is much longer, it's much easier to understand the logic and it allows you to add some debug statements to it so you can see at what point it set to true/false to debug. I put a couple of echo's in there as examples, but obviously if you can use logging functionality that's better.
EDIT: I just noticed that you're using !==
which as you may know does a type check as well as an equality check. I think in this case the cookie might always be a string, so you should try this with just !=
instead.
本文标签: phpIF statement not workingAny suggestions
版权声明:本文标题:php - IF statement not working - Any suggestions? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742271204a2444335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论