有没有更好的方法根据数据库列中的信息显示多个表? [英] Is there a better way to display multiple tables based on information in Database column?

查看:79
本文介绍了有没有更好的方法根据数据库列中的信息显示多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我创建了一个页面,执行3个非常相似的查询,以便检索属于某个特定的线程(在讨论板中)主题。



在我的数据库中,我有一个名为threadTopic的列,它指定表中线程所属的主题。所以当我举例来说,SELECT * FROM threads WHERE threadTopic = generalDiscussion时,它只会检索与generalDiscussion主题相关的线程;然后,我可以每次为每个threadTopic选项执行此过程(总共将有5个),然后可以在他们自己的单独表中显示。







我的问题不是寻找我所做的任何代码的修正,因为它工作正常,但更多的是最佳实践的问题,因为我做的方式感觉很乱,低效,耗时且便宜。



是否有一种技术可以使用foreach来获取每个threadTopic然后显示主题中的所有线程以便我将从一个查询结束5个表?



这是我重复的查询,使用不同的SQL查询来匹配每个threadTopic:



  $ query  = ' < span class =code-string> SELECT * FROM threads WHERE threadTopic =general'; 
$ output = $ db-> query($ query);
$ results = $ output-> fetchAll(PDO :: FETCH_ASSOC);
if (count($ results)> 0){
echo < span class =code-string> < table> \ n;
echo < tr>
< th>一般性讨论< / th>
< th>回复< / th>
< th>上次活动< / th>< / tr> \ n;
foreach($ results as $ result){
// 示例回复金额生成器
$ rand = rand(000,999);
echo < tr> \ N;
echo < td> $ result [threadName] LT; / TD>中;
echo < td> $ rand< ; / TD GT&;;
echo < td> $结果[lastActive]< br>按:< a href = \#\> $ result [threadBy]< / a>< / td>;
}
echo < ; / TR> \\\
;
echo < / table> ;
}

解决方案

查询 = ' SELECT * FROM threads WHERE threadTopic =general';


output =


db-> query(

Hi there,

I have created a page that executes 3 very similar queries in order to retrieve the threads (in a discussion board) that belong to a certain topic.

In my database, I have a column called threadTopic that specifies which topic the thread in the table belongs to. So when I for example, SELECT * FROM threads WHERE threadTopic=generalDiscussion, it will only retrieve the threads related to the generalDiscussion topic; which can then be displayed in their own seperate table.

However, I have to do this process every time for every threadTopic option (there will be 5 altogether).

My question isnt looking for correction on any code I have done as it works fine, but more a question of best practice as the way I have done it feels messy, inefficient, time-consuming and cheap.

Is there a technique where I can use a foreach to get each threadTopic and then display all the threads in the Topic so that I will end with 5 tables from one query?

This is the query I am repeating, with a different SQL query to match each threadTopic:

$query = 'SELECT * FROM threads WHERE threadTopic="general"';
				$output = $db->query($query);
				$results = $output->fetchAll(PDO::FETCH_ASSOC);
				if (count($results) > 0) {
					echo "<table>\n";
					echo "<tr>
					<th>General Discussion</th>
					<th>Replies</th>
					<th>Last activity</th></tr>\n";
					foreach($results as $result){
					//example replies amount generator
                                        $rand = rand(000, 999);
						echo "<tr>\n";
						echo "<td>$result[threadName]</td>";
						echo "<td>$rand</td>";
						echo "<td >$result[lastActive]<br>By: <a href=\"#\">$result[threadBy]</a></td>";
						}
						echo "</tr>\n";
						echo "</table>";
					}

解决方案

query = 'SELECT * FROM threads WHERE threadTopic="general"';


output =


db->query(


这篇关于有没有更好的方法根据数据库列中的信息显示多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆