[英]how to loop subcategories with their categories?
編輯:好的,讓我更清楚一點,我有一個html文件名file.html,它是這樣的:
{category} {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 = new template();
$ template-> add_file('file.html');
$ template-> file_vars(array('category'=> $ row_cat ['title'],'subcategory'=> $ row_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;
}
}
這將為您提供一個數組數組。 第一個數組包含類別標題作為鍵,並且它們將包含所有子類別標題的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.