简体   繁体   English

显示数据库中的类别

[英]Displaying categories from database

I'm trying to display categories for each post on an HTML table, and it's not giving any errors. 我正在尝试在HTML表格上显示每个帖子的类别,并且没有给出任何错误。

Well, the query is good. 好吧,查询很好。 I didn't find any kind of misspelled variables. 我没有发现任何拼写错误的变量。

我在类别侧获得了职位状态

<?php
$query = "SELECT * FROM  posts";
$se_posts = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($se_posts)) {
    $post_id = $row['post_id']; 
    $post_author = $row['post_author']; 
    $post_title = $row['post_title']; 
    $post_category_id = $row['post_category_id']; 
    $post_status = $row['post_status']; 
    $post_image = $row['post_image']; 
    $post_tags = $row['post_tags']; 
    $post_comment_count = $row['post_comment_count']; 
    $post_date = $row['post_date']; 

    echo "<tr>";
    echo "<td>{$post_id}</td>";
    echo "<td>{$post_author}</td>";
    echo "<td>{$post_title}</td>";

    // show post category
    $query = "SELECT * FROM categories WHERE cat_id Like $post_category_id";
    $query_update_cat = mysqli_query($connection, $query);

    while ($row = mysqli_fetch_assoc($query_update_cat)) {
        $cat_id = $row['cat_id'];  
        $cat_title = $row['cat_title'];

        echo "<td>{$cat_title}</td>";
    }

    echo "<td>{$post_status}</td>";
    echo "<td><img class='img-responsive' width='100'src='../images/{$post_image}'></td>";
    echo "<td>{$post_tags}</td>";
    echo "<td>{$post_comment_count}</td>";
    echo "<td>{$post_date}</td>";
    echo "<td><a href='post.php?delete={$post_id}'>delete</a></td>";
    echo "<td><a href='post.php?source=edit_post&p_id={$post_id}'>Edit</a></td>";

    echo "</tr>";
}  
?>

there is a problem that you are making a request on every post (or article). 您在每个帖子(或文章)上都提出了一个问题。 And this is wrong, you should reduce the number of requests as much as you can. 这是错误的,您应尽可能减少请求的数量。 Also, you are using LIKE !! 另外,您正在使用LIKE! you should use equal '='. 您应该使用等于“ =”。

for your query: 用于您的查询:

at this inside the categories loop you should define $cat_title before the loop, then set it in the loop, and outside the loop print it : 在category循环内部,您应该在循环之前定义$ cat_title,然后在循环中进行设置,并在循环外部打印它:

$cat_title = "";
while($row = mysqli_fetch_assoc($query_update_cat)){
    $cat_id = $row['cat_id'];  
    $cat_title = $row['cat_title'];
}
echo "<td>{$cat_title}</td>";

try with this query with JOIN: 尝试使用JOIN进行以下查询:

<?php
$query = "SELECT * FROM  posts JOIN categories ON (posts.post_category_id = categories.cat_id)";

$se_posts= mysqli_query($connection,$query);
?>
<table>
<tr>
    <td>post id</td>
    <td>post author</td>
    <td>post title</td>
    <td>category</td>
    <td>post status</td>
    <td>image</td>
    <td>tags</td>
    <td>comment count</td>
    <td>date</td>
    <td>action</td>
</tr>
<?php
while($row = mysqli_fetch_assoc($se_posts)) {
    echo "<tr>";
    echo "<td>{$row['post_id']}</td>";
    echo "<td>{$row['post_author']}</td>";
    echo "<td>{$row['post_title']}</td>";
    echo "<td>{$row['cat_title']}</td>";
    echo "<td>{$row['post_status']}</td>";
    echo "<td><img class='img-responsive' width='100'src=\"../images/{$row['post_image']}\"></td>";
    echo "<td>{$row['post_tags']}</td>";
    echo "<td>{$row['post_comment_count}</td>";
    echo "<td>{$row['post_date']}</td>";
    echo "<td><a href=\"post.php?delete={$row['post_id']}\">delete</a></td>";
    echo "<td><a href=\"post.php?source=edit_post&p_id={$row['post_id']}\">Edit</a></td>";
echo "</tr>";
}
?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM