不知道这是一个结构问题或不是,请确定如果我错了, 确定我有一个数组(通过使用foreach)不显示相同的标签?在PHP
foreach ($db['products'] as $product)
{
echo '<li><a href="">' . $product['tag'] . '</a></li>';
}
// displays like
tag1
tag4
tag2
tag2
tag1
tag3
tag1
tag1
tag3
tag2
tag3
tag2
做的人有一个想法如何显示
//count tags & no duplicate tags ?
tag1(4)
tag2(4)
tag3(3)
像stackoverflow有?
编辑一个(PDO)
$database->fetchall("SELECT pid, uid, name, information, image_tumb, tag, price
FROM products ORDER BY pid DESC LIMIT 100");
编辑两个当我做到这一点
$db['products'] = $database->fetchall("SELECT pid, uid, name, information, image_tumb, tag, price
FROM products GROUP BY tag ORDER BY pid DESC LIMIT 100");
它的工作,但有些产品没有显示。
- 所有产品显示
- 但标签就像上面。
感谢您的时间btw,它节省了我的学习时间很多。
编辑*它的工作。谢谢所有
$db['products'] = $database->fetchall("SELECT pid, uid, name, information, image_tumb, price
FROM products ORDER BY pid DESC LIMIT 100");
$db['tags'] = $database->fetchall("SELECT tag, COUNT(tag) AS counter
FROM products GROUP BY tag ORDER BY tag DESC LIMIT 100");
foreach ($db['tags'] as $tag) {
echo '<li><a href="">' . $tag['tag'] . '(' . $tag['counter'] . ')</a></li>';
}
买这样做我仍然得到我所有的产品与一个标签列表没有重复+计数器。再次感谢!。
这可以通过更改您的数据库查询轻松实现。你能展示代码的那一部分吗? – riwalk 2010-12-04 18:13:56
编辑好了,感谢 – 2010-12-04 18:29:52