mardi 4 août 2015

Get user information from users table

I have a query set where I am outputting comments which are bound to posts and also showing the user who posted the comment via this query:

$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, j.id, u.user_id
         FROM forum_post AS p
         INNER JOIN comment_post_join AS j ON p.post_id = j.post_id
         INNER JOIN forum_comment AS f ON f.comment_id = j.comment_id
         INNER JOIN user_comment_join AS u ON u.user_id = u.user_id
         WHERE p.post_id = '$id'
         ORDER BY created_date ASC
     ";   

$r = mysqli_query ($dbc, $q); // Run the query.


// FETCH AND PRINT ALL THE RECORDS
while ($row = mysqli_fetch_array($r)) {

echo '
<div style="border-bottom: thin solid #ddd;">
    <p style="font-size: 13px;">'.$row["comment_content"] . '</p>
    <p style="font-size: 12px;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["user_id"] . '</a> ' .date("F j, Y, g:i a", strtotime($row["created_date"])). '</p>
</div>
';

}  

As you can see the end result outputs the comment and also the user_id but if I ass u.first_name for example it fails to give me output. In my above it will output the users ID of who posted but struggling to get their name.

I have these tables:

users
  user_id
  first_name
  last_name

comment_post_join
  comment_id (Fk)
  post_id (Fk)

user_comment_join
  user_id (Fk)
  comment_id (Fk)

user_post_join
  user_id (Fk)
  post_id (Fk)

forum_post
  post_id
  post_title
  post_content
  post_created
  post_topic

forum_comment
  comment_id
  comment_content
  created_date

UPDATE

 function build_post_view(){

        global $dbc;

        $id = $_GET['post_id'];

        $q = "SELECT u.user_id, u.first_name, u.last_name, f.post_created, f.post_id, f.post_title, f.post_content, j.id 
                 FROM users AS u
                 INNER JOIN user_post_join AS j ON u.user_id = j.user_id
                 INNER JOIN forum_post AS f ON f.post_id = j.post_id
                 WHERE f.post_id = '$id'
             ";   

        $r = mysqli_query ($dbc, $q); // Run the query.

        // FETCH AND PRINT ALL THE RECORDS
        while ($row = mysqli_fetch_array($r)) {

        echo '
        <div style="border-bottom: thin solid #ddd;">
            <h3>'.$row["post_title"]. '</h3>
            <p style="font-size: 13px;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["first_name"] . ' ' .$row["last_name"]. '</a> ' .date("F j, Y, g:i a", strtotime($row["post_created"])). '</p>
            <br>
            <p style="font-weight: 500; line-height: 150%;">' . $row["post_content"] . '</p>
            <br><br>
        </div>    
        ';

        } 


$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, j.id, u.user_id, u.first_name, u.last_name
         FROM forum_post AS p
         INNER JOIN users AS us ON us.user_id = u.user_id
         INNER JOIN comment_post_join AS j ON p.post_id = j.post_id
         INNER JOIN forum_comment AS f ON f.comment_id = j.comment_id
         INNER JOIN user_comment_join AS u ON u.user_id = u.user_id
         WHERE p.post_id = '$id'
         ORDER BY created_date ASC
     ";   

$r = mysqli_query ($dbc, $q); // Run the query.


// FETCH AND PRINT ALL THE RECORDS
while ($row = mysqli_fetch_assoc($r)) {

echo '
<div style="border-bottom: thin solid #ddd;">
    <p style="font-size: 13px;">'.$row["comment_content"] . '</p>
    <p style="font-size: 12px;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["user_id"] . ' '.$row["first_name"] . ' '.$row["last_name"] . '</a> ' .date("F j, Y, g:i a", strtotime($row["created_date"])). '</p>
</div>
';

}   
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire