create multiples conditions in PHP - php

My application use notification. You have 3 checkboxes to determine what types of notifications you want ("followers", "subscribers", "donators"). You can check all of them or just one or two...
Checkboxes status are sent on a php file with an ajax function in post data. (if a checkbox is checked, return 1, else 0).
In my php file I need to check my database, but before check it, I need to know if I have to check followers only, or subscribers too, or donators, or all, or two of them, etc...
Instead of creating a huge list of conditions, I'm looking for an easier solution to know what to check in dbb desparately...
$currentuser=json_decode(stripcslashes($_POST['currentuser'])); //return the name of the user
$follow_mode = json_decode(stripcslashes($_POST['follow'])); //return 1 or 0
$subscribe_mode = json_decode(stripcslashes($_POST['subscribe'])); //return 1 or 0
$donation_mode = json_decode(stripcslashes($_POST['donation'])); //return 1 or 0
$req = $bdd->prepare('SELECT * FROM users WHERE seen=:seen AND target=:tgt ORDER BY id DESC');
$req->execute(array(':seen'=>false,':tgt'=>$currentuser));
In my table of my dbb, I have "is_follower", "is_sub", "is_donator" with "1" or "0" value inside. The use of "AND" in SQL request is impossible, because if for example all the checkboxes are checked ,the notification must be follower and sub and donator at the same time.
What is the easy way to make a good conditions?

Without completely understanding why you could not use the AND statement in your SQL query, I've been able to limit it to 7 scenarios.
if($follow_mode == 1 && $subscribe_mode == 1 && $donation_mode == 1) {
$sqlquery = 1;
}
if($follow_mode == 1 && $subscribe_mode == 1 && $donation_mode == 0) {
$sqlquery = 2;
}
if($follow_mode == 1 && $subscribe_mode == 0 && $donation_mode == 1) {
$sqlquery = 3;
}
if($follow_mode == 0 && $subscribe_mode == 1 && $donation_mode == 1) {
$sqlquery = 4;
}
if($follow_mode == 0 && $subscribe_mode == 0 && $donation_mode == 1) {
$sqlquery = 5;
}
if($follow_mode == 0 && $subscribe_mode == 1 && $donation_mode == 0) {
$sqlquery = 6;
}
if($follow_mode == 1 && $subscribe_mode == 0 && $donation_mode == 0) {
$sqlquery = 7;
}
if($sqlquery == 1) {
//run query where follow_mode == 1, subscribe_mode == 1, donation_mode == 1
}
//continue until $sqlquery 7

Related

Make final decision based on four input decisions php

I am using CI framework PHP,
I want to make final decision results based on four users input decisions,
$this->input->post('decision_1', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_2', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_3', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_4', TRUE) == 0 //or 1 or 2 or 3
Now if any three inputs are same, then it will become final decision, or out of four or three inputs, maximum decisions will become final decision,
Example, if decision 1,2,3 are 0, then final should be 0
but if decision 1,2 are 0 and decision 4 is 1, then final should also be 1.
I tried with switch statement, but it did worked if all minimum 3 inputs are same, but if of three is different then i does not worked,
$result_case = TRUE;
switch ($result_case) {
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 :
$result = 1;
break;
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1 :
$result = 1;
break;
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1:
$result = 1;
break;
case $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1:
$result = 1;
break;
//
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
//
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
default:
$result = 'Undecided';
}
Is there any easy way to check input decisions and make final one?
Thanks,
//save each post in as a key of an array.
$results[$this->input->post('decision_1', TRUE)][] = 1;
...
...
...
//count each type of the post
$results = array_map(function($v){return count($v);}, $results;);
$max = -1;
foreach($results as $k => $v)
{
if($v >= 3)
$result = $k;
$max = $results[$max] > $v ? $max : $k;
}
//if a post type has more or equal to 3, chose it. otherwise chose the biggest key as the result.
$result = isset($result) ? $result : $max;

use OR in if codition to print reverse results in php

I am trying echo result "OK" if ( 1 either both ) variable true, i did so far like this
<?php
$user_id = $_SESSION['user_id'];
$point= "select points from users where id = $user_id "; // in db right now points = 2000
$flag= "select m_boost from users where id = $user_id "; // in db right now flag = 1
?>
<div class="box border">
<div class="box-title">
<?php
if($point < 1000 || $flag = 0) {
echo "not ok";
} else { ?>
echo "ok";
<?php }?>
</div>
it's working if i do like this
if(($point == '2000') || ($flag == '0') ){
but i don't want == operator for $point i want less than < $point < '999'
The problem is :
Keep getting result " Not Ok " even one variable (flag = 1) is true in db
Expected Results:
i want to print "OK" if $point > 1000 or flag == 1,
try this
when you using OR condition you should careful about condition and login. you should implement logic in if condition instead of else.
if($point > 1000 || $flag ==1) {
echo "ok";
}else {
echo 'Not ok'
}
or AS YOU WANT
if($point < 1000 ) {
echo "Not ok";
}else if($flag ==0) {
echo 'Not ok'
}esle {
echo 'ok'
}
or you can use this way
$a=false;
if($point < 1000 ) {
$a=true;
}else if($flag ==0) {
$a=true;
}esle {
$a=false;
}
// you can use this variable in your condition.
if($a) {
echo "ok";
}else {
echo 'Not ok'
}
When you are using OR, if the first condition is met the second is disregarded.
Also, make sure you use double equals (==) for comparison, not single equals (=) which means assignment.
Therefore you want to replace this:
if($point < 1000 || $flag = 0) {
With one of these:
SWAPPED AROUND
if($flag == 0 || $point < 1000) {
or
USING && INSTEAD
if($point < 1000 && $flag == 0) {
Depending on what behaviour you're looking for. It's a little unclear - so any additional clarification from you would be helpful.
Once you get something that you think is working, try to test all possible combinations so that you can be confident it works how you wish.
$flag = 0
You're setting the variable's value to 0.
Example:
$flag = 1;
if($a < $b || $flag = 0){ //$flag's value is 0 now.
...
...
}
In conditions comparisons, the right operator is "==".
if($point < 1000 || $flag == 0) {
List of comparison operators
but i don't want == operator for $point i want less than < $point <
'999'
Didn't fully understood your goals, but maybe:
For checking a value in a range the right logical operator should be "and" (&&);
if($point > 0 && $point < 999){
List of logical operators
Update:
if $point > 1000 or flag == 1
if($point > 1000 || $flag == 1){
echo "ok"
}
if (!$flag && $point < 1000)
{
echo "Not OK";
} else {
echo "OK";
}
Writing this into a truth-table:
flag point result
0 < 1000 Not OK
1 < 1000 OK
0 >=1000 OK
1 >=1000 OK
Worked for me now as per my question
i think we are all here for some contribution reason, flagging down a question is not a way, if you got solution than respond otherwise my question was 100% clear.

Condition IF in PHP

I have the code :
function insertChamado($id, $area = 2)
{
if ($area != 2 && $area != 4)
$area = 2;
How Can I adjust this code to not accept 0 condition as show in the log bellow :
[12-May-2016 16:58:28 America/Sao_Paulo] id = 36445, area = 0
[12-May-2016 16:59:00 America/Sao_Paulo] id = 14635, area = 0
[12-May-2016 17:00:02 America/Sao_Paulo] id = 18599, area = 0
Just add a conditional to check for it... Not sure what is hard about it unless we are missing something.
function insertChamado($id, $area = 2)
{
if ($area == 0) die("Ruh-Rohh");
if ($area != 2 && $area != 4)
$area = 2;
}
Or if you expect it to be 2 if it is 0:
function insertChamado($id, $area = 2)
{
if (($area != 2 && $area != 4) || $area == 0) // Though || $area == 0 actually does nothing here as 0 already meets the previous condition.
$area = 2;
}
It occurs to me after the fact, that $area could never be 0 even in your original code as 0 != 2 and 0 != 4 thus $area = 2. I suspect an implementation issue, if this does not help I suggest you edit your ques to include more code.
Could be a scope issue, as you are not using a global $area and are not returning a value, the changed $area may not be breaking out of the function.
Try one of the se implementations:
Using global
$area = 0; // for testing only
function insertChamado($id)
{
global $area;
if ($area != 2 && $area != 4)
$area = 2;
}
Or using a return:
$area = insertChamado(0,0);
function insertChamado($id, $area = 2)
{
if ($area != 2 && $area != 4)
$area = 2;
return $area;
}
The incomplete code you supplied does not help as I have no idea what the implementation of id is.
After reading carefully your question i think that your best solution is a simple switch.
function insertChamado($id, $area = 2){
switch ($area) {
case 2:
echo "area equals 2\n";
break;
case 4:
echo "area equals 4\n";
break;
default:
echo "area is always 2 other wise\n";
}
}
insertChamado('id',0); // will output "area is always 2 other wise"
insertChamado('id'); // will output "area equals 2"

php keeping track of random condition

At the moment I've 3 conditions that need to be met randomly one time on 3 pages. So if page one gets randomly the second condition, page 2 can only get randomly condition 1 or 3 and so on. For that I write to a database (I already using a database to log activities) and call it on the other pages to see which condition already has been met on previous pages.
I created for the first page a random function:<?php $rand = rand(1, 3); ?>
The second page:
if ($row['manip_1'] == 1){
$man = rand(2,3);
} elseif ($row['manip_1'] == 2){ //should do randomly 1 or 3,
$man_a = rand(1,2); //but don't know how to skip #2 in a
if($man_a == 2){ //random function, so solved it like this
$man = 3;
} else {
$man = 1;
}
} else {
$man = rand(1,2);
}
The third page:
if ($row['manip_1'] == 1 && $row['manip_2'] == 2){
$man = 3;
} elseif ($row['manip_1'] == 1 && $row['manip_2'] == 3){
$man = 2;
} elseif ($row['manip_1'] == 2 && $row['manip_2'] == 1){
$man = 3;
} elseif ($row['manip_1'] == 2 && $row['manip_2'] == 3){
$man = 1;
} elseif ($row['manip_1'] == 3 && $row['manip_2'] == 1){
$man = 2;
} else {
$man = 1;
}
The problem now is that I suddenly need to have a fourth page. Meaning that there also should be a fourth condition. That also means that the number of possibilities are now 25 on the last page. I can program it again like I did before, but I was wondering if there is not any more convenient way to program a random condition that is dependent on the conditions of previous pages. During the 4 pages each condition can only be shown once.
Define an array of the conditions, and store them in the session, e.g.
$conditions = array(1,2,3,4);
$_SESSION['conditions'] = $conditions;
Randomization is easy:
array_shuffle($_SESSION['conditions']);
Then on each of the individual pages, you just pop off one of those values from the session:
<?php
# page 1
session_start();
$random_condition = array_pop($_SESSION['conditions']);
and do so for all the other pages:
<?php
#page n
session_start();
$random_condition = array_pop($_SESSION['conditions']);

give error if number is anything other than 1 or 0

<?php
$gender = 0;
if (($gender != 0) || ($gender != 1))
{
die('error:Must select a gender.');
}
?>
This should give a error if the gender is anything other than 1 or 0. So if i gave 5 it should die. If i gave it 1 it should not die. If i give it 0 it should not die.
I was thinking about a few work arounds
<?php
$gender = 0;
if ($gender == 0)
{
//number is okay
}
else if ($gender == 1)
{
//number is okay
}
else
{
die('error:Must select a gender.');
}
?>
Well that looks sloppy and it would work or i could create a array with 0 and 1 and check if its in it or not. Kinda overkill i think.
Not sure what i'm doing wrong.
You're using the wrong boolean operator:
if (($gender != 0) || ($gender != 1)) {
}
This will be entered if gender is 0, too, because it isn't 1, and vice versa. What you need to do is:
if (($gender != 0) && ($gender != 1)) {
}
Look at this table:
gender A (gender != 0) B (gender != 1) A || B A && B
----------------------------------------------------------------
0 false true true false
1 true false true false
5 true true true true
Also note Joshua's suggestion in the comments:
if (($gender == 0) || ($gender == 1)) {
/* number is ok */
} else {
die();
}
which is a bit longer, but more readable.
if (($gender != 0) && ($gender != 1))
^^
You want to die if both tests are true.
Change the or (||) to and (&&) and it should work, as you only want to fail, when it’s both not 0 and not 1.

Categories