PHP IF statement with four conditions [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I would like to examine four conditions in an php if statement? e.g.
If ((a == 1 || 2 || 3) && (b == 1 || 2 || 3) && (c == 1 || 2 || 3) && (d == 1 || 2 || 3)) {
.... Execute query
}
This is what I am using in my code thus far:
<?php
include 'dbconnect.php';
//check if variable is set
if ( isset($_POST['beaches'], $_POST['species']) && !empty($_POST['beaches'] && !empty($_POST['species']))) {
$beachid = strtolower($_POST['beaches']);
echo '<br>';
$speciesid = strtolower($_POST['species']);
echo '<br>';
//$fishmethodid = strtolower($_POST['fish_method']);
echo '<br>';
//if ( isset($_POST['species']) && !empty($_POST['species'])) {
// echo $speciesid = $_POST['species'];
//if ( isset($_POST['fish_method']) && !empty($_POST['fish_method'])) {
// echo $fishmethodid = $_POST['fish_method'];
// die();
//query begins
if ($beachid == '1' || $beachid == '2' || $beachid == '3'|| $beachid == '4'|| $beachid == '5' || $beachid == '6' || $beachid == '7' || $beachid == '8' || $beachid == '9' || $beachid == '10' || $beachid == '11' || $beachid == '13' || $beachid == '14' || $beachid == '15' || $beachid == '16' || $beachid == '17' || $beachid == '18' || $beachid == '19' || $beachid == '20' || $beachid == '21' || $beachid == '22' && ($speciesid == '1' || $speciesid == '2' || $speciesid == '3' || $speciesid == '4' || $speciesid == '5' || $speciesid == '6' || $speciesid == '7')) {
$query = mysql_query("SELECT `beach`.`beach_description`, `species_description`, `fishmethod`.`fishmet_description`, `weight`, `price`, `date`
FROM `returnlanded`
INNER JOIN `beach` ON `returnlanded`.`beachid` = `beach`.`beach_id`
INNER JOIN `species` ON `returnlanded`.`speciesid` = `species`.`species_id`
INNER JOIN `fishmethod` ON `returnlanded`.`fishmethodid` = `fishmethod`.`fishmet_id`
WHERE `returnlanded`.`beachid`='$beachid' AND `returnlanded`.`speciesid`='$speciesid'");//" ORDER BY returnlanded.`id` ";
if (mysql_num_rows($query) > 0) {
$resp["catch"] = array();
while ($query_row = mysql_fetch_array($query)) {
$weight = $query_row['weight'];
$price = $query_row['price'];
$fishmethodid = $query_row['fishmet_description'];
$date = $query_row ['date'];
$catch = array();
$catch["$beachid"] = $query_row['beach_description'];
$catch["$speciesid"] = $query_row['species_description'];
$catch["$fishmethodid"] = $query_row['fishmet_description'];
$catch["$weight"] = $query_row['weight'];
$catch["$price"] = $query_row['price'];
$catch["$date"] = $query_row['date'];
array_push($resp["catch"], $catch);
}
//if successful
$resp["success"] = 1;
echo json_encode($resp);
echo "<table border='1'><tr><th>Date</th><th>Location</th><th>Fish Species</th><th>Fishing Method (Gear)</th><th>Weight (lbs)</th><th>Price($)</th>";
echo "<tr>";
echo "<td>".$catch["$date"]."</td>";
echo "<td>".$catch["$beachid"]."</td>";
echo "<td>".$catch["$speciesid"]."</td>";
echo "<td>".$catch["$fishmethodid"]."</td>";
echo "<td>".$catch["$weight"]."</td>";
echo "<td>".$catch["$price"]."</td>";
echo "</tr>";
echo "</table>";
echo'<br>';
} else {
$resp["success"] = 0;
$resp["display"] = "No data found";
json_encode($resp);
echo '<br>';
echo "No results found for your search";
echo '<br>';
echo '<br>';
}
}
}
include 'templates/searchagain.php';
include 'templates/footer.php';
?>
However the query only works when I use up to two conditions ($speciesid and $beachid). I did some research but I am not finding anything to accommodate the four conditions. I would like to include $fishmethodid and $date in the if-statement.
Is it possible that someone can help please?

(a == 1 || 2 || 3)
This kind of structure is incorrect, because it will evaluate 1 || 2 || 3 (Which is true, because at least one... well, all of the operands are truthy).
Instead, try this:
in_array($a,range(1,3))

WOW! Glad you aren't checking between 1 and 100. How about:
if ($beachid >= 1 && $beachid <= 22 && $speciesid >= 1 && $speciesid <= 7) {
This might read better:
if ( ($beachid >= 1 && $beachid <= 22) && ($speciesid >= 1 && $speciesid <= 7) ) {
Or you can simplify somewhat:
if ( ($beachid > 0 && $beachid < 23) && ($speciesid > 0 && $speciesid < 8) ) {

Related

Compare dates in the format 1 year 2 months 3 weeks 4 days in PHP

In the project I am working, I get an age like
1Year 2Months 3Weeks 4Days
I am trying to do a comparison like
If age is less than 8 weeks, then it is a baby
If age less than or equal to 12 months/1 year then it is young
If greater than 1 year, it is adult. The code which I try is given below. It is confusing.
Is there a way to simplify this using some in built functions?
function getAgeGroup($age_years, $age_months, $age_weeks, $age_days) {
$age_group = 'baby';
if (!$age_years && !$age_months && (!$age_weeks || ($age_weeks <= 8 && (!$age_days || $age_days < 1)))) {
$age_group = 'baby';
}
elseif (!$age_years && ($age_months == 1 || $age_months == 2) && (!$age_weeks && !$age_days)) {
$age_group = 'baby';
}
elseif (!$age_years && $age_months == 1 && $age_weeks < 4 && $age_days) {
$age_group = 'young';
}
elseif (!$age_years && $age_months == 1 && $age_weeks > 4) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months >= 2 && $age_months < 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 0 && $age_months <= 12) && ($age_weeks >= 8 && $age_days > 1)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 2 && $age_months <= 12) && (!$age_weeks && !$age_days)) {
$age_group = 'young';
}
elseif ($age_years == 1 && !$age_months && !$age_weeks && !$age_days) {
$age_group = 'young';
}
elseif (($age_years > 0 || $age_months >= 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'adult';
}
elseif (($age_years > 0 || $age_months > 0)) {
$age_group = 'adult';
}
elseif ($age_years > 1) {
$age_group = 'adult';
}
return $age_group;
}
you can try to something like this: eval.in to get use of the DateTime and DateInterval classes.

PHP count some rows

I have a problem, I want to create a function which counts the data from the Database. But I want that from the column to count some ID's and not all rows.
My code that doesn't work:
function countMember() {
include("./config/config.php");
$fetch_countMember = $fetch_countMember->prepare("SELECT * FROM `accounts`");
$fetch_countMember->execute();
while($fetch_countMember= $fetch_countMember->fetch()) {
if($fetch_countMember["member"] == 5 || $fetch_countMember["member"] == 6 || $fetch_countMember["member"] == 12 || $fetch_countMember["member"] == 13 || $fetch_countMember["member"] == 14 || $fetch_countMember["member"] == 15 || $fetch_countMember["member"] == 16 || $fetch_countMember["member"] == 17 || $fetch_countMember["member"] == 20 || $fetch_countMember["member"] == 21)
$c_member++;
}
return $c_member;
}
You need to use PDO::exec()
elegant way to do it:
$pdo = new PDO(....);
$ids = [5,6,12,13,14,15,16,17,20,21];
$query = sprintf("SELECT * FROM accounts WHERE member IN (%s)", implode($ids, ','));
$count = $pdo->exec();
echo $count;

php search within string for word?

I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.
Why aren't the last two cases of my if statement ever matched?
$atype = strtolower($userData['user_type']); // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" ||
(preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
$atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||
(preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
$atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" ||
(preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
$atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" ||
(preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
$atype = 4;
}
echo $atype;
You need to explain your question in a better way.
But i guess as you say the data assumed is company introducer.
So it already matches condition for the first if block.
For ex:
In company introducer
The preg_match will return true.
if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ){
$atype =1 ;
}

How to do decompose conditional refactoring?

I've this situation in code where i think the code is unnecessary complex and i believe i can refactor it to make it more easier to understand and read.
So i googled about it and found decompose conditional refactoring, but i'm still in doubt how to do refactoring
if(count($bagTypes) == 1 && (array_key_exists('type1', $bagTypes)
|| array_key_exists('type2', $bagTypes)
|| array_key_exists('type3', $bagTypes))){
$flag = 1;
}
if(count($bagTypes) == 2 && (
(array_key_exists('type1', $bagTypes) && array_key_exists('type2', $bagTypes)) ||
(array_key_exists('type1', $bagTypes) && array_key_exists('type3', $bagTypes)) ||
(array_key_exists('type2', $bagTypes) && array_key_exists('type3', $bagTypes)))
){
$flag = 1;
}
Is there any better way of doing this?
You could try something like this:
$arrayKeys = array(
'type1',
'type2',
'type3'
);
$bagTypesKeys = array_keys($bagTypes);
if ((count($bagTypes) == 1 && count(array_diff($arrayKeys, $bagTypesKeys)) < 3)
|| (count($bagTypes) == 2 && count(array_diff($arrayKeys, $bagTypesKeys)) < 2))
{
$flag = 1;
}

Calculate Poker Hand

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';
}

Categories