SMARTY 二次循环的请教
//所有版块@$row_fid = "SELECT * FROM `forum_forum` ORDER BY `displayorder` ASC";$stmt = $pdo->prepare($row_fid);$stmt->execute();$row_forum = $stmt->fetchAll(PDO::FETCH_ASSOC); //主题分类 foreach ($row_forum as $key => $value) { $fid = $value["fid"]; @$sql = "SELECT `typeid`,`name` FROM `forum_topic_type` WHERE `fid` = $fid"; $stmt = $pdo->prepare($sql); $stmt->execute(); $row_type = $stmt->fetchAll(PDO::FETCH_ASSOC); } print_r($row_type);$smarty -> assign("all_forum",$row_forum);
代码是给各位看现在的逻辑 但没成功
学会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这卖二次循环问题已困扰很久,但好像不能再逃避,虚心请教,希望彻底了解这类问题
求指教!
回复讨论(解决方案)
另外也补充问个问题
我用SMARTY自己做 modifier.xxx.php 这类插件时,平时是常想顺利的
但为什么一用到数据库就完全不行?
我是用PDO的
因为没把$pdo这类句柄传入? 但是这种插件是由 模板直接调用,我根本就不能加入$pdo
我也试过加插件的function 内引入数据库文件,但也是表示数据库操作有误
请问有什么解决方案吗? 还是插件这部分根本不能操作数据库???
哪一步没成功?$row_type没值吗?
1.通过模板处理:类表 关联 子类表 得出结果集,在进行smarty的assign,模板循环2次 。
2.通过程序查询结果集处理:返回结果集,在程序组装成为字符串,直接assign传到模板。
//所有版块@$row_fid = "SELECT * FROM `forum_forum` ORDER BY `displayorder` ASC";$stmt = $pdo->prepare($row_fid);$stmt->execute();$row_forum = $stmt->fetchAll(PDO::FETCH_ASSOC); //主题分类 foreach ($row_forum as $key => $value) { $fid = $value["fid"]; @$sql = "SELECT `typeid`,`name` FROM `forum_topic_type` WHERE `fid` = $fid"; $stmt = $pdo->prepare($sql); $stmt->execute(); $row_forum[$key]['list'] = $stmt->fetchAll(PDO::FETCH_ASSOC); } print_r($row_forum);$smarty -> assign("all_forum",$row_forum);
大哥,谢谢你
就这意思
太感谢了,学到了
果然是逻辑问题
以后看来能解决大部分的二次循环问题了
谢谢

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



As the leading currency in the field of AI artificial intelligence, FET currency has attracted much attention due to its considerable return on investment. FET currency is not only a quantitative divisible token used by the Fetch.ai platform, but also the platform’s smart contracts and oracles. An important part of. With the arrival of the bull market, the price of FET coins continues to rise, but investors are still not satisfied with this market trend. They want to know how soon will FET coins experience a second surge? I would also like to know how much the currency circle analysts predict the maximum rise of FET coins? According to the predictions of analysts in the circle, the second surge will occur in 2025, with a maximum rise of $8.15. Next, the editor will tell you in detail. How soon will FET coins experience a second surge? According to the predictions of analysts in the circle, FET currency will explode for the second time.

Lambda expression breaks out of the loop, specific code examples are needed. In programming, the loop structure is an important syntax that is often used. However, in certain circumstances, we may want to break out of the entire loop when a certain condition is met within the loop body, rather than just terminating the current loop iteration. At this time, the characteristics of lambda expressions can help us achieve the goal of jumping out of the loop. Lambda expression is a way to declare an anonymous function, which can define simple function logic internally. It is different from an ordinary function declaration,

Note: This article compares loops and recursion from the perspective of Go language. When writing programs, you often encounter situations where a series of data or operations need to be processed repeatedly. To achieve this we need to use loops or recursion. Loops and recursions are both commonly used processing methods, but in practical applications, they each have advantages and disadvantages, so the actual situation needs to be considered when choosing which method to use. This article will conduct a comparative study of loops and recursion in the Go language. 1. Loops A loop is a mechanism that repeatedly executes a certain piece of code. There are three main types of Go language

This article will explain in detail how PHP returns all the values of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values in an array. It does not preserve the keys of the original array. $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values will be ["bar","qux"]Using a loop can Use a loop to manually get all the values of the array and add them to a new

Iterator interface The Iterator interface is an interface used to traverse collections. It provides several methods, including hasNext(), next() and remove(). The hasNext() method returns a Boolean value indicating whether there is a next element in the collection. The next() method returns the next element in the collection and removes it from the collection. The remove() method removes the current element from the collection. The following code example demonstrates how to use the Iterator interface to iterate over a collection: Listnames=Arrays.asList("John","Mary","Bob");Iterator

Replacement of recursive calls in Java functions with iteration In Java, recursion is a powerful tool used to solve various problems. However, in some cases, using iteration may be a better option because it is more efficient and less prone to stack overflows. Here are the advantages of iteration: More efficient since it does not require the creation of a new stack frame for each recursive call. Stack overflows are less likely to occur because stack space usage is limited. Iterative methods as an alternative to recursive calls: There are several methods in Java to convert recursive functions into iterative functions. 1. Use the stack Using the stack is the easiest way to convert a recursive function into an iterative function. The stack is a last-in-first-out (LIFO) data structure, similar to a function call stack. publicintfa

All programming languages are inseparable from loops. So, by default, we start executing a loop whenever there is a repeating operation. But when we are dealing with large number of iterations (millions/billions of rows), using loops is a crime. You might be stuck for a few hours, only to realize later that it doesn't work. This is where implementing vectorization in python becomes very critical. What is vectorization? Vectorization is a technique for implementing (NumPy) array operations on data sets. Behind the scenes, it applies the operation to all elements of the array or series at once (unlike a "for" loop that operates one row at a time). Next we use some use cases to demonstrate what vectorization is. Find the sum of numbers##Use the loop importtimestart

How to handle PHP loop nesting errors and generate corresponding error messages. During development, we often use loop statements to handle repeated tasks, such as traversing arrays and processing database query results. However, when using loop nesting, you sometimes encounter errors, such as infinite loops or too many nesting levels. This problem can cause server performance to degrade or even crash. In order to better handle such errors and generate corresponding error messages, this article will introduce some common processing methods and give corresponding code examples. 1. Use counters to
