Undefined index: cnt - php

Showing error as undefined index cnt.
please help me regarding this issue.thanks in adcance
if($_REQUEST["cnt"]!=""){
$count=$_REQUEST["cnt"];
$cntprev=$count-2;
}else
{
$count=1;
}

You have to check if the index is set. Actually you just check if it is an empty string. But you have to check if it is set, not empty and the value is numeric (if you want to cat it to int float ...). This should work:
if(isset($_REQUEST["cnt"]) && !empty($_REQUEST["cnt"]) && is_numeric($_REQUEST["cnt"])) {
$count=$_REQUEST["cnt"];
$cntprev=$count-2;
} else {
$count=1;
}

Related

Iterating through 3 loops of different length - PHP

I have 2 arrays and i want to make a 3rd array after comparison of the 2 arrays. Code is as follows:
foreach($allrsltntcatg as $alltests)
{
foreach($alltests as $test)
{
foreach($allCatgs as $catg)
{
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
$catcounts[$catg['testcategoryname']] +=1;
}
}
}
}
It, although returns the right answer, it also generates a PHP error and says undefined index and prints all errors and also the right answer.
I just want to avoid the array out of bound error. Kindly help me
Problem is in if condition correct like below : You have to initialize array first and than you can increment value
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
if (isset($catcounts[$catg['testcategoryname']]))
$catcounts[$catg['testcategoryname']] +=1;
else
$catcounts[$catg['testcategoryname']] =1;
}
When the array try to add some arithmetic operation of undefined index such as $catg['testcategoryname'] in the $catcounts array then the warning generates. Before add the number you have to check the index is present or not, and of not then just assign value otherwise add into it.
So do it in this way just if condition-
if(....){
if(array_key_exists($catg['testcategoryname'], $catcounts))
$catcounts[$catg['testcategoryname']] +=1; // Add into it
else
$catcounts[$catg['testcategoryname']] = 1; // Assign only
}
More about array key exists--See more
$catg['testcategoryname'] should represent an index in $catcounts array.

Undefined variables in point in polygon function

I created a function to see if a lat/lon are inside a polygon. I'm getting these notice: undefined variable: xx.xx but its an array of lat/lons being passed and its the value that is undefined not the array. I'm confused help please. The line is the second / the last if statement
public function checkCoordinates($lat, $lon, $polylat, $polylon) {
$j = count($polylat)-1;//number of sides and -1 because its an array
$result = true;
for($i=0;$i<count($polylat);$i++){
if($polylat[$i]<$lat && $polylat[$j]>=$lat
|| $polylat[$j]<$lat && $polylat[$i]>=$lat){ //if the latitude at the beggining is bigger than and at the end is smaller than or vise versa
if($polylon[$i]+($lat-$polylat[$i])/($$polylat[$j]-$polylat[$i])*($polylon[$j]-$polylon[$i])<$lon){ //
$result = false;
}
}
$j=$i;
}
return $result;
}
I didn't notice the extra $, that explains it

PHP - undefined offset

This code below has Undefined offset error on line 5.
I don't know why this appears, I'm fighting with this for about an hour.
It says it's in a line where for is, but as I see syntax is correct :/
<?php
function palindrom($broj) {
$brojniz=str_split($broj);
for ($x=0; $x<3; $x++) {
if ($brojniz[$x] != $brojniz[5-$x]) {return;}
}
return($broj);
}
$n=100;
$m=$n;
while ($n<1000) {
while ($m<1000) {
$br=$m*$n;
palindrom($br);
++$m;
}
$m=100;
++$n;
}
?>
but as I see syntax is correct
Yes, the syntax is correct. But the runtime values are not. This syntax is also correct, but will produce an error:
$x = 1 / 0;
The line in question is indexing an array:
if ($brojniz[$x] != $brojniz[5-$x])
And the value $x goes from 0-2 in that loop. So you're indexing as such:
if ($brojniz[0] != $brojniz[5])
if ($brojniz[1] != $brojniz[4])
if ($brojniz[2] != $brojniz[3])
Does that array go from 0-5? If not, then you're referencing an undefined index.

Undefined Index in php array

Looking to get a count of particular key=>values in an multi dimensional array. What I have works i.e. the result is correct, but I can't seem to get rid of the Undefined Index notice.
$total_arr = array();
foreach($data['user'] as $ar) {
$total_arr[$ar['city']]++;
}
print_r($total_arr);
Any ideas? I have tried isset within the foreach loop, but no joy...
$total_arr = array();
foreach($data['user'] as $ar) {
if(array_key_exists($ar['city'],$total_arr) {
$total_arr[$ar['city']]++;
} else {
$total_arr[$ar['city']] = 1; // Or 0 if you would like to start from 0
}
}
print_r($total_arr);
PHP will throw that notice if your index hasn't been initialized before being manipulated. Either use the # symbol to suppress the notice or use isset() in conjunction with a block that will initialize the index value for you.

PHP - checking isset on a $_SESSION[$_REQUEST[]] variable

Seems an easy one, but cannot work out why:
if(!isset($_SESSION[$_REQUEST["form_id"]]))
{
//do stuff
}
reutrns
Notice: Undefined index: form_id
empty returns same response.
This has been driving me mad for a while. :)
You're calling isset for $_SESSION but as the error states the issue is with $_REQUEST['form_id'] not being set.
if (!isset($_REQUEST['form_id']) || !isset($_SESSION[$_REQUEST['form_id']])) {
That's because it resolves $_REQUEST['form_id'] first and that causes the notice. You could do this instead:
if (!isset($_REQUEST['form_id']) || !isset($_SESSION[$_REQUEST["form_id"]]))
{
//do stuff
}
please check if key exists with
array_key_exists('form_id', $_REQUEST);
before checking value with
isset($_REQUEST['form_id']);
or check if your params are empty like
<?php
if (!empty($_REQUEST['form_id'])) {
// do anything
}
else
{
// I can't find the key in array
}
?>

Categories