0

I am struggling with my code. I try to make a News Feed.

The news feed is working well but now I want to add a avatar picture on each news row to have a picture from the author. The avatar picture path is in a different table (users).

That means I need to get in the loop the specific user ID ($row['uid']) and with that uid I need to get the related path from the user's avatar in the user table.

If I try to use a query in my loop it shows me only one result instead of 6 as specified in my query.

Do you have any suggestion or advise how I can solve that issue?

Many thanks for your support!

This is my attempt for the moment:

            <div class="timeline p-4 block mb-4">

                <?php

                // Getting the user id and the feedmessage from Table "activity_feed"
                $statement = $pdo->prepare("SELECT feed_message, uid FROM activity_feed WHERE cid = :cid ORDER BY id DESC LIMIT 6");
                $result = $statement->execute(array('cid' => $cid));
                $count = 1;
                while($row = $statement->fetch()) 
                
                { 
                // Starting the News feed Loop
                ?>
                
                <?php

                // This will repeat now X times as defined in the query above by = LIMIT ?

                    // Getting the avatar path from the user table 
                    $statement = $pdo->prepare("SELECT avatar FROM users WHERE id = :id");
                    $result = $statement->execute(array('id' => $row['uid']));
                    $user_avatar = $statement->fetch(); 
                    ?>

                        <style>
                            #circle_feed {
                            height:50px;
                            width:50px;
                            border-radius:50%;
                            /* Including the avatar path from query above*/ 
                            background-image: url("<?php echo $user_avatar['avatar']; ?>");
                            background-position:center;
                            background-size:cover;
                                        }
                        </style>
                                

                    <div class="tl-item ">
                        <div class="tl-dot"><a class="tl-author" href="#" data-abc="true">
                        <!-- Including the Avatar circle here-->
                            <span class="w-40 avatar circle gd-warning border" id="circle_feed"></span></a>
                        </div>
                        <div class="tl-content">
                            <div class=""><?php echo $row['feed_message']; ?></div>
                            <div class="tl-date text-muted mt-1">DATE</div>
                        </div>
                    </div>                    

                  <?php 
                  // News Feed Loop is ending here
                    }       
                  ?>                   
                    
                </div>
            </div>

1 Answer 1

1

There is no need to loop. SQL is a set-based language that can give your the results that you want in a single query, using the join syntax:

SELECT 
    af.feed_message, 
    af.uid,
    u.avatar
FROM activity_feed af
INNER JOIN users u ON u.id = af.uid
WHERE af.cid = :cid 
ORDER BY af.id DESC 
LIMIT 6

This is much more efficient than running 7 queries (one for the activity feed, then one for each row returned by the first query, in a PHP loop).

You can then fetch the rows of the resultset and use this directly in your application.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Looks much better and more simple. It works now.
Just for those with the same problem. The above mentioned answer is the solution for the problem. how ever i recognized that the style is doint no loop and answers always wit h the first picture. You need to move the background command out of the style {} and add the command into: HTML code like this: code style="background-image: url('<?php echo $row['avatar']; ?>');" code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.