Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将子类别与其类别循环?_Php_Mysql_Html_Templates - Fatal编程技术网

Php 如何将子类别与其类别循环?

Php 如何将子类别与其类别循环?,php,mysql,html,templates,Php,Mysql,Html,Templates,编辑:好的,让我更清楚一点,我有一个html文件名file.html,它是这样的: {类别} {sybcategory} 我有一个索引文件,如下所示: 类模板 { var$page function add_file($file){ $this->page = file_get_contents($file); return $this->page; } function vars($array) { foreach($array as $key =>

编辑:好的,让我更清楚一点,我有一个html文件名file.html,它是这样的:

{类别} {sybcategory}

我有一个索引文件,如下所示:

类模板 { var$page

function add_file($file){
    $this->page = file_get_contents($file);
    return $this->page;
}

function vars($array)
{
    foreach($array as $key => $value)
    {
        $this->page = str_replace('{'.$key.'}', $value, $this->page);
    }
    echo $this->page;
}
}

$template=新模板

$template->add_文件'file.html'

$template->file_varsarray'category'=>$row\u cat['title'],'subcategory'=>$row\u subcat['title']


现在,这只输出标题和子类别一次,我如何循环它,我没有在这里添加mysql查询,因为我不想浪费你更多的时间,如果你知道怎么做,请告诉我。

如何将所有内容存储在你有的时间内

while (mysql_fetch_assoc($mycategory)){

  $catsArray[] = $cat_title;

  while (mysql_fetch_assoc($mysubcategory)){

   $catsArray[] = $subcat_title;

  } 
}
这样,您将有一个单一的数组,其中所有的猫后跟它们的子猫

希望对你有所帮助:

编辑:我想我误解了你的问题,我回答的内容包括如何对它们进行分组,但你最后要问的是Smarty这样的模板引擎,它不能在Stackoverflow中用一个单一的答案来概括,你需要先构建引擎,然后才能使用类似{catArray}的东西。

给你:

$cats = array();

while (mysql_fetch_assoc($mycategory))
{    
    $cats[$cat_title] = array();

    while (mysql_fetch_assoc($mysubcategory))
    {
        $cats[$cat_title][]  $subcat_title;
    }
}

这将为您提供一个数组。第一个数组包含类别标题作为键,它们将包含所有子类别标题的数组。

为什么要删除while loops标记?它比mysql更相关。感谢您的更新-这完全取决于您使用的模板系统是什么?机智吗?不,我自己做了一个模板引擎是的,我只学会了如何接受上一个问题的答案。不管怎样,我认为你没有得到我想要问的,让我把它编辑一下,并正确地解释。