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? Link to comment https://forums.phpfreaks.com/topic/167194-solved-phpsmarty-problem-with-displaying-db-data-using-smartys-section-function/ 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. Link to comment https://forums.phpfreaks.com/topic/167194-solved-phpsmarty-problem-with-displaying-db-data-using-smartys-section-function/#findComment-881646 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 Link to comment https://forums.phpfreaks.com/topic/167194-solved-phpsmarty-problem-with-displaying-db-data-using-smartys-section-function/#findComment-881713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.