this is a table of comments [! [] [1] ] 1 I have categorie.php in which articles from the selected category are stored, in each block with an article there is a block in which the number of comments to the article should be displayed. The questionshow.php displays an article. I have a question how to do so in order to count the number of comments on the questionshow.php page and display the result of the calculation in the block on the categorie.php page? Write if something is not enough for you to understand my question, throw off everything you need.

<?php $article = mysqli_query($connection, "SELECT * FROM `articles` WHERE `categorie_id` = " . (int) $_GET['id'] ); if( mysqli_num_rows($article) <= 0 ) { ?> <div id="content"> <div class="container"> <div class="row"> <section class="content__left col-md-8"> <div class="block"> <h3>Ошибка!</h3> <div class="block__content"> <div class="full-text"> Запрашиваемая Вами Страница Не Существует! </div> </div> </div> </section> <section class="content__right col-md-4"> <div class="block"> <h3></h3> <div class="block__content"> </div> </div> </section> </div> </div> </div> <?php } else $art = mysqli_fetch_assoc($article); ?> <?php $categories = mysqli_query($connection, "SELECT * FROM `articles_categories`"); while( $cat = mysqli_fetch_assoc($categories) ) ?> <?php $art_cat = false; foreach( $categories as $cat ) { if( $cat['id'] == $art['categorie_id']) { $art_cat = $cat; break; } } ?> <div class="w-100 mt-4"> <div class="block block-shadow p-1"> <div class="block block-shadow light-input-bg py-1"> <h3 class="light-text m-0 d-inline-block"><?php echo $art_cat['title']; ?></h3> <p class="light-text d-inline-block m-0 mt-1 float-right"><?php echo 'Вопросов: ' . mysqli_num_rows($article);?></p> </div> <div class="p-2"> <div class="card example-2 square scrollbar-dusty-grass-2 square thin bg-transparent"> <?php echo'<a href=/questionshow.php?id='. $art['id']. '><div class="question-block block-shadow mt-1 mb-0"> <div><h3>'. mb_substr(strip_tags($art['title']) , 0, 75, 'utf-8'). '</h3></div> <div class="question-block-right"> <div class="question-block-right-in text-white"> <span class="time-question">'.$art['pubdate'].'</span> </div> <p>Ответов <span>'.mysqli_num_rows($article).'</span></p> </div> </div> </a>'; ?> Комментарии хранятся в questionshow.php.Вот код комментариев: <?php $comments = mysqli_query($connection, "SELECT * FROM `comments` WHERE `id_articles` = " . (int) $art['id'] . " ORDER BY `id` DESC "); ?> <h5 class="mt-2 light-text mb-0">Комментарии</h5> <?php { while( $comment = mysqli_fetch_assoc($comments) ) { echo'<div style="padding-bottom: 1px !important;" class="block block-shadow light-bg p-0"> <div class="block block-shadow light-bg m-0 py-1"> <h3 class="light-text m-0 d-inline-block"> <img class="d-inline-block" style="border-radius: 90px;" width="35" src=/images/avatars/200x200.png alt=""> <p style="font-size: 20px" class="d-inline-block m-0">'.$comment ['author'].'</p> </h3> <p style="padding: 10px 0px;" class="light-text d-inline-block m-0 mt-0 float-right">'.$comment ['pubdate'].'</p> </div> <div class="block block-shadow dark-bg m-1 light-text"> <p class="mb-0">' .$comment['text']. '</p> </div> </div>'; } ?> <?php } ?> 

it comments

enter image description here

  • And what causes the difficulty? Can't make a request? - n.osennij
  • And how do we say how to count comments, if you don’t give the database structure and where these comments are stored at all. And of course, it is necessary to do this in the same query that receives the articles, perform the left join of the article table with the comment table, make a group by before the article with the count (comment. Id) will give the number of comments - Mike
  • @Mike added code with comments output, how do you drop the structure of the database? - skeet.cc
  • @ n.osennij the difficulty is that the comments in questionshow.php are located, and where you need to display the result, the counting is in the category.php file, in different pages, files, and I don’t understand how to make requests from different pages to me: ( - skeet.cc
  • What does it mean to be? Comments are in the database. - n.osennij

1 answer 1

 SELECT * FROM `articles` WHERE `categorie_id` = " . (int) $_GET['id'] $_GET['id'] => не делайте так ни когда sql инъекция. $id = inval($_GET['id']); SELECT count() FROM `articles` WHERE `categorie_id` = $id // если подсчитать только один SELECT categorie_id, COUNT(*) FROM `articles` WHERE `categorie_id` = $id GROUP BY categorie_id // если подсчет всех, чего !НЕ рекомендую // нужно передать массив id $id = 1,2..; SELECT categorie_id, COUNT(*) FROM `articles` WHERE `categorie_id` IN $id GROUP BY categorie_id // некоторое кол-во 
  • Fatal error: Call to undefined function inval () gives this error, I just deleted this SELECT * FROM clause WHERE categorie_id = ". (Int) $ _GET ['id'] and replaced it with your $ id = inval ($ _GET ['id']); $ article = mysqli_query ($ connection, "SELECT count () FROM articles WHERE categorie_id = $ id"); What is the problem and why is it unsafe to send id through $ _GET? - skeet.cc
  • Apparently the function inval does not work on my php 5.5 version - skeet.cc
  • Imprint intval () => php.ru/manual/function.intval.html - Sukhrab
  • now works, thanks, get the daw - skeet.cc