Jump to content

Really big newbie question -- foreach errors


sw9

Recommended Posts

Hi Everyone,

 

I have what I hope is a really simple newbie question....I am using PHP 4.4 with a Drupal front end. I am not sure if I am calling something that doesn't work with PHP 4.4, but the code actually does work; it just generates this continuous error in my logs: "Invalid argument supplied for foreach() on line..." and the line in question is:

<?php
foreach ($GLOBALS['taximages'] as $image){
print $image;

}?>

 

I define this variable in a separate file with this code:

<?php if (module_exist("taxonomy_image")) {
       foreach (taxonomy_node_get_terms($vars['node']->nid) as $term) {
        $GLOBALS['taximages'][$vars['node']->nid]=taxonomy_image_display($term->tid,"border='0'", "alt='$term->name'");
        $vars['taxonomy_images'][] = taxonomy_image_display($term->tid, "border='0'", "alt='$term->name'");
       }
    }
}
return $vars;?>

 

Can anyone shed some light on why I get this foreach error? Much appreciated in advance,

sw

I've never used Drupal But I am sure that $GLOBALS['taximages'] is not an array.

Test it using

echo 'Datatype of \$GLOBALS[\'taximages\'] is : '.gettype($GLOBALS['taximages']).'Value : '.$GLOBALS['taximages'];

insert that code just befor the foreach loop starts.

I get this when I post in that code: Datatype of \$GLOBALS['taximages'] is : arrayValue : Array

 

So it is an array (as far as I know), except that I've realized I actually DON'T want to print a foreach. I only want ONE result (the first result found in the array). However, when I try this, nothing displays:

 

<?php
print $GLOBALS['taximages'][1]
?>

 

I get slightly confused with arrays, so I also tried this:

<?php
print $GLOBALS['taximages'][0]
?>

 

And I also tried using session variables as suggested, but again nothing displays if I just want to display one.

Any help?

 

Thanks,

sw

Thank you; that makes sense. Except that I don't want to reference any specific piece of the array in this code because it's dynamically pulling in an image based on what section of my site it's on. So I can't use the direct reference.

 

So I think what I need to do is translate this code to not pull an array, but to just pull a single variable each time.

<?php    
foreach (taxonomy_node_get_terms($vars['node']->nid) as $term) {
        $GLOBALS['taximages'][$vars['node']->nid]=taxonomy_image_display($term->tid,"border='0'", "alt='$term->name'");
        $vars['taxonomy_images'][] = taxonomy_image_display($term->tid, "border='0'", "alt='$term->name'");
       }
    }
}
return $vars;?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.