模板类不与str_replace一起循环 [英] Template class not looping with str_replace

查看:145
本文介绍了模板类不与str_replace一起循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?php 
includeincludes / config.php ;
类模板{
var $ page;
var $ built;
public $ block = array();

函数_start($ tpl){
$ this-> page = $ tpl;


函数set_array($ data){
$ this-> block [] = $ data;


foreach($ v $ foreach $ $ $ $ foreach($ this-> block as $ k => $ v){
foreach k1 => $ v1){
// echo $ k1。< br />;
// echo $ v1。< br />;
$ this-> page = str_replace({。$ k1。},$ v1,$ this-> page);
}
}
echo $ this-> page;
}
}

$ template = new template();
$ b $ file =< html>
< body>
< p> {CAT}< / p>
< p> {SUBCAT }< / p>
< / body>
< / html>;

$ template-> _start($ file);

//类别查询
while($ row1 = mysql_fetch_assoc($ cat)){

$ template-> set_array(array(CAT=> ; $ row1 ['title']));

//子类别查询
while($ row2 = mysql_fetch_assoc($ subcat)){

$ template-> set_array(array(SUBCAT= > $ row2 ['title']));



$ b $ template-> _show();

?>

现在,当我回显$ k1或$ v1时,它们以正确的顺序显示键和值

CAT1
SUBCAT1.1
SUBCAT1.2
CAT2
SUBCAT2.1
SUBCAT2.2



但是当它通过str_replace时,它只显示CAT1和SUBCAT1.2出了什么问题?

解决方案

您正在覆盖foreach()循环中的变量$页面;要么让它成为一个数组,要么追加到变量中。可能是:

  function _show(){
$ this-> page ='';
foreach($ this-> block as $ k => $ v){
foreach($ v as $ k1 => $ v1){
$ this-> page 。= str_replace({。$ k1。},$ v1,$ this-> page); //每次追加到前一个。正如您所做的那样:
// $ this-> page = str_replace({。$ k1。},$ v1,$ this-> page);
//在这里你覆盖$ this->页面的每一个循环
}
}
echo $ this-> page;
}


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?

解决方案

You're overwriting the variable $page inside the foreach() loop; either you make it an array or you append to the variable. Could be:

function _show(){
       $this->page = '';
        foreach($this->block as $k => $v){
            foreach($v as $k1 => $v1){
                $this->page .= str_replace("{".$k1."}", $v1, $this->page); //appending every time onto the previous. As you were doing:
                // $this->page =str_replace("{".$k1."}", $v1, $this->page);
                // here you were overwriting $this->page at every passage of the loop
            }
        }
        echo $this->page;
    }

这篇关于模板类不与str_replace一起循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆