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;
Related
I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..
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
My charity does "Poker Runs" motorcycle runs where player go to each stop and pick a card. We are looking to make this an easier way to track the winners without having to manually sort through each card.
I believe I have all other functions done, I just am unsure how to check for the full house with the method I am using. And also how to score just for a high card hand.
<?php
$card_one_suit = $_POST['card_one_suit'];
$card_two_suit = $_POST['card_two_suit'];
$card_three_suit = $_POST['card_three_suit'];
$card_four_suit = $_POST['card_four_suit'];
$card_five_suit = $_POST['card_five_suit'];
$card_one = $_POST['card_one'];
$card_two = $_POST['card_two'];
$card_three = $_POST['card_three'];
$card_four = $_POST['card_four'];
$card_five = $_POST['card_five'];
$player = $_POST['name'];
$total_card_amount = $card_one + $card_two + $card_three + $card_four + $card_five;
$card_list = array();
array_push($card_list, $card_one, $card_two, $card_three, $card_four, $card_five);
$card_suits = array();
array_push($card_suits, $card_one_suit, $card_two_suit, $card_three_suit, $card_four_suit, $card_five_suit);
$rank = 0;
foreach($card_list as $card)
{
$count_values[$card]++;
}
foreach($card_suits as $cards)
{
$count_suit_values[$cards]++;
}
//echo "$count_suit_values[$card_one_suit]";
//print_r($count_suit_values);
// ROYAL FLUSH
if(($total_card_amount == "60") && ($count_suit_values[$card_one_suit] == 5))
{
$rank = 1;
echo "ROYAL FLUSH";
}
// STRAIGHT FLUSH
else if (($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40") &&
($count_suit_values[$card_one_suit] == 5))
{
$rank = 2;
echo "STRAIGHT FLUSH";
}
// FOUR OF A KIND
else if ($count_values[$card_one] == 4 || $count_values[$card_two] == 4 || $count_values[$card_three] == 4)
{
$rank = 3;
echo "FOUR OF A KIND";
}
// FULL HOUSE
// HOW TO FIGURE THIS OUT?
// FLUSH
else if ($count_suit_values[$card_one_suit] == 5 || $count_suit_values[$card_two_suit] == 5 || $count_suit_values[$card_three_suit] == 5)
{
$rank = 5;
echo "FLUSH";
}
// STRAIGHT
else if ($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40")
{
$rank = 6;
echo "STRAIGHT";
}
// THREE OF A KIND
else if ($count_values[$card_one] == 3 || $count_values[$card_two] == 3 || $count_values[$card_three] == 3 || $count_values[$card_four] == 3)
{
$rank = 7;
echo "THREE OF A KIND";
}
// TWO PAIR
else if ($count_values[$card_one] == 2 && $count_values[$card_two] == 2 || $count_values[$card_one] == 2 && $count_values[$card_three] == 2
|| $count_values[$card_one] == 2 && $count_values[$card_five] == 2 || $count_values[$card_two] == 2 && $count_values[$card_three] == 2)
{
$rank = 8;
echo "TWO PAIR";
}
// ONE PAIR
else if ($count_values[$card_one] == 2 || $count_values[$card_two] == 2 || $count_values[$card_three] == 2 || $count_values[$card_four] == 2)
{
$rank = 9;
echo "ONE PAIR";
}
// HIGH CARD
else
{
$rank = 10;
echo "NO MATCHES. DETERMINE HIGH CARD";
}
?>
For the hand to be a full house, the array count_values must contain two entries, one of those entries must have a value of 3.
else if (count($count_values) == 2 && (array_values($count_values)[0] == 3 || array_values($count_values)[1] == 3)) {
echo 'FULL HOUSE';
}
Here is my code.
$value = 0;
switch($value) {
case ( $value <= 25 ):
$CompScore = 'low';
break;
case ($value > 25 && $value <= 50 ):
$CompScore = 'fair';
break;
case ($value > 50 && $value <= 75 ):
$CompScore = 'good';
break;
case ($value >75 ):
$CompScore = 'excellent';
break;
default:
$CompScore = 'low';
break;
}
echo $CompScore;
When the value is 0, $compScore is fair. Why it is not showing low? I don't understand why.
switch not working like that.
Since $value is 0 which is falsy value.
$value <= 25 is true, $value > 25 && $value <= 50 is false, so $CompScore will be 'fair'.
For you code, use an if elseif else flow will be more readable.
You could rewrite your code like below:
// from excellent to low
if ($value > 75) {
$CompScore = 'excellent';
} else if ($value > 50) {
$CompScore = 'good';
} else if ($value > 25) {
$CompScore = 'fair';
} else {
$CompScore = 'low';
}
The problem is, you use your switch in a particular way.
You are saying :
$value = 0;
Switch ($value){
case ($value < 25):
....
}
This finally compares $value<25 and 0 as described below :
($value<25) == $value.
=> true == 0
Which is wrong because true != 0
A way to do what you want this way is simply to replace switch($value) with switch(true) so the interpreter will actually compare check if your case statements are true.
The value you pass into a switch statement is basically what the switch statement looks for an evaluated match for, going from top to bottom down the list of cases until it finds one it is equal to (loose comparison), e.g. true == true.
In your example, your comparisons are evaluated as booleans (true or false) - your variable $value is set to zero, which is equal to false, but not identical (strict comparison) to false. For example:
(0 == false) // true
(0 === false) // false
(1 == false) // false
(1 === false) // false
(1 == true) // true
(1 === true) // false
(true === true) // true
So by using a boolean true as your switch value, you can do this to have numeric comparison inside the statement, where each comparison will evaluate to either true or false to match/not match the original true value (boolean - boolean comparison).
switch(true) {
case ($value <= 25): // true
$CompScore = 'low';
break;
case ($value > 25 && $value <= 50 ): // false
$CompScore = 'fair';
break;
case ($value > 50 && $value <= 75 ): // false
$CompScore = 'good';
break;
case ($value >75 ): // false
$CompScore = 'excellent';
break;
default: // if you removed the first case
$CompScore = 'low'; // this default case would be used
break;
}
You can "smarten up" your code with a lookup array and some simple mathematics.
Code: (Demo)
$lookup = [
1 => 'low',
2 => 'fair',
3 => 'good',
4 => 'excellent'
];
for ($i = -1; $i <= 101; ++$i) {
echo "\n$i : " , $lookup[ceil($i / 25)] ?? $lookup[1];
}
By dividing the value by 25, then rounding up to the next whole number (with ceil()), you achieve the same result without a battery of condition statements. Using a lookup away not only keeps your code compact, it ensures that you aren't performing n checks on the same value AND it provides a clean separation between the process and the values.
If you ever decide to split the groups by 20, instead of 25, you would only need to change the 25 to 20, then add a fifth lookup value in the desired location (with the appropriate key).
I am stuck on some php coding, that seemed initially easy to accomplish. Here is what i would like to do :
<?php
$amountOfDigits = 1;
$numbers = range(1,3);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
while ( have_posts() ) : the_post();
static $count = 0;
if ($digits == '1') {
//Do this if statement
if ($count == "2") }
elseif ($digits == '2') {
//Do this if statement
if ($count == "2" || $count == "3") }
elseif ($digits == '3') {
//Do this if statement
if ($count == "2" || $count == "3" || $count == "4") }
{ //here the rest of the code
?>
So depending on the $digits variable, the if statement is formatted to be used on the line above //the rest of the code
How to put this in PHP properly?
Thanks.
Robbert
If I understand your question properly, you want something like this:
if ($digits == '1')
$mycond = ($count == "2");
elseif ($digits == '2')
$mycond = ($count == "2" || $count == "3")
elseif ($digits == '3')
$mycond = ($count == "2" || $count == "3" || $count == "4")
then you can further use
if($mycond){
// blahblahblah
}
Well if you want the same execution block on each case, there is a very simple solution for that.
You should simply use a function that check status of "count" depending on "digits".
<?php
function checkCountAgainstDigits($count, $digits){
switch($digits){
case 1:
return $count === 1;
case 2:
return $count === 2 || $count === 3;
case 3:
return $count === 2 || $count === 3 || $count === 4;
default:
// The default case "ELSE"
return FALSE;
}
}
if(checkCountAgainstDigits($count, $digits)){
// do
}
?>
If you want a different one, your solution is correct.