SMARTY 二次循环的请问

WBOY
Release: 2016-06-13 12:04:05
Original
899 people have browsed it

SMARTY 二次循环的请教

本帖最后由 sky94132003 于 2014-08-06 03:20:32 编辑
<br />//所有版块<br />@$row_fid = "SELECT * FROM `forum_forum` ORDER BY `displayorder` ASC";<br />$stmt = $pdo->prepare($row_fid);<br />$stmt->execute();<br />$row_forum = $stmt->fetchAll(PDO::FETCH_ASSOC);<br /><br />	//主题分类<br />	foreach ($row_forum as $key => $value) {<br />		$fid = $value["fid"];<br />		@$sql = "SELECT `typeid`,`name` FROM `forum_topic_type` WHERE `fid` = $fid";<br />		$stmt = $pdo->prepare($sql);<br />		$stmt->execute();<br />		$row_type = $stmt->fetchAll(PDO::FETCH_ASSOC);<br />	}<br />		print_r($row_type);<br />$smarty -> assign("all_forum",$row_forum);<br />
Copy after login

代码是给各位看现在的逻辑 但没成功

学会SMARTY时间不长

有两张表
forum_forum 是讨论版块ID 主要是`name`,`fid`
forum_topic_type 是讨论版块的主题分类ID 主要是`typeid`, `name`,`fid`

本来
我是先把
forum_forum 列出所有版块,然后加到smarty的assign

成功在前台的所有版块列表中,显示出来

但想在版块名称的下方,把此讨论版块的旗下所有「主题分类」都循环出来,

但发现,这些东西(这张表forum_forum )已在前台循环了,我无法取得fid 来循环 相关的主题分类,因为循环结果的ID已经在模板中才能取得

所以想了想...在smarty的assign之前,试试foreach一下有什么结果,

然后试试看能不能合并成同一个组数再assign,发觉有点不行

所以想请教一下各位前辈


己想过的解决方法

1. 合并多表查询,但是主题分类也不只一个...所以就放弃这方法

2. 做个smarty 插件,已试了,但是也是卡住了不能前进

其实SMARTY这卖二次循环问题已困扰很久,但好像不能再逃避,虚心请教,希望彻底了解这类问题

求指教!
------解决方案--------------------
//所有版块<br />@$row_fid = "SELECT * FROM `forum_forum` ORDER BY `displayorder` ASC";<br />$stmt = $pdo->prepare($row_fid);<br />$stmt->execute();<br />$row_forum = $stmt->fetchAll(PDO::FETCH_ASSOC);<br /> <br />    //主题分类<br />    foreach ($row_forum as $key => $value) {<br />        $fid = $value["fid"];<br />        @$sql = "SELECT `typeid`,`name` FROM `forum_topic_type` WHERE `fid` = $fid";<br />        $stmt = $pdo->prepare($sql);<br />        $stmt->execute();<br />        $row_forum[$key]['list'] = $stmt->fetchAll(PDO::FETCH_ASSOC);<br />    }<br />        print_r($row_forum);<br />$smarty -> assign("all_forum",$row_forum);
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template