michalchojno Posted July 23, 2009 Share Posted July 23, 2009 I seem to have a problem to display db data using Smarty's {section} function. Here is what I have: table1: id 1 3 4 14 15 index.php require('Smarty.class.php'); $smarty = new Smarty; $smarty->template_dir = 'c:...'; //use the correct path $smarty->config_dir = 'c:...'; //use the correct path $smarty->cache_dir = 'c:...'; //use the correct path $smarty->compile_dir = 'c:...'; //use the correct path $link = mysql_connect ('host', 'user', 'pass'); // use the right input data $db = mysql_select_db('db', $link); // use the right input data $q = "Select * from table1 order by id"; $r = mysql_query ($q, $link); while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { $arrid[($row['id'])] = $row['id']; } $smarty -> assign ( array( 'Itemid' => $arrid ) ); $smarty->display('index.tpl'); mysql_free_result ($r); mysql_close ($link); index.tpl ...... {section name=listt loop=$Itemid} <tr> <td>{$Itemid[listt]}</td> </tr> {/section} ....... Now this generates something like this: 1 - 3 4 - I'd like the result to be: 1 3 4 14 15 How can I change that? Where did I make a mistake? Quote Link to comment Share on other sites More sharing options...
btherl Posted July 24, 2009 Share Posted July 24, 2009 Try smarty's "foreach" instead of section. That will loop only over keys which exist in the array. Quote Link to comment Share on other sites More sharing options...
michalchojno Posted July 24, 2009 Author Share Posted July 24, 2009 Yes! How simple... Many thanks. The code below does the trick. index.tpl {foreach from=$Itemid item=vvv} <tr> <td>{$vvv}</td> </tr> {/foreach} SOLVED Quote Link to comment 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.