简体   繁体   中英

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:

{category} {sybcategory}

and i have a index file which is like this:

class template { 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->add_file('file.html');

$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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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