I am having Undefined 1 and 2 on line 32 and 33. I don't even have an idea of what is going on and what the error means. How can i fix this please?
$splittedGSM = str_split($gsm);
$correctGSM = TRUE;
if(count($splittedGSM ) != 11) $correctGSM = FALSE;
if($splittedGSM[0] != 0) $correctGSM = FALSE;
if($splittedGSM[1] != 7 && $splittedGSM[1] != 8 && $splittedGSM[1] != 9) $correctGSM = FALSE; //Line 32
if($splittedGSM[2] != 0 && $splittedGSM[2] != 1) $correctGSM = FALSE; //Line 33
foreach ($splittedGSM as $realgsm) {
if(!is_numeric($realgsm)){
return FALSE;
}
}
Try Below code. Offset is not defined because it's not set you have to check that if it's not set then offset will be null. You have to use isset() function for it.
$splittedGSM = str_split($gsm);
$correctGSM = TRUE;
// check using isset function
if(!isset($splittedGSM[1]))
{
$splittedGSM[1] = null;
}
if(!isset($splittedGSM[2]))
{
$splittedGSM[2] = null;
}
if(count($splittedGSM ) != 11) $correctGSM = FALSE;
if($splittedGSM[0] != 0) $correctGSM = FALSE;
if($splittedGSM[1] != 7 && $splittedGSM[1] != 8 && $splittedGSM[1] != 9) $correctGSM = FALSE; //Line 32
if($splittedGSM[2] != 0 && $splittedGSM[2] != 1) $correctGSM = FALSE; //Line 33
foreach ($splittedGSM as $realgsm) {
if(!is_numeric($realgsm)){
return FALSE;
}
}
The problem is that you try to access elements 1 and 2 of $splittedGSM without first checking that they exist. You are probably calling str_split with an empty string as argument, in which case a one-element array containing the empty string will be returned. This explains why you don't get an error when accessing $splittedGSM[0].
As you require a valid result to contain 11 elements, you can solve the problem by only accessing the array elements if the length test is satisfied, using elseif instead of if:
$splittedGSM = str_split($gsm);
$correctGSM = TRUE;
if(!is_array($splittedGSM) || count($splittedGSM ) != 11){
$correctGSM = FALSE;
}elseif($splittedGSM[0] != 0){
$correctGSM = FALSE;
}elseif($splittedGSM[1] != 7 && $splittedGSM[1] != 8 && $splittedGSM[1] != 9){
$correctGSM = FALSE;
}elseif($splittedGSM[2] != 0 && $splittedGSM[2] != 1){
$correctGSM = FALSE;
}
foreach ($splittedGSM as $realgsm) {
if(!is_numeric($realgsm)){
return FALSE;
}
}
Related
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;
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
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
I have a problem with calculating the standard deviation in php. But it's generating the wrong result.
e.g. I am getting as result for (4+4+4+4+4+4) = 1.40 instead of 0.
Please help.
function std_dev ($attr, $test1,$test2,$test3,$test4,$test5,$test6) {
//$items = array($test1->$attr,$test2->$attr,$test3->$attr,$test4->$attr,$test5->$attr,$test6->$attr);
$items[] = array();
if (isset($test1) && $test1->$attr != 9 && $test1->$attr != 0) {
$items[] = $test1->$attr;
}
if (isset($test2) && $test2->$attr != 9 && $test2->$attr != 0) {
$items[] = $test2->$attr;
}
if (isset($test3) && $test3->$attr != 9 && $test3->$attr != 0) {
$items[] = $test3->$attr;
}
if (isset($test4) && $test4->$attr != 9 && $test4->$attr != 0) {
$items[] = $test4->$attr;
}
if (isset($test5) && $test5->$attr != 9 && $test5->$attr != 0) {
$items[] = $test5->$attr;
}
if (isset($test6) && $test6->$attr != 9 && $test6->$attr != 0) {
$items[] = $test6->$attr;
}
$sample_square[] = array();
$item_count = count($items);
for ($current = 1; $item_count > $current; ++$current) $sample_square[$current] = pow($items[$current], 2);
$standard_deviation = sqrt(array_sum($sample_square) / $item_count - pow((array_sum($items) / $item_count), 2));
return round($standard_deviation,2);
}
$items = array();
$sample_square = array();
no [] when you define those variables as arrays.
for ($current = 0; $item_count > $current; ++$current)
start from 0 not 1 if you want to iterate over all the elements (otherwise you'll miss item at index 0)
Wondering what this is for...
if (isset($test1) && $test1->$attr != 9 && $test1->$attr != 0) {
$items[] = $test1->$attr;
}
and how you pass input values to the function. This may also cause wrong result...
I have the following test on some data:
if(isset($option_type)
&& ($option_type == 3 || $option_type == 4
&& (isset($ext_data) && $ext_data != "")
)
){}
What it should do, is if $option type is NOT 3 or 4, the test should return true. If $option type is 3 or 4 AND $ext_data does not exist or equals "", then the test should return false.
However, no matter what $option_type is, it returns false.
How can I format this test so when the $option_type is 3 or 4 and $ext_data exists and is not "", the result is true?
lee
I would split the tests into two to make it clearer:
if (!isset($ext_data) || $ext_data == "") return false;
return $option_type != 3 && $option_type != 4;
//try this
if(isset($option_type)&& ($option_type == 3 || $option_type == 4)){
if (isset($ext_data) && $ext_data == ""){
return false;
}else{
return true;
}
}