List Login User Post

Asked

Viewed 496 times

0

I’m a beginner in PHP and I’m making a system where the user can register and register and post, it’s a blogging system. I built the user with SESSION and can display only his data. In my database I have the users table, where are your data and the posts table. What I want to know now, is how I can list in the user profile, only the posts that he made, because the way I did so far, when I display the posts appear posts of all registered users. Here’s my code and thank you to 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, in this way I am selecting posts from all users and need help to list posts only from the currently logged in user.

  • $query = mysqli_query($conexao, "select * from postagens WHERE idusuario = {$variavelcontendoIDdousuario}");, replace the column idusuario the respective column in your database table and the variable $variavelcontendoIDdousuario by user id value.

1 answer

0


Simple friend, you will use the clause where 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 me all user posts with the following id, now it is only you edit to the name of the field that you put right.

  • So, @Rickpariz changed the function, but is giving the following error Notice: Undefined variable: id in C: xampp htdocs toqve internal profile.php on line 10 Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in C: xampp htdocs toqve internal funcoes.php on line 63

  • the line 63 is while($post = mysqli_fetch_assoc($query)) {

  • and line 10 is where listed: $posts = list);

  • @Francisvagnerdalight, are you passing the user id correctly to the function ? change the username in the query, to the name of the field that stores the user id in its table

  • Thanks @Rickpariz, I managed to solve here... thanks anyway!

  • Brother, glad I could help, I could mark my response as the right one ?

  • has yes @Rickpariz... but where do I do it?... rs.

  • below the points of the answer, which is in the left corner of it, has type a V, just click on it

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.