RE: [SMARTY] trouble with arrays in template

From: Date: Thu, 24 Jul 2003 01:33:23 +0000
Subject: RE: [SMARTY] trouble with arrays in template
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-9953@lists.php.net to get a copy of this message
-----Original Message----- From: Erich Beyrent [mailto:erich.beyrent@beyrent.net] Sent: Wednesday, July 23, 2003 6:27 PM To: smarty-general@lists.php.net Subject: RE: [SMARTY] trouble with arrays in template > Hi there, > > I am trying to pass in the following array to my template: > > $gallery_dir = $DOCUMENT_ROOT.'/photos/gallery/'; > $smarty->assign('PhotoArray', > array('link' => $gallery_dir.'yellow/Bikes0.jpg'), > array('link' => $gallery_dir.'yellow/Bikes1.jpg'), > array('link' => $gallery_dir.'yellow/Bikes2.jpg'), > array('link' => $gallery_dir.'yellow/Bikes3.jpg'), > array('link' => $gallery_dir.'yellow/Bikes3.jpg'), > array('link' => $gallery_dir.'yellow/Bikes3.jpg') > ); > > And then in my template, loop through them: > > <!-- Template code --> > <table border="0" cellspacing="0" cellpadding="5"> > {section name=i loop=$PhotoArray} > <tr> > <td><img src="{$PhotoArray[i].link}"</td> > </tr> > {/section} > </table> > > > > However, none of the images are loading, and none of the template code > is showing up at all. > > What have I done (or not done), and how do I fix it? > > Thanks for your help! > > -Erich- > > You need a numerically indexed array. Just do: > > $smarty->assign( 'PhotoArray', array( $gallery_dir.'yellow/Bikes0.jpg', >... $gallery_dir.'yellow/Bikes3.jpg' )); > > and dispense with the "link": > > <img src="{$PhotoArray[i]}"> > > You forgot the closing angle bracked on your IMG tag, btw. > > cheers, > > Travis OK - I got this to work: --- PHP Code --- $smarty->assign('PhotoArray', array('links' => array($gallery_dir.'photos/gallery/yellow/Bikes0.jpg', $gallery_dir.'photos/gallery/yellow/Bikes1.jpg', $gallery_dir.'photos/gallery/yellow/Bikes1.jpg', $gallery_dir.'photos/gallery/yellow/Bikes2.jpg', $gallery_dir.'photos/gallery/yellow/Bikes3.jpg', $gallery_dir.'photos/gallery/yellow/Bikes4.jpg', $gallery_dir.'photos/gallery/yellow/Bikes5.jpg'), 'alt' => array('1','2','3','4','5'), )); --- Template Code --- <!-- Start of photos loop --> <table border="0" cellspacing="0" cellpadding="5"> {section name=p loop=$PhotoArray.links} <tr> <td> <table border=0 cellpadding=0 cellspacing=0 bgcolor="black" width="100%" align="left"> <tr> <td> <table border=0 cellpadding=2 cellspacing=2 width="100%" align="left"> <tr> <td align="center"><a href="{$PhotoArray.links[p]}"><img src="{$PhotoArray.links[p]}" border=0></a></td> <td align="center"><a href="{$PhotoArray.links[p.index_next]}"><img src="{$PhotoArray.links[p.index_next]}" border=0></a></td> </tr> </table> </td> </tr> </table> </td> </tr> {/section} </table> <!-- End of photos loop --> So as you might be able to tell, I am trying to get two images per row by iterating through my $PhotoArray variable. Unfortunately, it displays the same image back to back. How do I get the loop variable "p" to increment across the rows? -Erich0

« previous php.smarty.general (#9953) next »