我正在一个有类别的网站上工作。当用户单击链接时,它会转到categories.php?id=$id
这是有效的。由于我使用的是 FK,因此我必须使用以下代码:
$result = mysql_query( "SELECT * FROM post_posts, post_cat WHERE post_cat = $id" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
if ($result) {
while ($row = mysql_fetch_array($result)) {
$_SESSION['idpost_posts']=$row['idpost_posts'];
echo '<div id="single-post" class="slidingDiv">
<div id="title">' . $row['post_header'] . '</div>
<div id="image"><img src="' . $row['post_thumb'] . '" width="300" height="200"></div>
<div id="text">' . substr($row['post_content'], 0, 500) . '<a href="page.php?id=' . $row['idpost_posts'] . '"> Les mer...</a>' . ' </div>
<div id="author">' . "Skrevet: " . date('j. F Y ', strtotime($row['post_date'])) . ' | <a href="user.php?id=' . $row['idpost_users'] . '">' . $row['user_name'] . '</a></div>
<div id="post-divider"></div>
</div>';
}
}
该代码正在运行,但正如我之前所说,它显示同一行 5 次。知道如何解决这个问题吗?
编辑:这是我现在使用的,现在只显示两行,这更好:
$result = mysql_query( "SELECT DISTINCT idpost_posts, post_content, post_header, post_thumb, post_date, idpost_users, user_name FROM post_posts, post_cat, post_users WHERE post_cat = $id" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);if ($result) {while ($row = mysql_fetch_array($result)) {
$_SESSION['idpost_posts']=$row['idpost_posts'];
echo '<div id="single-post" class="slidingDiv">
<div id="title">' . $row['post_header'] . '</div>
<div id="image"><img src="' . $row['post_thumb'] . '" width="300" height="200"></div>
<div id="text">' . substr($row['post_content'], 0, 500) . '<a href="page.php?id=' . $row['idpost_posts'] . '"> Les mer...</a>' . ' </div>
<div id="author">' . "Skrevet: " . date('j. F Y ', strtotime($row['post_date'])) . ' | <a href="user.php?id=' . $row['idpost_users'] . '">' . $row['user_name'] . '</a></div>
<div id="post-divider"></div>
</div>';