[英]How to sort and display database results with MySQL/PHP
I am using PHP to sort items on my website into different locations/different divs. 我正在使用PHP将网站上的项目分类到不同的位置/不同的div中。 I have a script in each div where I want the results to be displayed So if there are 4 divs there are 4 scripts.
我在每个div中都有一个脚本,希望在其中显示结果,因此,如果有4个div,则有4个脚本。 (4 categories) Each script has a query in the head of my page.
(4类)每个脚本的页面顶部都有一个查询。
MySQL query for category/div 1 MySQL查询类别/ div 1
$sql_category1 = <<<SQL
SELECT *
FROM `list_items`
WHERE `category` = 1
ORDER BY `rating` ASC;
SQL;
if(!$category1 = $db->query($sql_category1 )){
die('There was an error running the query [' . $db->error . ']');
}
My PHP for category/div 1: 我的类别/ div的PHP 1:
<?php
while($row = $category1->fetch_assoc()) {
'<div class="list-item-container">'
. '<a href="' . $row['url'] . '"><img title="' . $row['title'] . '" class="favicon" src="http://www.google.com/s2/favicons?domain='. $row['fav_url'] . '" /></a>'
. '</div>';
}
?>
My goal: When i am entering data into the table and there are two items that can be classified as two different things, so two different categories, I have to make two copies of it. 我的目标:当我在表中输入数据时,有两个项目可以归为两个不同的事物,因此有两个不同的类别,我必须制作两个副本。 This way a item will appear in both categories.
这样,一个项目将同时出现在两个类别中。 Same item two different categories.
同一项目有两个不同的类别。 I give one of the items a category value of 1 and the other item a category value of 2.
我给其中一个项目的类别值为1,将另一个项目的类别值为2。
Rather than making two separate entries of the same item is there a way that I can give that item two separate category values? 除了可以为同一项目输入两个单独的条目之外,还有一种方法可以为该项目提供两个单独的类别值? This way the item will appear in both categories when my scripts run and sort them.
这样,当我的脚本运行并对其进行排序时,该项目将同时出现在两个类别中。
I tried 1,2 1:2 and 1;2 as the value of the item. 我尝试了1,2 1:2和1; 2作为项目的值。 Tho im sure it's not even close to that.
我确信这还差得远。
Thanks in advance! 提前致谢!
make 3 tables: 制作3张桌子:
category (id, name, ...) 类别(ID,名称,...)
item (id, name, ...) 项目(ID,名称,...)
item_category (id_item, id_category) item_category(id_item,id_category)
them make join of them in your queries, eg your first query will become 他们将它们加入您的查询中,例如您的第一个查询将变为
SELECT *
FROM `list_items` t,`item-category`
WHERE `id_category` = 1
and `id_item` = t.`id`
ORDER BY `rating` ASC;
After you have connection table you can put in it whatever connections you want 连接表建立后,您可以在其中放入所需的任何连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.