开发者

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开发者_如何学Python you know how to do it then let me know.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜