简体   繁体   English

PHP SQL内部联接?

[英]Php SQL inner join?

So I'm making a portfolio website where I can post and edit blogs. 因此,我正在制作一个投资组合网站,可以在其中发布和编辑博客。

I'm making the blog edit page and I'm having trouble with combining two table together. 我正在制作博客编辑页面,但无法将两个表组合在一起。

The first table is for the blogs and the second table holds all the different blog categories. 一个表用于博客, 第二个表包含所有不同的博客类别。

Here's how I'm getting the blog posts: 这是我获取博客文章的方式:

$qStr = "SELECT post_title, post_content, post_description, post_active, category_id  FROM posts WHERE post_id = {$post_id}";

Here's how I'm getting the blog categories: 这是我获取博客类别的方法:

$qStr = "SELECT category_name FROM categories WHERE category_id = {$category_id}";

So in my edit blog post page I have a dropdown box that I need to show which category that blog post is in, and be able to change it. 因此,在我的编辑博客文章页面中,我有一个下拉框,我需要显示该博客文章所在的类别并进行更改。 I have a category_id in my blog table. 我的博客表中有一个category_id。 My question is how do I get the dropdown to show which category the post is under? 我的问题是如何获得下拉菜单以显示帖子所属的类别?

Right now my drop down code looks like this (note: Right now I'm just populating the dropdown with all the categories) 现在,我的下拉代码看起来像这样(注意:现在,我只是用所有类别填充下拉列表)

<div class="form-group">
     <label for="categorySelect" class="col-lg-2 control-label">Category</label>
     <div class="col-lg-3">
          <select class="form-control" id="categorySelect">
                <?php
                     foreach ($categories as $cat) {
                         echo("<option>{$cat['category_name']}</option>");   
                     }
                 ?>
           </select>
     </div>
</div>

You need to set up the selected attribute for select (option) element, something like this: 您需要为select(option)元素设置selected属性,如下所示:

<?php
    foreach ($categories as $cat) {
       if ($cat['category_name'] == $category_of_your_post)
           echo "<option selected=\"selected\">{$cat['category_name']}</option>"; 
       else  
           echo "<option>{$cat['category_name']}</option>"; 
    }
?>

BTW, I think that you have the wrong queries (if I understood correctly your question). 顺便说一句,我认为您的查询有误(如果我正确理解您的问题)。 Basically you need to retrieve the category_id of your post and perform an equality question against the array of categories: 基本上,您需要检索帖子的category_id并针对类别数组执行相等性问题:

$qStrBlog = "SELECT post_title, post_content, post_description, post_active, category_id  FROM posts WHERE post_id = {$post_id}";
$qStrCat = "SELECT category_id, category_name FROM categories";

And then: 接着:

<?php
    foreach ($categories as $cat) {
       if ($cat['category_id'] == $blog_data['category_id'])
           echo "<option selected=\"selected\">{$cat['category_name']}</option>"; 
       else  
           echo "<option>{$cat['category_name']}</option>"; 
    }
?>

You have two options: 您有两种选择:

Add a selected attribute with an if-condition in the foreach loop: foreach循环中添加带有if条件的selected属性:

foreach ($categories as $cat) {
    echo '<option';
    if ($cat['category_id'] == $post['category_id']) echo ' selected';
    echo '>'.$cat['category_name'].'</option>';
}

Or use an INNER JOIN on the post SELECT query: 或在SELECT查询后使用INNER JOIN

$qStr = "SELECT p.post_title, p.post_content, p.post_description, p.post_active, p.category_id, c.category_name FROM posts INNER JOIN categories c ON c.category_id = p.category_id WHERE post_id = {$post_id}";

and throw the first option element as the "default currently selected" option: 并将第一个option元素作为“默认当前选定”选项:

      <select class="form-control" id="categorySelect">
            <option value="<?= $post['category_id'] ?>"><?= $post['category_name'] ?> (current)</option>
            <?php
                 foreach ($categories as $cat) {
                     echo("<option>{$cat['category_name']}</option>");   
                 }
             ?>
       </select>

I've made the assumption that you've correctly loaded your result from the first SELECT query into $post . 我假设您已将第一个SELECT查询的结果正确加载到$post Note that if it's possible to not set a Category to a Post, you should use LEFT JOIN instead and wrap the first option element output contingent on $post['category_id'] not being empty. 请注意,如果有可能将Category设置为Post,则应改用LEFT JOIN并包装取决于$post['category_id']的第一个option元素输出(不为空)。

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

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