模板类不使用 str_replace 循环

发布于 2024-12-08 20:53:13 字数 1268 浏览 0 评论 0原文

好吧,我有这组代码

<?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();

?>

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失眠症患者 2024-12-15 20:53:13

您正在覆盖 foreach() 循环内的变量 $page ;要么将其设为数组,要么将其附加到变量中。可能是:

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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文