homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP - problem printing array with 'foreach'
not sure how to use multiple values with 'foreach', an alternative?
Soupisgoodfood




msg:1250411
 10:37 am on Jul 17, 2001 (gmt 0)

Hi. I'm trying to get the code below to echo a list of images using an array. But I can only get it to work with one varible (the name). So I end up with width="Array" etc...

I've tried foreach ($images as $image, $widths as $width, $lefts as $left) (and retrospectivly changed $width = array... to $widths = array... etc.)
But it says it has too many parimiters, or something like that.

Should I be doing this another way? Like using 'while' (which I don't know how to use).

<?php

$images = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
$width = array('25', '25', '26', '26', '26', '27', '25', '27', '26', '25');
$left = array('31', '56', '81', '107', '133', '159', '186', '211', '238', '264');

foreach ($images as $image) {
echo '<a href=""><img src="pix/m_'.$image.'.gif" width="'.$width.'" height="16" id="'.$image.'" style="border: 0px; position:absolute; left:'.$left.'px; top:6px;" /></a>'."\n\n";

}
?>

Thanks,
Justin.

 

sugarkane




msg:1250412
 10:55 am on Jul 17, 2001 (gmt 0)

Assuming all your arrays have the same number of elements, a way of doing it would be something like:

$num_elements=count($image);
for ($i=0; $i < $num_elements; $i++) {
echo("<img src=\"$image[$i]\" width=\"$width[$i]\" height=\"16\">");
}

That basically increments the variable $i which you then use as an index to the array eg $image[$i]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved