PHP count some rows - php

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;

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

PHP IF statement with four conditions [closed]

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) ) {

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

I need a shorter way to compare variables?

Okay I have 15 variables $A - $O. I need a different way to compare the variables as I think I am doing this a really long way. It works the way I want it to but I'm not happy with the amount of code.
So what my code is doing it's generating a random number for each of the variables. I have it doing this while they equal each other, so in other words it will stop generating random number when each of the variables is different here is my code... Thanks in advance.
do{
$a = rand($min,$max);
$b = rand($min,$max);
$c = rand($min,$max);
$d = rand($min,$max);
$e = rand($min,$max);
$f = rand($min,$max);
$g = rand($min,$max);
$h = rand($min,$max);
$i = rand($min,$max);
$j = rand($min,$max);
$k = rand($min,$max);
$l = rand($min,$max);
$m = rand($min,$max);
$n = rand($min,$max);
$o = rand($min,$max);
}
while ($a==$b || $a==$c || $a==$d || $a==$e || $a==$f || $a==$g || $a==$h || $a==$i || $a==$j || $a==$k || $a==$l || $a==$m || $a==$n || $a==$o
|| $b==$a || $b==$c || $b==$d || $b==$e || $b==$f || $b==$g || $b==$h || $b==$i || $b==$j || $b==$k || $b==$l || $b==$m || $b==$n || $b==$o
|| $c==$a || $c==$b || $c==$d || $c==$e || $c==$f || $c==$g || $c==$h || $c==$i || $c==$j || $c==$k || $c==$l || $c==$m || $c==$n || $c==$o
|| $d==$a || $d==$b || $d==$c || $d==$e || $d==$f || $d==$g || $d==$h || $d==$i || $d==$j || $d==$k || $d==$l || $d==$m || $d==$n || $d==$o
|| $e==$a || $e==$b || $e==$c || $e==$d || $e==$f || $e==$g || $e==$h || $e==$i || $e==$j || $e==$k || $e==$l || $e==$m || $e==$n || $e==$o
|| $f==$a || $f==$b || $f==$c || $f==$d || $f==$e || $f==$g || $f==$h || $f==$i || $f==$j || $f==$k || $f==$l || $f==$m || $f==$n || $f==$o
|| $g==$a || $g==$b || $g==$c || $g==$d || $g==$e || $g==$f || $g==$h || $g==$i || $g==$j || $g==$k || $g==$l || $g==$m || $g==$n || $g==$o
|| $h==$a || $h==$b || $h==$c || $h==$d || $h==$e || $h==$f || $h==$g || $h==$i || $h==$j || $h==$k || $h==$l || $h==$m || $h==$n || $h==$o
|| $i==$a || $i==$b || $i==$c || $i==$d || $i==$e || $i==$f || $i==$g || $i==$h || $i==$j || $i==$k || $i==$l || $i==$m || $i==$n || $i==$o
|| $j==$a || $j==$b || $j==$c || $j==$d || $j==$e || $j==$f || $j==$g || $j==$h || $j==$i || $j==$k || $j==$l || $j==$m || $j==$n || $j==$o
|| $k==$a || $k==$b || $k==$c || $k==$d || $k==$e || $k==$f || $k==$g || $k==$h || $k==$i || $k==$j || $k==$l || $k==$m || $k==$n || $k==$o
|| $l==$a || $l==$b || $l==$c || $l==$d || $l==$e || $l==$f || $l==$g || $l==$h || $l==$i || $l==$j || $l==$k || $l==$m || $l==$n || $l==$o
|| $m==$a || $m==$b || $m==$c || $m==$d || $m==$e || $m==$f || $m==$g || $m==$h || $m==$i || $m==$j || $m==$k || $m==$l || $m==$n || $m==$o
|| $n==$a || $n==$b || $n==$c || $n==$d || $n==$e || $n==$f || $n==$g || $n==$h || $n==$i || $n==$j || $n==$k || $n==$l || $n==$m || $n==$o
|| $o==$a || $o==$b || $o==$c || $o==$d || $o==$e || $o==$f || $o==$g || $o==$h || $o==$i || $o==$j || $o==$k || $o==$l || $o==$m || $o==$n
);
You could do this with an array:
$randoms = array();
while(count($randoms) < 15)
{
$randoms[] = mt_rand($min, $max);//add random
$randoms = array_unique($randoms);//remove duplicates
}
$randoms = array_combine(range('a', 'o'), $randoms);//set keys a to o
And if you really, Really want those values as separate variables, you can do this using variable variables:
foreach ($randoms as $letter => $val)
{
$$letter = $val;
}
Because I wrote $$letter (note two $ signs), this expression evaluates to $<value of $letter> or $a, $b and so on, to which I assign the corresponding value
I suppose, if you want to get the best performance for the least amount of code/overhead, this would be the way to get it:
$random = array();
$count = 0;
$rand = null;
while($count < 15)
{
$rand = mt_rand($min, $max);
if (!isset($random[$rand]))
{//assign random value, and use keys for faster lookup
++$count;
$random[$rand] = $rand;
}
}
var_dump($random);//guaranteed to hold 15 random values, both the keys and values
//so:
$vals = array_values($random);//or array_keys($random)
And again, to turn this array into a distinct variables:
$arr = array_combine(range('a', 'o'), array_values($random));
//or even array_combine(range('a', 'o'), $random);
foreach ($arr as $name => $val)
$$name = $val;
try this
$min = 0;
$min = 999;
while (count($array) < 16){
$val = rand($min,$max);
if (!in_array($val, $array)) {
$array[] = $val;
}
}
The easiest way is to add the variables to an array and then perform your do-while as such:
$arr = array(&$a, &$b, &$c, &$d);
do {
foreach ($arr as $i => $k)
$arr[$i] = rand($min, $max);
}
while (count($arr) == array_unique($arr));
But in your example you are re-doing a lot of work that is not needed, e.g do not re-random unique values. Just a tip.
In PHP you can use variables to access other variables by names in strings.
For instance you can do something like that in your case:
$names = array('a','b','c','d'); // And so on - it can be done easier with casting to char
function check_names_equal($names) {
foreach($names as $name) {
global $$name;
}
foreach($names as $name) {
foreach($names as $name2) {
if ($name != $name2 && $$name == $$name2) {
return true;
}
}
}
return false;
}
while(check_names_equal($names)); // <- this is your shortened while
My comment to that - I know it's not the perfect way to that and I would suggest that you use arrays for holding variables, don't store some random data in single variables.
Your start of the code should like like this prefferably: (it would better to use numbers as indexes too, so you can use loops)
$arr = array();
$arr['a'] = rand($min,$max);
$arr['b'] = rand($min,$max);
$arr['c'] = rand($min,$max);
$arr['d'] = rand($min,$max);
$arr['e'] = rand($min,$max);
$arr['f'] = rand($min,$max);
$arr['g'] = rand($min,$max);
$arr['h'] = rand($min,$max);
$arr['i'] = rand($min,$max);
$arr['j'] = rand($min,$max);
$arr['k'] = rand($min,$max);
$arr['l'] = rand($min,$max);
$arr['m'] = rand($min,$max);
$arr['n'] = rand($min,$max);
$arr['o'] = rand($min,$max);
And with numeric indexing:
$arr=array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min,$max);
}
Ahother option is to create an array and go growing it with random values comparing every new vaue with the existing ones:
$myvars=array();
while (sizeof($myvars)<15)
{
$success=false;
do{
$candidate=rand($min,$max);
//compare the candidate with every element in array
if (!in_array($candidate, $myvars))
$success=true; //if found no success
} while (!$success);
$myvars[]=$candidate;
}
Now in $myvars you have 15 different values.

Categories