In my website, user’s can update and other can comment on the updates, as usual like other social networking websites..
What am doing :
query = fetches first name, last name, user id, update id, update post UNION same details for the user who has logged in i.e /* Updates Of Current Logged In User + Updates Of People He Is Following */
loop {
prints first name last name and update
}
later I introduced commenting feature so now what I do is in the loop I call a query and run a loop again for comments..
loop {
prints first name last name and update
commentquery = fetches firstname, last name, user id, comment, of the particulat post
again loop {
prints first/last name of the person who commented, and his comment
}
}
Now according to me I guess this can be accomplished with the SQL query, that when I retrieve posts, I can fetch it’s comments along with it, or is this the right way? the problem is when the outer loop runs, the inner loop also runs to fetch the appropriate comments, so can I join tables to retrieve the comments related to posts/updates?
This is very difficult to answer without your database structure or actual SQL but I have tried to generalise as much as possible. What you are trying to do should be done via SQL with two queries and one loop, something like below.
Edit Your fine looping and re-executing the query. You CAN fetch all this data in one go with SQL but you will duplicate allot of information in each call. For scalability and readability I would stick with two queries and two loops