Why does "if($_SESSION['count'] >= 3" giving me error? - php

This php line
<?php if($_SESSION['count'] >= 3)?>
of code gives me the following error message
Notice: Undefined index: count in C:\xampp\htdocs\vb\Step4.php on line
451 Number of Room/s

You need to check for the existence of the count index before trying to use it - so:
<?php
if( !empty( $_SESSION['count'] ) && intval( $_SESSION['count'] ) >= 3 ){/* do something */}
?>
You could use isset to test that the index has been defined but empty does that and also checks that whatever value is held does is not empty and not equate to false.

Session with name 'count' is not set.
i.e. The array $_SESSION does not any key with name 'count'.
Also, it is a NOTICE ( information ) not an ERROR.

Related

PHP comparing values with a for loop in arrays gives an offset error

I have 2D arrays where I have to keep comparing the next value.
I am using a for loop to go through all values and I am getting an offset error because when it reaches the last array, it wants to check the next array, which does not exist. How can I prevent that? I know that ($items[$row][0]!=$items[$row+1][0]) is the issue. Should I not use a for loop? What I do not understand is that the code below does not give any error. If $items[$row+1][0] is the problem when it reaches the last array, shouldn't $items[$row-1][0] give an error as well when it is checking the first array in the arrays?
if ($row==0 || ($row>0 && $items[$row][0]!=$items[$row-1][0]) )
But this one is not ok.
Notice: Trying to access array offset on value of type null
Notice: Undefined offset
if (($row<$num_rows && ($items[$row][0]!=$items[$row+1][0]))||$num_rows==$is_odd) {
//$is_odd is the number of last array.
//$num_rows is the total number of arrays.
echo "</table></div></div>";
}
in your condition check next index is exist or not like
if ((!empty($items[$row + 1]) && $row < $num_rows && ($items[$row][0] != $items[$row + 1][0])) || $num_rows == $is_odd) {
//$is_odd is the number of last array.
//$num_rows is the total number of arrays.
echo "</table></div></div>";
}

Notice: Undefined index with used isset and $_GET

this is my code
if(isset($_GET['delt']) || isset($_GET['editt'])){
$delt = intval($_GET['delt']);
$editt = intval($_GET['editt']);
}
when run the code I get the error
Notice: Undefined index: delt in
/opt/lampp/htdocs/tel-s/admin/top_a.php on line 116
I'm confused because i use isset and test used empty but do get the same problem
Your are testing only if one of params is set and later use both of them no matter if it's actually set.
Use && (and) instead of || (or):
if (isset($_GET['delta']) && isset($_GET['editt']))
OR
if (isset($_GET['delta'], $_GET['editt']))
Check both of variables for exists or change code
if( isset($_GET['delt']) ) {
$delt = intval($_GET['delt']);
}
if ( isset($_GET['editt']) ) {
$editt= intval($_GET['editt']);
}
It must have gotten $_GET['editt'] but couldn't get $_GET['delt'] and threw an error because it couldn't set anything to $delt. Consider #Justinas' answer.
did your url contains both "delt" and "editt" parameters ?
what is the out put of print_r($_GET) ?
isset function checks whether a variable is set or not ?
if you pass a string to intval() it will return 0

Undefined offset for $_SESSION variable

I have been trying to create a very simple shopping cart and I'm having a problem with my $_SESSION array. This is for a school project and I'm trying to make it as simple as possible.
The error I'm getting is:
Notice: Undefined index: cart in C:\xampp\htdocs\Final\menu.php on
line 31
Notice: Undefined offset: 5 in C:\xampp\htdocs\Final\menu.php on
line 31
if(isset($_GET['id'])){
$product_id = $_GET['id'];
$_SESSION['cart'][$product_id]++;
print_r($_SESSION);
print "<br>";
print_r($_GET);
}
Once I've added more than one item to a particular product_id, the error goes away. This is the way the tutorial I read explained to add items to the cart. Any suggestions?
Looks like $_SESSION['cart'] does not yet exist. Since it's going to be an array, instantiate it first with:
if(!array_key_exists('cart', $_SESSION)) $_SESSION['cart'] = array();
Since you have not yet assigned anything to $_SESSION['cart'][$product_id], you'll get this type of error when trying to increment it. You may want to try:
$_SESSION['cart'][$product_id] = (array_key_exists($product_id, $_SESSION['cart'])) ? $_SESSION['cart'][$product_id] +1 : 1;
or as an if statement:
if(array_key_exists($product_id, $_SESSION['cart'])) $_SESSION['cart'][$product_id]++;
else $_SESSION['cart'][$product_id] = 1;
When you do $_SESSION['cart'][$product_id]++; you are actually doing:
$_SESSION['cart'][$product_id] = $_SESSION['cart'][$product_id] + 1;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ generates warning if they keys do not exist yet
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ assigns without problems if they keys do not exist yet
The assignment with newly created keys is not the problem, the warning is generated by php trying to get the actual value of $_SESSION['cart'][$product_id].
To solve this you should properly initialize the variable:
$_SESSION['cart'][$product_id] = isset($_SESSION['cart'][$product_id])
? $_SESSION['cart'][$product_id]++
: 1;

Want to fix undefined index notice from this code

My code is this and I want to remove following notices:
1. Notice: Undefined index: name
2. Notice: Undefined index: website
3. Notice: Undefined index: description
Also, this code adding data automatically when I refresh the webpage how to get rid of?
<?php
{
// Instructions if $ _POST [ 'name'] exists
}
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
?>
You should use isset()
For example
if (isset($_POST['name']){
echo $_POST['name'];
}
As a side note, read this article on the $wpdb class, looks like your code isn't follwing the guidelines there.
What you need to do, is check to see if the indexes you need, actually exists and is not empty, before trying to use them.
if ( isset ( $_POST['name'] ) && ! empty ( $_POST['name'] ) &&
isset ( $_POST['website'] ) && ! empty ( $_POST['website'] ) &&
isset ( $_POST['description'] ) && ! empty ( $_POST['description'] ))
{
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
}
To get rid of the post being added again after refreshing, you could redirect to the same URL, after posting. This way it removes the POST from the URI.
header ( 'location : http://myDomain.com/myUrl.php' );
This redirect, would of course only get called, inside the if statement, we created before, and be the last thing you do with the POST.
You should use issetfor variable checking that those variables are assigned correctly or not.
And now Your Second problem that if you refreshed the page the same value is added to the database. After Submitting a Form Browser Keeps those value in there memory so if you try ignore this problem then do this:
<?php
{
// Instructions if $ _POST [ 'name'] exists
}
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
header('Location:/path/to/your/file.php');
?>
Here After Adding the Value To database Your are redirected to the same page where all submitted variables are gone.

Drupal 7 - Notice: Undefined index: und in include()

I'm getting this error:
Notice: Undefined index: und in include() (line 24 of /home/cliffdwellerproductions/dev.cliffdwellerdigital.com/Dahl/sites/all/themes/basic/templates/node--page2.tpl.php).
the code is:
if ($node->field_body_left !== NULL) :
$text = trim($node->field_body_left['und']['0']['value']);
else:
$text = '';
Please help, as I haven't been able to define the variable...
Alf
Your $node->field_body_left variable is existent but it doesn't have an 'und' element.
It looks like you're attempting to check for an empty field, but you're using $field_body_left!==null which will only be false if the variable is literally null. When a drupal field is present but empty, it's usually equal to array(). Use != instead of !==, and then it will correctly detect both null variables and empty arrays and move on.
--
Extra info: If the variable had a value, its structure would be:
$field_body_left = array(
'und' => array(
0 => array (
'value' => YOURVALUE
)
)
)
But since it doesn't have a value, its structure is:
$field_body_left = array()

Categories