Hello.

I do my first php project.

It is necessary to show all comments to a specific post. That is, from the right of the image of the post there are 2 blocks one red not confirmed comments and green confirmed comments.

In the course (which is used by the old php ) the guy adds this code. Everything works fine for him. However, I didn’t understand why he works, because only the first element of the array extracts it from array_shift .

In short, after adding this code to show how many comments each post has, all my posts have disappeared except the last one. But comments are working. Please help.

Here is the code if you remove all posts will be visible. (But comments will not work)

  <td> <!-- status on --> <?php $connection; //покдлючение к базе данным $queryApproved = "SELECT COUNT(*) FROM comments WHERE admin_panel_id='$id' AND status='ON'"; //берем коммент с БД у которого таблица статус равно ON $execute = mysqli_query($connection,$queryApproved); //выполняем $rows_approved = mysqli_fetch_array($execute); $total_approved = array_shift($rows_approved);//извлекает первый элемент если документация не врет if($total_approved > 0){ //если потвержденные комментарии больше нулья ?> <span class="badge float-right badge-success"><?php echo $total_approved; ?></span> //показать их <?php } ?> <!-- status off --> <?php //тоже самый код как и наверху только тут берет если статус равен OFF то есть не потвержден админом $connection; $queryUnApproved = "SELECT COUNT(*) FROM comments WHERE admin_panel_id='$id' AND status='OFF'"; $execute = mysqli_query($connection,$queryUnApproved); $rows_un_approved = mysqli_fetch_array($execute); $total_un_approved = array_shift($rows_un_approved); if($total_un_approved > 0){ ?> <span class="badge float-left badge-danger"><?php echo $total_un_approved; ?></span> <?php } ?> </td> 

help me please

For complete clarity I will upload a picture.

Before adding the code, all posts on the site everything works

After adding the code as you can see all the posts are gone but the comments work.

enter image description here

    1 answer 1

    Rename all variables except $connection and you will be happy.

    • Better yet, make a function that will accept the status flag, on the basis of which a selection will be made - Artem Korsunov
    • Oo, but how? Tell me if you guessed that I had an error in the variables? Where was the error? And did not array_shift extract the first element? Thanks a lot in advance, you helped me a lot with the plus sign. - Demon __ ANT
    • tell me plz where did you find out - Demon __ ANT
    • @Demon__ANT just guessed. The practice was;) - Manitikyl 5:38 pm
    • one
      @Demon__ANT yazh you answered, just guessed. deduction method like Sherlock's - Manitikyl