Sim Posted August 24, 2009 Posted August 24, 2009 my code for a sectioned loop $data = array('name' => 'tom', 'phone' => '55a35-3425'); $data = array('name' => 'tommy', 'phone' => '55a35-3425'); $smarty->assign('users', $data); my template side <table> {section name=mysec loop=$users} {strip} <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}"> <td>{$users[mysec].name}</td> <td>{$users[mysec].phone}</td> </tr> {/strip} {/section} </table> any idea why it shows nothing? Quote
CrazyT Posted August 24, 2009 Posted August 24, 2009 Re: smarty templating system Try <table> {foreach from=$users name="users"} <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}"> <td>{$users.name|htmlspecialchars}</td> <td>{$users.phone}</td> </tr> {/foreach} </table> Quote
CrazyT Posted August 24, 2009 Posted August 24, 2009 Re: smarty templating system Wait.. shouldn't $data be like.. $data = array('name' => array('tom', 'tommy'), 'phone' => array('55a35-3425', '55a35-3425')); Here is a link.. http://smarty.net/crashcourse.php scroll down you will see. Quote
Sim Posted August 24, 2009 Author Posted August 24, 2009 Re: smarty templating system yeah I seen that. but i need to loop through records in a mySQL DB and I cna't really put them all into the array at once by doing that. Quote
CrazyT Posted August 24, 2009 Posted August 24, 2009 Re: smarty templating system Then you will do something like this.. <?php include_once 'smarty.configfile.php'; include_once 'somethingelse.php'; //this is a function i use for inside db class. function GetAll($query) { $rs = $this->Execute($query); if( !is_resource($rs) ) return false; $rows = array(); while ($row = mysql_fetch_assoc($rs)) { $rows[] = $row; } mysql_free_result($rs); return $rows; } //Then you do something like.. $users = $db->GetAll('SELECT * FROM `users`'); $smarty->assign('users', $users); <table> {foreach from=$users name="users"} <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}"> <td>{$users.name|htmlspecialchars}</td> <td>{$users.phone}</td> </tr> {/foreach} </table> Hope it helps.. XD Quote
CrazyT Posted August 24, 2009 Posted August 24, 2009 Re: smarty templating system Or maybe something like.. $users = array(); $sql = mysql_query('SELECT * FROM `users`;'); if ( mysql_num_rows($sq) > 0 ) { while ( $row = mysql_fetch_assoc($sql) ) $users[] = $row; } $smarty->assign('users', $users); But im not 100% sure tho. lol Quote
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.