List logged user post

0

I am a beginner in PHP and I am making a system where the user can register and register and make postings, it is a blog system. I mounted the user with SESSION and I can only display his data. In my database I have the users table, where your data and table posts remain. What I want to know now is how I can list in the user profile, only the posts he made, because the way I have done so far, when I display the posts appear the posts of all registered users. Here is my code and thanks to those who help.

My table where I display the list of posts

 <table>
      <?php
      $postagens = listaPostagens($conexao);
      foreach($postagens as $postagem) {
    ?>
        <tr>
          <td>
            <img src="envios/<?=$postagem['img']?>"/>
          </td>
          <td>
            </span><?=$postagem['localizacao']?>
          </td>
          <td>
            <?=$postagem['texto']?>
          </td>
          <td>
            <?=$postagem['visualizacoes']?>
          </td>
          <td>
           <?=$postagem['curtidas']?>
          </td>
          <td>
            <?=$postagem['compartilhamentos']?>
          </td>
        </tr>
        <?php
      }
    ?>
    </table>
  </section>
  <?php

Function that lists posts:

  function listaPostagens($conexao) {

    $postagens = [];
    $query = mysqli_query($conexao, "select * from postagens");
    while($postagem = mysqli_fetch_assoc($query)) {
      array_push($postagens, $postagem);
    }
    return $postagens;
  }

As I said, this way I'm selecting posts from all users and need help to list the posts only from the currently logged-in user.

    
asked by anonymous 05.09.2017 / 14:55

1 answer

0

Simple friend, you will use the where clause of mysql , in your function.

function listaPostagemDoUsuario($conexao, $id){
 $postagens = [];
 $query = mysqli_query($conexao, "select * from postagens where idUsuario = {$id}");
 while($postagem = mysqli_fetch_assoc($query)) {
   array_push($postagens, $postagem);
 }
 return $postagens;
}

the query select * from postagens where idUsuario = {$id} , says the following: return all the user's post with the following id, now it's just you edit for the field name you put right.

    
05.09.2017 / 15:04