Kload Posted October 10, 2011 Share Posted October 10, 2011 Well i have this set of codes <?php include "includes/config.php"; class template{ var $page; var $built; public $block = array(); function _start($tpl){ $this->page = $tpl; } function set_array($data){ $this->block[] = $data; } function _show(){ foreach($this->block as $k => $v){ foreach($v as $k1 => $v1){ //echo $k1."<br />"; //echo $v1."<br />"; $this->page = str_replace("{".$k1."}", $v1, $this->page); } } echo $this->page; } } $template = new template(); $file = "<html> <body> <p>{CAT}</p> <p>{SUBCAT}</p> </body> </html>"; $template->_start($file); // Category Query while($row1 = mysql_fetch_assoc($cat)){ $template->set_array(array("CAT" => $row1['title'])); // Sub Category Query while($row2 = mysql_fetch_assoc($subcat)){ $template->set_array(array("SUBCAT" => $row2['title'])); } } $template->_show(); ?> Now, when i echo $k1 or $v1 they display the keys and values in the correct order like CAT1 SUBCAT1.1 SUBCAT1.2 CAT2 SUBCAT2.1 SUBCAT2.2 but when it goes through the str_replace its only displays the CAT1 and SUBCAT1.2 what going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/248806-problem-with-template-class/ Share on other sites More sharing options...
sunfighter Posted October 10, 2011 Share Posted October 10, 2011 This line: while($row1 = mysql_fetch_assoc($cat)) $cat not defined Quote Link to comment https://forums.phpfreaks.com/topic/248806-problem-with-template-class/#findComment-1277820 Share on other sites More sharing options...
Kload Posted October 11, 2011 Author Share Posted October 11, 2011 Actually that is having the mysql query and configuration so i didn't posted how to fetch sql data and all its correct only. Quote Link to comment https://forums.phpfreaks.com/topic/248806-problem-with-template-class/#findComment-1278020 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2011 Share Posted October 11, 2011 You are overwriting the 'template' with the result of the str_replace(). After the first two iterations of the loops, the {CAT} and {SUBCAT} tags in the template don't exist. I recommend storing the output in a different variable than the actual 'template'. Quote Link to comment https://forums.phpfreaks.com/topic/248806-problem-with-template-class/#findComment-1278025 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.