Notice: Undefined variable: node in include() Drupal 7 - php

I have looked but it seems I can't find the right answer or I don't have the skills for that. The thing is I'm getting this error:
Notice: Undefined variable: node in include() (line 69 of /home/xwebmedia/public_html/ltr/sites/all/themes/ltr/page.tpl.php).
and the code I'm using is:
<?php
if (count($node->field_adds) != 0)
{
foreach($node->field_adds['und'] as $key => $value)
{
$nid = $value['nid'];
$mywidget = node_view(node_load($nid));
print drupal_render($mywidget);
}
}
?>
Thing is everything is working fine, I get my widgets with adds in sidebar, but I am having this notice of error.
I know that I need to define a variable in template.php but I tried something it didn't work.
Any suggestions?

Check that $node has been set.
<?php
if (isset($node) && count($node->field_adds) != 0)
{
foreach($node->field_adds['und'] as $key => $value)
{
$nid = $value['nid'];
$mywidget = node_view(node_load($nid));
print drupal_render($mywidget);
}
}
?>

The page template file doesn't have a $node variable by default (you can have pages that aren't nodes so it's not required).
The menu_get_object() function is your friend here:
$node = menu_get_object();
if ($node) {
...
}

Related

2 warnings: Cannot use a scalar value as an array and Illegal offset type in isset or empty

Here is the code that I wrote:
class init{
private $time_spent;
public function __construct(){
$this->time_spent = array(); //<- caching variable
}
function get_course_unit_time_spent( $user_id,$course_id,$unit_id ){
if(empty($this->time_spent) || empty($this->time_spent[$course_id]) || empty($this->time_spent[$course_id][$unit_id])){
$this->time_spent[$course_id][$unit_id] = get_user_meta($user_id,'time_spent_'.$course_id.'_'.$unit_id,true);
if( empty($this->time_spent[$course_id][$unit_id]) ){
$this->time_spent[$course_id][$unit_id] = 0;
}
}
return $this->time_spent[$course_id][$unit_id];
}
function get_course_time_spent( $user_id,$course_id ){
if( empty($this->time_spent[$course_id]) ){
if(!bp_course_is_member($course_id,$user_id))
return 0;
$time_spent = 0;
$this->time_spent[$course_id] = array();
$course_curriculum = bp_course_get_curriculum( $course_id );
foreach ($course_curriculum as $key => $unit_id) {
if( is_numeric($unit_id) ){
$time_spent += $this->get_course_unit_time_spent( $user_id,$course_id,$unit_id );
}
}
$this->time_spent[$course_id] = $time_spent;
}
return $this->time_spent[$course_id];
}
}
There are lots of code but the issue is with the first function I wrote above, in two different places I am getting the above to warnings.
1) Warning: Cannot use a scalar value as an array on line 10 and 12 (in the above code).
2) Warning: Illegal offset type in isset or empty on line 8 (in the above code).
3) Warning: Illegal offset type on line 10, 11 and 12 (in the above code).
The second function is not throwing any error but whenever I use the 1st function or the second everytime the warning comes from the 1st function. I am not sure what is it, can someone help to correct it ?
UPDATE: Updated the code and now the first warning is gone but still getting the illegal ofset error.
Your calling get_course_time_spent(), which creates an array for
$this->time_spent[$course_id][$unit_id] in
get_course_unit_time_spent()
Then your setting $this->time_spent[$course_id] as an int once the foreach loop has finished.
Then on the next call to get_course_unit_time_spent your checking
as if its an array empty($this->time_spent[$course_id][$unit_id])

PHP - How do I expand all arrays within an array

My code is giving an error when I try to echo all values within an array but also expand and echo the values of any arrays in the original array.
while (list($key, $val) = each($bal)) {
If (is_array($val)){
while (list($k, $v) = each($v)) {
If (is_array($va)){
while (list($ka, $va) = each($va)) {
echo "$ka => $va\n <br>";
}
} else {
echo "$k => $v\n <br>";
}
}
} else {
echo "$key => $val\n <br>";
}
}
The error I am getting is
Warning: Variable passed to each() is not an array or object in
C:\xampp2\htdocs\money\production\simalgorithm1.php on line 234
Line 234 is while (list($k, $v) = each($v)) {
Shouldn't this automatically not be an array as I did a check the line previous? My Syntax must be off I am fairly new to PHP if someone could help me see what I am not seeing I would greatly appreciate it.
Sometimes the simplest solutions are the best one line problem solved.
print_r($bal);
The problem is this line:
while (list($k, $v) = each($v)) {
I think you want this
while (list($k, $v) = each($val)) {
EDIT: I would also point out that your code will not work if the arrays are nested more than a few levels deep. You should consider defining a recursive function or using var_export or var_dump.

Cannot redeclare function previously declared php

I'm running into an "Cannot redeclare" error and I can't figure out how to fix it. So I have a few functions in a php file located below. Now these functions iterate over an array of data.
I think I've surmised that the problem is that I'm looping the function over and over again in the foreach loop, and its the foreach loop thats been the problem. It seems like its already writing one the function to memory the first time and then for some reason it doesn't like being evoked again.
Your help appreciated.
P.S I've seen a number of similar posts on the issue such as Fatal error: Cannot redeclare but that doesn't seem to work.
<?php
// *****Code Omitted from Stack****
function postHelper($data, $field1, $field2)
{ //TODO Abstract and make sure post Helper and modify Post can be the same thing.
$result = array();
for ($j = 0; $j < count($data); ++$j) { //iterator over array
if ($field2 == "") {
$result[$j] = $data[$j][$field1];
} else {
return $result[$j] = $data[$j][$field1][$field2];
}
}
return $result;
}
//returns an array with only # and # values
function modifyPost($data)
{
//puts symbol # before read data
function addSymbol($data, $field1, $field2)
{
$info = postHelper($data, $field1, $field2);
foreach ($info as &$n) {
$n = '#' . $n;
}
print_r($info);
}
/*
Parse texts and returns an array with only # or # signs used
*/
function parseText($data)
{
$newarr = array();
$text = postHelper($data, "text", "");
foreach ($text as &$s) { //separates into words
$ex = explode(" ", $s);
foreach ($ex as &$n) { //if text doesnt' begin with '#' or '#' then throw it out.
if (substr($n, 0, 1) === '#' || strpos($n, '#') !== false) {
array_push($newarr, $n . ',');
}
}
}
return $newarr;
}
}
foreach ($posts as $entry) {
if (!function_exists('modifyPost')) {
$nval = "hello";
modifyPost($entry);
$entry['mod_post'] = $nval;
}
}
?>
EDIT: I've solved the error. Turns out that the original posts did actually work. I messed in naming. I will give points to anyone who can explain to me why this is necessary for a call. Moreover, I will update post if there is an additional questions that I have.
Php doesn't support nested functions. Although you technically can declare a function within a function:
function modifyPost($data)
{
function addSymbol($data, $field1, $field2)
the inner function becomes global, and the second attempt to declare it (by calling the outer function once again) will fail.
This behaviour seems counter-intuitive, but this is how it works at the moment. There's RFC about real nested functions, which also lists several workarounds for the problem.
The error says it all. You have duplicate modifyData() & parseText functions.
Remove the top half of the php file so only one of each occurs.

mysql error msg reading table data (Warning: Invalid argument supplied for foreach() in)

ive this table which is updated and re-ranked every midnight ,
after few days i start getting this msg
Warning: Invalid argument supplied for foreach() in
in last page of pagination to fix it i used to trunk the table and run my code again then its work fine , here is my mysql statement case the issue
mysql_free_result($result);
$i = 0;
$total_amount = count($tweeps);
foreach ($tweeps as $tweep) {
$i++;
if ($total_amount == $i) {
$class = 'divrow divrowlast';
} else {
$class = 'divrow';
}
need to tips to do permanent fix
Without seeing what $tweeps contains its hard to help. You are however missing a closing bracket. For practice you should check if $tweeps is in fact an array though. My assumption is that $tweeps isn't an array.
if (is_array($tweeps)) {
foreach ($tweeps as $tweep) {
$i++;
if ($total_amount == $i) {
$class = 'divrow divrowlast';
} else {
$class = 'divrow';
}
}
}

Why am I getting this error Notice: Undefined index: host

my sample code is here
include 'simple_html_dom.php';
function get_all_links($url){
global $host;
$html = new simple_html_dom();
$html->load(file_get_contents($url));
foreach($html->find('a') as $a){
$host1 = parse_url($a->href);
$host = parse_url($url);
if($host1['host'] == $host['host']){
$data[] = $a->href;
}
}
return $data;
}
$links = get_all_links("http://www.example.com/");
foreach($links as $link){
echo $link."<br />";
}
When I try this code, I got this error: Notice: Undefined index: host in... What's wrong in my code? Please suggest me some helping code, Thanks in Advance.
You need to check if the arrays contain entries for 'host' using isset before assuming they exist:
if (isset($host1['host']) && isset($host['host'])
&& $host1['host'] == $host['host']) {
Or you can use # to suppress warnings from the check.
if (#$host1['host'] == #$host['host']) {
However, you'll need to double-check that the latter works as you desire when both are missing.
Update: As the others pointed out there is also array_key_exists. It will handle null array values whereas isset returns false for null values.
As others have answered, both isset() and array_key_exists() will work here. isset() is nice, because it can actually take multiple arguments:
if (isset($array[0], $array[1], $array[2]))
{
// ...
}
// same as
if (isset($array[0]) && isset($array[1]) && isset($array[2]))
{
// ...
}
Returning true only if all arguments are set.
you can find out if an array has an index called "host" using array_key_exists
if (array_key_exists($host, "host") && array_key_exists($host1, "host") && ...)
http://us3.php.net/manual/en/function.array-key-exists.php

Categories