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

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()

Related

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

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.

Am unable to remove messages that are nested within $_SESSION array using a foreach loop, unset

For some reason my code below is not completely working. i’ve been unable to unset the array element. otherwise, the code seems to works. i’m using echo javascript alert to confirm the elements exist in the array. Maybe the problem has to do with the nature of $_SESSION vars?
The array is this,
Array
(
[commerce_cart_orders] => Array
(
[0] => 33
)
[messages] => Array
(
[error] => Array
(
[0] => Notice: Use of undefined constant actions - assumed 'actions' in mytheme_form_alter() …..
[1] => Notice: Use of undefined constant submit - assumed 'submit' in mytheme_form_alter() ….
[2] => Notice: Use of undefined constant actions - assumed 'actions' in mytheme_form_alter() ….
[3] => Notice: Use of undefined constant submit - assumed 'submit' in mytheme_form_alter() ….
My code so far is this,
$toRemoveOne = "Notice: Use of undefined constant actions - assumed 'actions' in ";
$toRemoveTwo = "Notice: Use of undefined constant submit - assumed 'submit' ";
foreach ($_SESSION['messages']['error'] as $val) {
if((strpos($val, $toRemoveOne) == true) || (strpos($val, $toRemoveTwo) == true)){
echo '<script>alert("it exists")</script>';
//unset($_SESSION['messages']['error'][$val]);
unset($val);
}
but then i print out the $_SESSION vars and they are still there.
What am i doing wrong here? thanks!
Try using:
foreach ($_SESSION['messages']['error'] as $key => $val) {
if((strpos($val, $toRemoveOne) == true) || (strpos($val, $toRemoveTwo) == true)){
echo '<script>alert("it exists")</script>';
//unset($_SESSION['messages']['error'][$val]);
unset($_SESSION[$key]);
}
$val is passed by value not by reference
RTM: http://php.net/strpos
It can return either an integer position of the matched string, or boolean false. It'll never return true. == true will simply be treated as == 1, and since your matched string occurs right at position 0, you're doing 0 == 1 and the if() never triggers.
You want:
if (strpos(...) !== false)
^^^
instead.
As well, $val in your foreach() is a COPY of the value in the array. You're unsetting that temporary copy, not the value that's actually in the array. you need to unset($yourarray[$key]) instead.

PHP variables variable not displaying if passed as an array or object

This works with simple variables. But it shows empty result with complex variables. AM I MISSING SOMETHING HERE? or is there anyother way around. Thanks.
#1. This works with simple variables.
$object = "fruit";
$fruit = "banana";
echo $$object; // <------------ WORKS :outputs "banana".
echo "\n";
echo ${"fruit"}; // <------------ This outputs "banana".
#2. With complex structure it doesn't. am I missing something here?
echo "\n";
$result = array("node"=> (object)array("id"=>10, "home"=>"earth", ), "count"=>10, "and_so_on"=>true, );
#var_dump($result);
$path = "result['node']->id";
echo "\n";
echo $$path; // <---------- This outputs to blank. Should output "10".
Not exactly using variable variables, but if you want to use a variable as the var name, eval should work
$path = "result['node']->id";
eval("echo $".$path.";");
From php.net's page on variable variables
A variable variable takes the value of a variable and treats that as the name of a variable.
The issue is that result['node']->id is not a variable. result is the variable. If you turn on error reporting for PHP notices you will see the following in your output:
PHP Notice: Undefined variable: result['node']->id ...
This can be solved as follows:
$path = "result";
echo "\n";
echo ${$path}['node']->id;
The curly braces around $path are required.
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
If not present the statement is equivalent to
${$path['node']->id}
which will result in the following output:
PHP Warning: Illegal string offset 'node' in /var/www/html/variable.php on line 18
PHP Notice: Undefined variable: r in /var/www/html/variable.php on line 18
PHP Notice: Trying to get property of non-object in /var/www/html/variable.php on line 18

Undefined offset warning in php

I am doing this simple thing in php whenever i run the code i got an error
Notice: Undefined offset: 3 in C:\xampp\htdocs\colorconverter.php on line 37
this is the code that generated that error
function colorConverter($color)
{
preg_match_all("/(\d+\.+\d+)/", $color, $rgba);
list($rgba[0], $rgba[1], $rgba[2], $rgba[3]) = $rgba[1] ;
$rgbaValues = array("RED"=>$rgba[0], "GREEN"=>$rgba[1], "BLUE"=>$rgba[2], "ALPHA"=>$rgba[3]);
return $rgbaValues;
}
although it return correct value but why it still show an error
It should be because you do not have $rgba array with 4 elements from the beginning.
Preg match all returns 2 elements 0 and 1 where second (1) is array which I guess is $rgba[1][0], $rgba[1][1], $rgba[1][2] and so on. You are trying to override $rgba[1] with its child elements.
Either declare new array and fill it with 4 empty elements, or not apply array elements in list() there should be variables:
list($rgba1, $rgba2, $rgba3, $rgba4) = $rgba[1] ;

Variable Variable wont work with array

I'm trying to do a variable variable, but it wont work when I use it in the code below.
I keep getting:
Notice: Undefined variable: C in C:\web\apache\htdocs\cats-test.php on line 8
This only wont work when used with an array. Can you help?
$Consumer = array(
"a" => "Apparel",
"b" => "Books & Stationary",
);
$cat = "Consumer";
echo $$cat['a']; //I'm trying to make this $Consumer['a'];
echo ${$cat}['a'];
It's ambiguous whether you mean $$cat ['a'] or $ $cat['a']. Use brackets.
Be aware of operator priorities. ${$cat}['a'] should work better.
When accessing an array key in a variable variable, enclose the variable in {} to be certain that PHP expands the correct set of characters ($cat) as a variable.
echo ${$cat}['a'];
// Apparel

Categories