Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having a little problem with validation. I have here this code snippet that works by this way: if the pizza_size variable is not set in the Query String and the ID of product category is 1 (corresponds to pizzas) or this is set but the pizza_size value is not equal to small, medium or big this needs to return to the page products.php with the message that the size is not selected. I'm trying with:
if(!isset($_GET['pizza_size']) || $_GET['pizza_size'] != "small" || $_GET['pizza_size'] != "medium" || $_GET['pizza_size'] != "big" && $category_id == 1)
{
redirect_to('products.php?message=no-size-informed');
}
else //Add the product
The problem is that this is not working. Even if the pizza_size corresponds to small, medium or big I receive the error message. But if I use only one comparison, this works great, like:
if(!isset($_GET['pizza_size']) || $_GET['pizza_size'] != "small" && $category_id == 1)
{
redirect_to('products.php?message=no-size-informed');
}
But I need to validate with the 3 sizes. How to do this validation with all the three possible variables? Thanks!
if(($category_id == 1)
&& !(isset($_GET['pizza_size']) && in_array($_GET['pizza_size'], array('small', 'medium', 'big')))
) {
redirect_to('products.php?mesage=no-size-informed');
}
else {
// Add the product
}
You have to do it like this:
if(($category_id == 1)
&& (isset($_GET['pizza_size'] && !in_array($_GET['pizza_size'], array('small', 'medium', 'big')) || !isset($_GET['pizza_size']
) {
redirect_to('products.php?mesage=no-size-informed');
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Take these levels:
level1 = 0days
level2 = 30days
level3 = 90days
All these levels are totally dynamic. Admin can alter number of levels and duration of levels at any time.
I am recording numberOfDailyLogin in a series, so if user not login even for 1 day meter reset to 0.
I am trying to give user a special level tag on basis of numberOfDailyLogin
I have a solution already by running a while loop from 0 to numberOfDailyLogin and update variable once >= daysRequired for login.
But I don't want it this way, want something easy
I am totally clueless at this point, I can't hardcode any of the
value, so please if you are interest please give some suggestion.
#Edit1
// json response for levels
{
"status": 200,
"body": [
{
"name": "level1",
"after_days": 0,
},
{
"name": "level2",
"after_days": 50,
},
{
"name": "level3",
"after_days": 180,
}
]
}
function getLevel($allLevels, $numberOfDailyLogins) {
foreach ($allLevels as $l) {
if ($numberOfDailyLogins > $l['after_days']) {
return $l['name'];
}
}
}
Problem is if numberOfDailyLogins=51.. so it should show level2... cause after 50 days userLevel reached to level2.. but my code return level1 which is totally wrong.
#brombeer suggestion works for me, Thanks mate
function getLevel($allLevels, $numberOfDailyLogins) {
$level = null;
foreach ($allLevels as $l) {
if ($numberOfDailyLogins> $l['after_days']) {
$level = $l;
}
}
// TO-DO... do whatever you want
}
It's a simple logic error - your code returns after the first iteration because the value is already greater than 0 (the first level in the list).
One simple solution is to search the array in reverse instead:
function getLevel($allLevels, $numberOfDailyLogins) {
for ($i = count($allLevels) -1; $i >= 0; $i--) {
if ($numberOfDailyLogins > $allLevels[$i]['after_days']) {
return $allLevels[$i]['name'];
}
}
}
Demo: http://sandbox.onlinephpfunctions.com/code/3b00c6d182676411915cae1fbb21a1afba2548e8
I would like to set the disabled state of a form field based on the combination of 4 variables: processed, process started, process ended, user id
If it is not going to be processed, the form field should be disabled
If the process has started OR ended, it should be also disabled, except if the user id == 1. So User 1 can still fill the form field, even if the process has started OR ended. And it should be also disabled for User 1 also if it is not going to be processed.
I was trying this way, but doesn't work as I expect, so there must be a flaw in my logic or understanding how PHP works:
'disabled' => !$proc || (($proc_started || $proc_ended) && !$user_id == 1)
This way other users see the form field also enabled, which I don't want. Is it the hierarchy of the Logical Operators ?
Can you please point me to the right direction? Thanks.
!$user_id == 1 is (!$user_id) == 1
$foo = 42;
!$foo == false;
You want to write !($user_id == 1) or $user_id != 1
Should work.
if($user_id === 1) {
if($state != "processed") {
$state = "Enabled" // or anything else of your choice
}
} else {
$state = "Disabled";
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to condition if one of these has null value, then else but i am getting error:
ErrorException (E_NOTICE)
Undefined variable: average_winning_trade
DD(winning_trades_profit) result : []
here is the code for condition:
if(!empty($losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades)) {
$average_winning_trade = round(array_sum($winning_trades_profit) / $profitable_trades);
$average_losing_trade = array_sum($losing_trades_loss) / $losing_trades;
$payoff_ratio_per_trade = abs(round($average_winning_trade / $average_losing_trade, 2));
}else { $payoff_ratio_per_trade ="0";}
Your problem is in your if condition:
if(!empty($losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades))
because $losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades is a boolean expression, it will always return true or false, and so empty() will always return false, so regardless of the state of those variables, the first part of your if statement will execute.
I think what you actually want is
if (isset($losing_trades_loss, $profitable_trades, $winning_trades_profit, $losing_trades))
or possibly
if (isset($losing_trades, $profitable_trades) && count($winning_trades_profit) && count($losing_trades_loss))
without seeing the rest of your code it's difficult to say which of the above would be correct.
Edit
You probably also need to change your assignments to check for 0 divisors:
$average_winning_trade = $profitable_trades ? round(array_sum($winning_trades_profit) / $profitable_trades) : 0;
$average_losing_trade = $losing_trades ? array_sum($losing_trades_loss) / $losing_trades : 0;
$payoff_ratio_per_trade = $average_losing_trade ? abs(round($average_winning_trade / $average_losing_trade, 2)) : 1;
As your error code shows
ErrorException (E_NOTICE) Undefined variable: average_winning_trade
Meaning you are trying to call the variable somewhere else in your code while it hasn't been set yet. This could be either before or after you reach the if-statement you put here. However, this if-statement is not causing the error to appear.
UPDATE
Your current if-statement is as follow:
IF NOT EMPTY $losing_trades_loss AND $profitable_trades AND $Winning_trades
but you clearly want
IF NOT EMPTY $losing_trades_loss OR $poritable_trades OR $winning_trades
so update your if-statement as follow:
if(!empty(
$losing_trades_loss ||
$profitable_trades ||
$winning_trades_profit ||
$losing_trades)) {
$average_winning_trade = round(array_sum($winning_trades_profit) / $profitable_trades);
$average_losing_trade = array_sum($losing_trades_loss) / $losing_trades;
$payoff_ratio_per_trade = abs(round($average_winning_trade / $average_losing_trade, 2));
}else { $payoff_ratio_per_trade ="0";}
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I have a custom field in a WP post called "profession." If this profession is RN, then I want one piece of text displayed. If the profession is not RN, then I want another piece of text displayed.
I added the following code to execute this function:
<?php if ( $profession = 'RN' ) {
echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>
The problem is, the first echo statement is displaying even when the post does not have RN in the profession field. If I purposely break the if variable, then it defaults to the second echo statement across the board. I can't get the post to respond dynamically to one vs the other.
Am I missing something in this code that is causing one or the other to display for all instead of based on the parameter I am trying to set?
You need to compare in your if not assign.
= is used to assign values
== is used for comparison, checks if value is equal
=== is used for strict comparison, checks if value and type are equal.
So your php if statement should look like this
if ( $profession == 'RN' ){
code
}else{
code
}
Please read how to use condition before writing.
http://php.net/manual/en/control-structures.if.php
<?php if ( $profession == 'RN' ) {
echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>
<?php if ( $profession == 'RN' ) {
echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li> <li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>
I'm having a small problem. In a class called reservation that has an attribute called reserve , which in the database is a tinyint(4), and an attribute kamp, which is int(10). I'm trying to do this:
if ($this->kamp == 387 || $this->kamp == 388 || $this->kamp == 389) {
$this->reserve = 0;
} else {
$this->reserve = 1;
}
Now my problem is, the code ALWAYS jumps straight to the else bracket. Even when I'm 100% sure $this->kamp is 387, 388 or 389.
Does this have anything to do with datatypes or am I missing something? I think the problem lies within this piece of code, since in my database there are objects showing up where reserve = 1 and the kamp is one of the three numbers I mentioned.
Thanks!
i think this will work for you.
$val = intval($this->kamp);
and then print or echo for result it will giving you value or not ?
let me know if i can help you more.