There is post of select form like:
<select name="option[color][0]">
<select name="option[color][1]">
// option[color][2] isnt posted
Some products doesnt have that select, and then when I try to get them from post, each time if select isn't posted, im getting error like:
Undefined offset: 2
How to check if something is posted?
Tried:
$ids = $_POST['id'];
$option = $_POST['option'];
foreach ($ids as $key => $id)
{
//Undefined offset: 2
if( $option['color'][$key] )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
//Undefined offset: 2
if( !empty($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
//Undefined offset: 2
if( isset($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
//... etc
}
Etc.... what ever I try, there is error :(
Please help
Try array_key_exists to see if it exists.
isset($option['color'][$key]) is the way to go.
Check the exact line of code the error occurs when you still get it using isset().
if it is always 0,1,2 or any line of consecutive integers you could do if(count($option['color']) > $key ){}
if( isset($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
use isset or empty.
http://php.net/manual/en/function.isset.php
http://us2.php.net/manual/en/function.empty.php
for example:
if (isset($array['idx'])){ ... }
if (!empty($array['idx'])){ ... }
Related
Warning: array_key_exists() expects parameter 2 to be array, string given in D:\xampp\htdocs\ecom\customer\data.php on line 45
Notice: Array to string conversion in D:\xampp\htdocs\ecom\customer\data.php on line 51
My Code
$qry = dbQuery("SELECT `orders_product`.`pd_id`, `op_quantity` FROM `orders_product`
INNER JOIN `products_detail` ON `products_detail`.`pd_id`=`orders_product`.`pd_id`
WHERE orders_product.order_id = $id");
if(dbNumRows($qry)>0) {
$cart="";
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
$result = dbFetchArray($qry);
while($result = dbFetchArray($qry)) {
$product_detail_id = $result["pd_id"];
$order_quantity = $result["op_quantity"];
// echo $product_detail_id;
#print_r($result);
if(array_key_exists($product_detail_id, $_SESSION['cart'])){
$quantity = $_SESSION['cart'][$product_detail_id]['quantity'] + $order_quantity;
$_SESSION['cart'][$product_detail_id]['quantity'] = $quantity;
} else {
$cart = array("pd_id"=>$product_detail_id, "quantity"=> $order_quantity);
$_SESSION['cart'][$product_detail_id] = $cart;
print_r($cart);
}
}
}
$return = 1;
echo json_encode($return);
}
First problem
Warning: array_key_exists() expects parameter 2 to be array, string given in D:\xampp\htdocs\ecom\customer\data.php on line 45
That's because you use $_SESSION['cart'] with function array_key_exists. So when a person enters the website for the first time the variable $_SESSION['cart'] is not set.
What you do then is assign an empty string.
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
So then you use empty string with function array_key_exists.
Solution
To resolve this issue simply change
if(!isset($_SESSION['cart'])) { $_SESSION['cart']="";}
to
if(!isset($_SESSION['cart'])) { $_SESSION['cart']=array();}
Second problem
Notice: Array to string conversion in D:\xampp\htdocs\ecom\customer\data.php on line 51
Now, this is only a notice so that's not a big issue. It may not even be something wrong. After you change to not display errors it won't affect your website in any way.
But why is this showing? Again the same reason as with the previous problem. You assign empty string to variable $_SESSION['cart'] but then you treat is as an array
$_SESSION['cart'][$product_detail_id] = $cart;
When you change the assignment from empty string to empty array this issue will be solved as well.
By the way when you write a one line if statement a good practice is not to use the braces
So I would write it like
$_SESSION['cart'] = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
Or even better
$_SESSION['cart'] = $_SESSION['cart'] ?? [];
But this is only php 7.0 and higher
I hope you understand now what was wrong and why. If you have any questions feel free to ask. I'll try to reply the best away I can.
I'm trying to count the occurence of a string in multidimentional array and get it.
So I found this : http://php.net/manual/fr/function.array-count-values.php
and an exemple in the "User Contributed Notes".
Here my code :
$count_values = array();
foreach ($Bigtable as $a)
{
foreach ($a as $table)
{
$count_values[$table]++; // line 32
}
}
asort($count_values,SORT_NUMERIC );
$onet_code=end ($count_values);
Here my error: Notice: Undefined index: 11-1011.00 in /home/goldiman1/www/postQuestion.php on line 32
I think that the error is in the last line when I try to get the string.
What do you guys think about it?
Thank you
Goldiman
Edit: Thank you everybody for your help ! All solutions work like a charm and I understood what was the issue, it was easier to understand Kasia answer because i'm more familiar with isset()
$count_values[$table]++;
This line is trying to increment the value in $count_values[$table] - but you never initialize that value in the first place!
You need to change this code to create the value at 1 if it doesn't already exist, and only increment it if it does exist.
If $table exists on the array(key) then increment the value else create new one. Try this -
foreach ($Bigtable as $a)
{
foreach ($a as $table)
{
if (array_key_exists($table, $count_values)) {
$count_values[$table]++;
} else {
$count_values[$table] = 1;
}
}
}
by default $count_values[$table] won't be set
foreach ($a as $table)
{
if(isset($count_values[$table])){
$count_values[$table]++;
} else {
$count_values[$table] =1
}
}
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;
}
This is my code:
if(is_array($ItemAttr["Actor"])){
$Actors = implode(", ", $ItemAttr["Actor"]);
} else {
$Actors = $ItemAttr["Actor"];
}
I'm getting undefined index: Actor in **line 1** and **line 3**
I guess i should use isset() function. Can anyone tell me how to combine that function with is_array() function..?
Not sure if this is what you mean but:
if( isset($ItemAttr["Actor"]) && is_array($ItemAttr["Actor"])){
....
}
In this case you are checking if the index exist before accessing its value.
Right so i have E_NOTICES on and my code works its just i keep getting "Severity: Notice Message: Undefined index: 0" everytime i try to insert my data into the array with the set key. Its really annoying when ur trying to debug.
What am i doing wrong that will make the notices go away without turning off E_NOTICES?
foreach ($bracketmatches->result() as $row)
{
if(!isset($bracketdata[$row->position]))
{
$bracketdata[$row->position] = array();
}
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
Is $teams[$row->home_id] definitely defined?
edit: Quick and dirty test for you:
foreach ($bracketmatches->result() as $row)
{
if(!isset($teams[$row->home_id]))
{
die('GOTCHA!!!');
}
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
Impossible to say for certain without more information, but I would check that $row->position is set and that $row->home_id is set if there is any possibility they may be undefined.
You must initialize the base array before pushing values to it. The isset here doesn't really do anything. Just throw it away. If you still get the error make sure $teams[$row->home_id]['team_name'] is always set.
$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
if (isset($row->position) && !empty($row->position) && isset($teams[$row->home_id]['team_name']))
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}