Okay, I have a php code for my mail.php, so that if the current user id doesn't match the to or from id, it will show an error message, but I tested it out and any use can view the message, here's the code :
if (!$mail['to_id'] == $account['id'] && !$mail['from_id'] == $account['id']) {
$system->message(L_ERROR, L_MAIL_ERROR_PERMISSION, './?area=forum&s=mail', L_CONTINUE);
}
But when I added != instead of ==, it shows an error message for everyone, including the people who sent the message. Does anyone know how I can fix this problems?
Use this code instead.
if ($mail['to_id'] != $account['id'] && $mail['from_id'] != $account['id']) {
$system->message(L_ERROR, L_MAIL_ERROR_PERMISSION, './?area=forum&s=mail', L_CONTINUE);
}
Your logic is:
!$mail['to_id'] == $account['id']
&&
!$mail['from_id'] == $account['id']
The problem stems from doing this:
!$mail['to_id']
and
!$mail['from_id']
The inverse operator (!) flips the value of what it's being applied to. In this case, you're flipping the value of $mail ID's. That's probably not going to do what you're thinking since those ID's, I'm assuming, are integers.
It might help to think of this in the positive and then reverse the whole thing.
The positive is:
$mail['to_id'] == $account['id']
&&
$mail['from_id'] == $account['id']
The inverse of it is wrapping the whole thing in a "!" (note the parenthesis):
!(
$mail['to_id'] == $account['id']
&&
$mail['from_id'] == $account['id']
)
We can use the rules of Algebra to convert that statement by multiplying "!" against each element (including the AND operator).
!($mail['to_id'] == $account['id']) // Note you apply the inverse of to the whole expression
!&& // this isn't an actual code but for demonstration purposes
!($mail['from_id'] == $account['id']) // Note you apply the inverse of to the whole expression
Which simplifies into:
$mail['to_id'] != $account['id'] // Not Equal is inverse of equal
|| // the inverse of && (AND) is || (OR)
$mail['from_id'] != $account['id'] // Not Equal is inverse of equal
Which states:
"If the Mail_To OR Mail_From ID doesn't match the Account ID, execute the following code"
So, at the end of the day you should be able to use:
if( $mail['to_id'] != $account['id'] || $mail['from_id'] != $account['id'] )
{
// do something
}
or
if( !( $mail['to_id'] == $account['id'] && $mail['from_id'] == $account['id']) )
{
// do something
}
Both say the same thing.
I hope that helps!
Related
I'm having trouble trying to make the statement below work the way I want it to.
I am trying to display an error message for an order form if at least one text field is not filled out. Below is a snippet of my PHP code. The 'cookieGiftBox' is the name of a checkbox that users can select and if it is selected, they must enter an amount of cookies under their desired flavors in the text fields provided. How can I display an error message to display when the checkbox IS selected but NO text fields have been filled out?
<?php
if (isset($_POST['cookieGiftBox'])
&& (!isset($_POST['sugar'])
|| ($_POST['chocolateChip'])
|| ($_POST['chocolateChipPecan'])
|| ($_POST['whiteChocolateRaspberry'])
|| ($_POST['peanutChocolateChip'])
|| ($_POST['peanutButter'])
|| ($_POST['tripleChocolateChip'])
|| ($_POST['whiteChocolateChip'])
|| ($_POST['oatmealRaisin'])
|| ($_POST['cinnamonSpice'])
|| ($_POST['candyChocolateChip'])
|| ($_POST['butterscotchToffee'])
|| ($_POST['snickerdoodle']))) {
$error.="<br />Please enter an Amount for Cookie Flavors";
}
?>
&& takes precedence over ||, thus you would have to use parentheses to get the expected result:
if (isset($_POST['cookieGiftBox']) && (!isset($POST['sugar']) || ...)
Actually, to get check if nothing is selected, you would do something like this:
if checkbox is checked and not (sugar is checked or chocolatechip is checked) or the equivalent:
if checkbox is checked and sugar is not entered and chocolatechip is not entered....
If you want to know more, search for information about Boolean algebra.
Update: In your example and in the correct syntax the sentence I took as an example would like this (for the first sentence, note the not (!) and the parentheses around the fields, not the checkbox):
if (isset($_POST['cookieGiftBox']) &&
!(
isset($_POST['sugar']) ||
isset($_POST['chocolatechip'])
)
) { error ...}
Or the second sentence, which might be easier to understand (note the && instead of ||):
if (isset($_POST['cookieGiftBox']) &&
!isset($_POST['sugar']) &&
!isset($_POST['chocolatechip'])
) { error...}
Using ands makes sure it is only true (and thus showing the error) if none of the sugar, chocolatechips, etc. fields are set when the giftbox checkbox is checked.
So if the checkbox is checked, and no fields are set it looks like this:
(true && !false && !false) which is equivalent to (true && true && true) which is true, so it shows the error.
If the checkbox is checked and sugar is entered it would look like this:
(true && !true && !false), equ. to (true && false && true), equ. to false and thus no error is shown.
If both sugar and chocolatechip are entered also no error will be shown.
I have simillar issue:
I try use:
if (isset($this->context->controller->php_self) && ($this->context->controller->php_self != 'category') || ($this->context->controller->php_self != 'product') || ($this->context->controller->php_self != 'index')){ ... }
Thats didn't work, I change it to:
if (isset($this->context->controller->php_self) && ($this->context->controller->php_self != 'category') && ($this->context->controller->php_self != 'product') && ($this->context->controller->php_self != 'index')){ ... }
I dont know it is correct?
Edit. This is not correct, but for now i didnt know how to fix it.
I'm trying really hard to figure out how to solve this problem. Please be gentle, i'm still learning!
There are four workplaces named: B.Avf, R.Avf, Office and Production. Products is passing theese workplaces. Now i'm building a script that can alert if the same product is passing the same workplace twice. Now, this is the hard part: If the product has passed "B.Avf" it can't pass "R.Avf" whitout an alert, same if the product has passed "R.Avf" it can't pass "B.Avf" whitout and alert. But if the product has passed for ex. "R.Avf" it´s ok to pass "Office" whitout an alert.
This is what i got so far: (It's like example number 5 =)
Maby should i create nested ifstatements instead?
This code will turn true all times!
PHP
if($_SESSION['user']['usr_workplace'] == "R.Avf" ||
$_SESSION['user']['usr_workplace'] == "B.Avf" &&
strpos($history, "R.Avf") !== FALSE ||
strpos($history, "B.Avf") !== FALSE)
Your if condition should be :
//IF this session user workplace is "B.Avf" or "R.Avf"
//AND "B.Avf" or "R.Avf" excist in history, then alert!
if(($_SESSION['user']['usr_workplace'] == "R.Avf" || $_SESSION['user']['usr_workplace'] == "B.Avf") && (strpos($history, "R.Avf") !== FALSE || strpos($history, "B.Avf") !== FALSE))
Your if statement can be made a lot simpler by removing one condition and still getting the desired result like so:
if(
($_SESSION['user']['usr_workplace'] == "R.Avf" || $_SESSION['user']['usr_workplace'] == "B.Avf")
&&
strpos($history, $_SESSION['user']['usr_workplace']) !== false
)
Notice how you don't need two strpos checks, as the first part of the if statement will only allow the second part to run if it was true.
It would be better to extract this to a method or simplify it further, but for what you've asked this will do :).
This works, it has the right braces:
if($_SESSION['user']['usr_workplace'] == "R.Avf" || ($_SESSION['user']['usr_workplace'] == "B.Avf" && (strpos($history, "R.Avf") !== FALSE || strpos($history, "B.Avf") !== FALSE)))
It checks for r.Avf first, OR and then checks all the conditions within the braces
you should break this if into 2 methods.
one should be called:
isUserSessionRBAvf($_SESSION['user']['usr_workplace'])
the other should check the strpos pos:
isHistoryAvf($history, "R.Avf")
and you would end up with just:
if(isUserSessionRBAvf(...) && isHistoryAvf(...))
this way you have a more readable code and easier to debug.
P.S. consider a different method naming
what you need to do is simply check with session value to $history so you will avoid checking two strpos() where you are doing static string checking:
here you are checking with static string:
strpos($history, "R.Avf") !== FALSE ||
strpos($history, "B.Avf") !== FALSE)
make it dynamic like this:
if($_SESSION['user']['usr_workplace'] == "R.Avf" || $_SESSION['user']['usr_workplace'] == "B.Avf" && strpos($history, $_SESSION['user']['usr_workplace']) !== FALSE )
I have php if statement that should display certain HTML code if two conditions are true or another two are true or third part of conditions are true.
I have several arrays - $Options_arr, $MoreOptions_arr, $Special_arr .
To explain in the easiest possible way I want to do this:
if(!empty($Options_arr[0]) && $Options_arr[0]!="") or
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") or
(!empty($Special_arr[0]) && $Special_arr[0]!="")
{?> some HTML here
All help will be appreciated thank you.
empty() already checks for empty string "" so it's shorter:
if(!empty($Options_arr[0]) || !empty($MoreOptions_arr[0]) || !empty($Special_arr[0])) {
//some HTML here
}
BragG, you can use elseif
Like:
if((!empty($Options_arr[0]) && $Options_arr[0]!="") ||
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") ||
(!empty($Special_arr[0]) && $Special_arr[0]!=""))
{
// some html or any code
}
I hope that is what you were looking for..
Feel free to ask any question.
You are just missing some brackets. Also || is more frequently used than OR
if((!empty($Options_arr[0]) && $Options_arr[0]!="") || (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") || (!empty($Special_arr[0]) && $Special_arr[0]!="")){
echo '<p>hello</p>';
}
You're basically already there...
if (
(!empty($Options_arr[0]) && $Options_arr[0]!="")
|| (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="")
|| (!empty($Special_arr[0]) && $Special_arr[0]!="")
){
...do something
Basically you write an if statement that resolves if any of the sub-statements are true by joining the sub-statements together with ORs
Any ideas why this isn't working?
if($page->Slug != 'water-filters' || $page->Slug != 'pet-care' || $page->Slug != 'books') {
//do something
}
I think you mean AND instead of OR because you're using not equals.
By using not equals in the way you are the statement will always be true, if $page->Slug equals 'water-filters' it doesn't equal 'pet-care' and hence the if statement as a whole returns true.
if($page->Slug != 'water-filters' && $page->Slug != 'pet-care' && $page->Slug != 'books')
{
//do something
}
I'm guessing that "do something" is always getting executed?
if($page->Slug != 'water-filters' || $page->Slug != 'pet-care' || $page->Slug != 'books')
{
//do something
}
For any value of $page->Slug, it will always be not equal to ONE of those three conditions, therefore at least one (technically, at least two) of the statements will always be true. Since you're using an 'OR' as long as one of the three statements is true, the whole thing will be true.
Therefore, this is essentially saying
if (true) {
//do something
}
$page->Slug is either 'water-filters' or 'pet-care' or 'books'
Try
== or == or ==
or
!= and != and !=
:-D
If the Slug is not "water-filters" or is not "pet-care" or is not "books"...
Well, if it's one of those, or any other value, it's by definition not the other two (or not all three). So this condition is always true.
Aside from what the others have said above, which are correct also, try this syntax for readability.
if(!in_array($page->Slug, array('water-filters', 'pet-care', 'books')) {
// Do something
}
My site requires a login that establishes permissions based on the user. In the photo listing of the site, I must determine if a particular photo will be displayed if the user has guest access only. So I thought that this if else statement would work:
if (!($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST')) {
// show the photo if it isn't private and the user isn't a guest
But it doesn't.
However, if I separate this test into three lines then it works just fine.
$is_private_photo = $mysql_row['guest_access'] == 'NO';
$is_guest = $_SESSION['user_level'] == 'GUEST';
$both_private_and_guest = ($is_private_photo AND $is_guest);
if (!$both_private_and_guest) {
// show the photo if it isn't private and the user isn't a guest
What is wrong with the first version?
Your first if is
if (!($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST'))
Which is actually interpreted like this :
if (
(!($mysql_row['guest_access'] == 'NO'))
AND ($_SESSION['user_level'] == 'GUEST')
)
The ! is only applied to the first condition, not both, because it has a higher level of priority than AND (see Operator Precedence)
Your condition should probably be re-written with some additional parentheses :
if (!(($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST')))
So the ! is applied to the whole condition.
The logical not operation needs to be applied to the AND results, but in your code sample it only applied to the first clause (NOT has higher precedence than AND). You can solve it by grouping both conditions:
if (!(($mysql_row['guest_access'] == 'NO' AND ($_SESSION['user_level'] == 'GUEST'))) {
The not operator (!) only covers the first condition so either add another bracket, or do this
if ( $mysql_row['guest_access'] != 'NO' AND...
It's because you put the parentheses in the wrong place in your first version. The equivalent form of the second version is
if (!$is_private_photo AND $is_guest)
which is clearly not what you intended.
Make it like this:
if (($mysql_row['guest_access'] != 'NO') AND ($_SESSION['user_level'] != 'GUEST')) {
Your '!' is not being applied to the 'AND' statement...it is only being applied to the ($mysql_row['guest_access'] == 'NO') statement.
You are only negating the first condition. Try this:
if (!($mysql_row['guest_access'] == 'NO' AND $_SESSION['user_level'] == 'GUEST')) {
Your negation operator does not apply as expected. You should use something like:
if ( ! (($mysql_row['guest_access'] == 'NO') AND ($_SESSION['user_level'] == 'GUEST')) )
if ( ! ($mysql_row['guest_access'] == 'NO') OR ! ($_SESSION['user_level'] == 'GUEST') )