sw9 Posted May 19, 2007 Share Posted May 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/ Share on other sites More sharing options...
MadTechie Posted May 19, 2007 Share Posted May 19, 2007 register_globals are probably off try using sessions insead Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257002 Share on other sites More sharing options...
neel_basu Posted May 19, 2007 Share Posted May 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257021 Share on other sites More sharing options...
sw9 Posted May 19, 2007 Author Share Posted May 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257073 Share on other sites More sharing options...
neel_basu Posted May 19, 2007 Share Posted May 19, 2007 If its really an array try this code: print_r($GLOBALS['taximages']); Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257077 Share on other sites More sharing options...
sw9 Posted May 19, 2007 Author Share Posted May 19, 2007 Thanks for helping me on this...This works in that it prints out the image correctly, but it also puts this code in front of the image Array ( [6] => Why does this code show up? Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257087 Share on other sites More sharing options...
neel_basu Posted May 19, 2007 Share Posted May 19, 2007 Show the HTML Code generated by that code [print_r()] use echo '<code>'; print_r($GLOBALS['taximages']); echo '</code>'; Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257089 Share on other sites More sharing options...
chronister Posted May 19, 2007 Share Posted May 19, 2007 when you use print_r, it prints the array with the keys. The [6] => is the key for that particular image. This is standard when you use a print_r. If you use $GLOBALS['taximages'][6], you will be referencing this image directly. Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257090 Share on other sites More sharing options...
sw9 Posted May 19, 2007 Author Share Posted May 19, 2007 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;?> Quote Link to comment https://forums.phpfreaks.com/topic/52116-really-big-newbie-question-foreach-errors/#findComment-257110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.