我正尽力做好相关链接。所以我有一个链接,该链接转到一个名为post.php的页面,并显示数据库中的正确帖子。现在我想在类别等于该帖子的另一个div中回显所有标题(及其链接)吗?
例如,我要显示的帖子是表中的第一条记录。它应该显示类别1的所有标题。但是,如果我显示的是类别2的页面,那么还将显示类别2的其他标题。
代码我已将页面回显到post.php:
<?php
require_once("db/db.php");
$sql = $db->prepare("
SELECT *
FROM mytable
WHERE slug=?
");
$sql->bind_param("s",$_GET["slug"]);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
}
?>
<h2><?php echo $row['mytitle'];?></h2>
<?php echo $row['mypost'];?>
+----+-----------------------------+-------------------------------+----------+------------------------+
| id | mytitle | mypost | category | slug |
+----+-----------------------------+-------------------------------+----------+------------------------+
| 1 | title of apost cat1 | it amet, sollicitudin euismod | 1 | my-first-post |
+----+-----------------------------+-------------------------------+----------+------------------------+
| 2 | post of title cat1 | quam tempor mauris elem | 1 | post-of-title |
+----+-----------------------------+-------------------------------+----------+------------------------+
| 3 | Fun title Cat2 | verra mollis justo eget ti | 2 | fun-title |
+----+-----------------------------+-------------------------------+----------+------------------------+
| 4 | What about another one Cat2 | et consequat tortor et vive | 2 | what-about-another-one |
+----+-----------------------------+-------------------------------+----------+------------------------+
答案 0 :(得分:0)
您可以尝试
SELECT t1.mytitle,
t1.mypost,
t2.titles
FROM mytable AS t1
INNER JOIN
(SELECT group_concat(mytitle) AS titles,
category
FROM mytable
GROUP BY category) AS t2 ON t1.category=t2.category
WHERE t1.id=1
标题是与该特定帖子类别相关的所有标题。
答案 1 :(得分:0)
您可以通过$row['category']
获取当前的帖子类别并运行相关查询:
$sql = $db->prepare("
SELECT *
FROM mytable
WHERE category=? AND id NOT IN (?)
LIMIT 5
");
$sql->bind_param("i",$row['category']);
$sql->bind_param("i",$row['id']);
然后将结果循环到单独的div中。我认为最好通过ajax调用。