简体   繁体   English

如何用子类别循环子类别?

[英]how to loop subcategories with their categories?

EDIT: ok let me be a bit more clear i have a html file name file.html it is like this: 编辑:好的,让我更清楚一点,我有一个html文件名file.html,它是这样的:

{category} {sybcategory} {category} {sybcategory}

and i have a index file which is like this: 而且我有一个像这样的索引文件:

class template { var $page; 类模板{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 = new template(); $ template = new template();

$template->add_file('file.html'); $ template-> add_file('file.html');

$template->file_vars(array('category' => $row_cat['title'], 'subcategory' => $row_subcat['title'])); $ template-> file_vars(array('category'=> $ row_cat ['title'],'subcategory'=> $ row_subcat ['title'])));

now, this only outputs the title and the subcategory only once how do i loop that and i have not added the mysql query here because i don't wanna waste your more time please if you know how to do it then let me know. 现在,仅输出一次标题和子类别一次,如何循环该循环,并且我还没有在此处添加mysql查询,因为我不想浪费您更多的时间,如果您知道该怎么做,那就告诉我。

How about storing everything within those whiles you have there? 将所有内容存储在您那里的那一刻如何?

while (mysql_fetch_assoc($mycategory)){

  $catsArray[] = $cat_title;

  while (mysql_fetch_assoc($mysubcategory)){

   $catsArray[] = $subcat_title;

  } 
}

This way you'll have a single array with all the cats followed by their subcats. 这样,您将拥有一个阵列,其中所有的猫及其子猫都在其后。

Hope it helps a bit :) 希望能有所帮助:)

EDIT: I think I misunderstood your question, what I answered covers how you can group them, but what you're asking in the end is a template engine such as Smarty, which can't be "summarized" in a single answer here in Stackoverflow, you'd need to build the engine before you can use something like {catArray}. 编辑:我想我误解了您的问题,我回答的内容涵盖了如何将它们分组,但是最后您要问的是诸如Smarty之类的模板引擎,在此处无法在一个答案中对其进行“概括” Stackoverflow,您需要先构建引擎,然后才能使用{catArray}之类的东西。

Here you go: 干得好:

$cats = array();

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

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

This will give you an array of arrays. 这将为您提供一个数组数组。 The first array contains the category titles as keys and they will contain an array of all the sub category titles. 第一个数组包含类别标题作为键,并且它们将包含所有子类别标题的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM