Jump to content

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;?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.