V
V
vovkka2016-09-17 19:57:02
PHP
vovkka, 2016-09-17 19:57:02

How to correctly build a query structure for selecting from several tables?

Good day. The essence of the question is this: there is a table with articles, there is a table with categories. I want to join two tables with a single query, while doing this (screenshot). Am I on the right track? + There will also be a table with users who are also linked to articles with keys. How can I query the database in this case? I'm thinking of joining everything in one query with inner joins. Please tell me how it is done correctly. Thank you.

//функция выборки одной статьи по id
function out_article_id($id){
  global $con;
  $query = "SELECT c.name AS cat, a.* FROM category c INNER JOIN articles a ON c.id = a.category WHERE a.id = $id;";
  $query_result = mysqli_query($con, $query);
  $query_result_top = mysqli_fetch_all($query_result, MYSQLI_ASSOC);
  if ($query_result_top) {
    return $query_result_top;
  }else {
    return false;
  }
}

//функция выборки всех статей 
function out_category(){
  global $con;
  $query = "SELECT c.name AS cat, a.* FROM category c INNER JOIN articles a ON c.id = a.category;";
  $query_result = mysqli_query($con, $query);
  $query_result_top = mysqli_fetch_all($query_result, MYSQLI_ASSOC);
  if ($query_result_top) {
    return $query_result_top;
  }else {
    return false;
  }
}

<?php foreach ($article as $value): ?>
    <div class="article-item">
      <div class="article-item-title"><?=$value['title'];?></div>
      <div class="article-item-text"><?=$value['text'];?></div>
      <div class="article-property-holder">
        <div class="article-item-date">Дата публикации: <?=$value['date'];?></div>
        <div class="article-item-aythor">Автор: <?=$value['author'];?></div>
        <div class="article-item-category">Категория: <?=$value['cat'];?></div>
      </div>
    </div>
    <?php endforeach; ?>

a8b6aea2e8374669838875d3ab61cde8.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eqcodes, 2016-09-18
@eqcodes

Can you provide a normal code listing and clarify the task?
Do you display all the news from the specified category at once without restriction?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question