簡體   English   中英

如果我選擇該類別的新聞,如何選擇該類別的名稱

[英]how to pick a category name if i chose a news of that category

我正在設置一個查詢,其中我選擇了新聞ID來顯示特定的ID新聞,我在新聞表中將類別表的外鍵作為類別ID,現在我希望如果顯示新聞,我還要顯示類別標題。

我試過拿起只顯示ID的類別ID,並且我想顯示該類別的標題。

<?php
        if(isset($_GET['news_id'])){
         $the_news_id = $_GET['news_id'];
        }
   $query = "SELECT * FROM news_title WHERE news_id = '$the_news_id'";
        $select_all_news_query = mysqli_query($connection,$query);
        while($row= mysqli_fetch_assoc($select_all_news_query)){
            $title = $row['news_title'];
            $description = $row['news_description'];
            $image = $row['news_image'];
            $news_cat_title= $row['news_cat_id'];
        ?>
        <h1 class="page-header">News Page</h1>

        <!-- First Blog Post -->
        <h2>
            <a href="#"><?php echo $title;?></a>
        </h2>


        <img class="img-responsive" src="image/<?php echo $image; ?>" 
alt="abc">
        <hr>
        <p><?php echo $description; ?></p>

        <hr>
<?php  }
        ?>
        <!-- Second Blog Post -->
        <hr>
        <div class="container">
            <div class="row">
                <h1 class="page-header">
  <!--This is where i want to show title but i am getting the ID--!>
                 <?php echo $news_cat_title; ?>
                </h1>
            </div>
        </div>

我希望得到標題,但我得到一個號碼。

在不知道各個表的架構的情況下,很難確定我是否正確解釋了該問題,但是我假設您要執行的操作是聯接兩個表? 如果可以添加相關的表模式,則將有所幫助。

select * from news_title t
left outer join category c on c.category_id=t.category_id
where news_id = '$the_news_id'

根據您的最后評論,類別表簡稱為category ,外鍵為cat_id

select * from news_title t
left outer join category c on c.cat_id=t.cat_id
where news_id = '$the_news_id'

然后,當顯示記錄集時,您應該能夠:-

echo $row['cat_title'];

在Windows上捕獲和顯示表模式的簡單方法:

  • 打開cmd提示
  • 鍵入mysql -u root -p <DBNAME> 〜更改為實際數據庫!
  • 類型describe <TABLE>; -按回車
  • 復制顯示的信息並粘貼到文本編輯器中
  • 對所有相關表重復

請嘗試以下代碼:

<?php
if(isset($_GET['news_id'])){
 $the_news_id = $_GET['news_id'];
}

$query = "SELECT N.*, NC.cat_id AS news_cat_id
FROM news_title AS N
LEFT JOIN news_category AS NC
ON N.cat_id = NC.cat_id 
WHERE news_id = '$the_news_id'";

$select_all_news_query = mysqli_query($connection,$query);

while($row= mysqli_fetch_assoc($select_all_news_query)){
    $title = $row['news_title'];
    $description = $row['news_description'];
    $image = $row['news_image'];
    $news_cat_title= $row['news_cat_id'];
?>
<h1 class="page-header">News Page</h1>

<!-- First Blog Post -->
<h2>
    <a href="#"><?php echo $title;?></a>
</h2>


<img class="img-responsive" src="image/<?php echo $image; ?>" alt="abc">
<hr>
<p><?php echo $description; ?></p>

<hr>
<?php } ?>
<!-- Second Blog Post -->
<hr>
<div class="container">
    <div class="row">
        <h1 class="page-header">
<!--This is where i want to show title but i am getting the ID--!>
         <?php echo $news_cat_title; ?>
        </h1>
    </div>
</div>

基於您的問題和評論的查詢語句可能像這樣:

$query = "SELECT N.*, NC.cat_id AS news_cat_id
FROM news_title AS N
LEFT JOIN news_category AS NC
ON N.cat_id = NC.cat_id 
WHERE news_id = '$the_news_id'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM