I am trying to validate multiple numeric fields for multiple checks ie. There are 6 numeric field and I want to run 6 checks.
Firstly, total less than 0 and more then 60 give expected results, but
Failing checks and not behaving as expected for the checks below:
a. Integer
b. Numeric
c. Negative
d. Not Float
$seatsSTA = $_POST['seats']['STA'];
$seatsSTP = $_POST['seats']['STP'];
$seatsSTC = $_POST['seats']['STC'];
$seatsFCA = $_POST['seats']['FCA'];
$seatsFCP = $_POST['seats']['FCP'];
$seatsFCC = $_POST['seats']['FCC'];
$minSeats = 1; $maxSeats = 60;
$totalSeats= ((int)$seatsSTA + (int)$seatsSTP + (int)$seatsSTC + (int)$seatsSTP + (int)$seatsFCA + (int)$seatsFCP + (int)$seatsFCC );
if ( $totalSeats < $minSeats ) {
$errorsBook['user']['seats'] = "Atleast one session must be selected. ";
// header("Location: index.php");
} elseif ( $totalSeats > $maxSeats ) {
$errorsBook['user']['seats'] = "Too many seats selected. ";
// header("Location: index.php");
} elseif ( !is_int($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "Please correct seat selection. ";
header("Location: index.php");
}
elseif ( !is_numeric($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "NOT numeric character. ";
header("Location: index.php");
}
elseif (($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) < 0 ){
// $errorsBook['user']['seats'] = "Negative number not allowed. ";
header("Location: index.php");
}
elseif ( is_float($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "Please correct seat selection. ";
header("Location: index.php");
}
return $errorsBook;
}
Related
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 ;
}
I'm trying to check if a date is real, it must return true, and if not, it must return false.
It does not seemes to work when I write 35-02-2012 (Date format dd-mm-yy) it return true, but I was expecting false, I do not know where I'm wrong.
below is my function
function isItRealDate($date) {
if ($date == '') {
return false;
} else {
$rxDatePattern = '/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/'; //Declare Regex
$dtArray = preg_match($rxDatePattern, $date); // is format OK?
if ($dtArray == '0') {
return false;
} else {
$tableau_date = explode('-', $date);
//Checks for dd-mm-yyyy format.
$dtMonth = $tableau_date[1];
$dtDay = $tableau_date[0];
$dtYear = $tableau_date[2];
if ($dtMonth < 1 || $dtMonth > 12) {
return false;
} elseif ($dtDay < 1 || $dtDay > 31) {
return false;
} elseif (($dtMonth == 4 || $dtMonth == 6 || $dtMonth == 9 || $dtMonth == 11) && $dtDay == 31) {
return false;
} elseif ($dtMonth == 2) {
$isleap = ($dtYear % 4 == 0 && ($dtYear % 100 != 0 || $dtYear % 400 == 0));
if ($dtDay > 29 || ($dtDay == 29 && !$isleap)) {
return false;
} else {
return true;
}
}
}
}
}
anykind of help will be much appreciated
If you want something that just works, use checkdate().
As suggested by Ibrahim, use:
function isItRealDate($date) {
if(preg_match("#^(\d{1,2})\-(\d{1,2})\-(\d{1,4})$#", $date, $match)){
//regex could match 99-99-9999 - checkdate will take care.
return checkdate($match[2], $match[1], $match[3]);
}
return false;
}
This will work for ANY (valid) date between 1-1-1 and 31-12-9999
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';
}
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.
This seems simple but I don't know why it doe snot work.
I need to write an if statement that
first, checks if it is numeric
second, if it is not between 1 and 10, issue errorA
third, if it is not between 20 and 30, issue errorB
fourth, it is not a number, issue errorC
If is not numeric and satisfies all the ranges, added to the database.
anyways, I am not sure about the if and while combination to satisfy this....
So far I have,
if numeric and satisfies ranges, add to database
else, issue errorC
How can I filter for error A and B?
if ( isset [some code...]) {
$a = ...;
$b = ...);
$c = ...;
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c) &&
((1 <= $b && 10 >= $b)) && ((20 <= $c && 30 >= $c))) {
$sql = "INSERT [some code...]
mysql_query($sql);
$_SESSION['success'] = $_POST['success'];
header('Location: index.php') ;
return;
} else {
$_SESSION['error'] = $_POST['error'];
header('Location: index.php') ;
return;
}
}
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c)) {
if (($b >= 1 && $b <= 10) && ($c >= 20 && $c <= 30)) {
echo "OK";
} else {
echo "not in range";
}
} else {
echo "not a number";
}