Page 1 of 1

PHP select MySQL data if 'subscribed' to it

Posted: Tue Aug 07, 2012 8:57 am
by Helx
I want to have a 'feed' sort of thing on the users profiles homepage based on who they have subscribed to.

The user subscription data is stored like this:
user1|user2|user3
Then exploded using "|" as a delimiter.

The part I am having trouble with is showing the posts.
I tried this, but tested it and I know it wont work:
[syntax=php]
<?php
$feed_register_array = $feed_reg_to["post_author"];
foreach($feed_reg_to as $feed_follow){
$sql_feed = "SELECT * FROM post where post_author='$feed_register_array'";
echo $sql_feed["post"];
}
?>[/syntax]
Thats the basic plan. Obviously I'll add some more style and 'pizazz' :)
I really don't know much about foreach or while, but I'm sure they can be used in this situation.

Any suggestions?

Re: PHP select MySQL data if 'subscribed' to it

Posted: Tue Aug 07, 2012 10:35 pm
by JelvinJS7
I think i can help you as i think i've developed something similar before, but i'm having trouble understanding the project and problem. Can you clarify, please? (i have comprehension problems with some things :oops: )

Re: PHP select MySQL data if 'subscribed' to it

Posted: Tue Aug 07, 2012 11:26 pm
by Helx
User subscribes to a user, that users posts are updated on their homepage. I guess I could explain it like the youtube homepage :)

Re: PHP select MySQL data if 'subscribed' to it

Posted: Tue Aug 07, 2012 11:57 pm
by JelvinJS7
how is your database set up? my project was an RSS reader that had a users table (with all the obvious data), a feeds table (which is basically a URL to the feed, a description, title, and ID), and a feed_subs table (which basically is the ID of the user subscribing, and which feed they subscribed to).
if it's like that then on the homepage, assuming the session that logs them in stores the user's id (or their id is stored in some session… point is you have access to the id), then you can basically grab the last 10 entries to the database that the user has subscribed to… using my example, it'd be like
[syntax=sql]
SELECT `link` FROM `feeds` WHERE `id` IN (SELECT `feed_id` FROM `feed_subs` WHERE `user_id` = '{$_SESSION['uid']}')
[/syntax]

basically it selects all feeds from the feeds table that have an id that have a row in the feed_subs table which matches the users id. does this help at all? (convert feeds to posts and it should work). IF it doesn't help then i'd need an explanation of your database

Re: PHP select MySQL data if 'subscribed' to it

Posted: Wed Aug 08, 2012 12:00 am
by JelvinJS7
actaully, that being said: what is the problem: retrieving entries, or displaying them? it occurred to me that i may be helpong you do something you don't need help with… :oops:

Re: PHP select MySQL data if 'subscribed' to it

Posted: Sun Aug 12, 2012 5:51 am
by Helx
JelvinJS7 wrote:it occurred to me that i may be helping you do something you don't need help with… :oops:

I think you may be... :/

What I want is to show users 'posts' or comments if they have subscribed to it.
For example, if you subscribed to 'user1' then his posts would show up on your profile page, and posts by 'user2' will not be shown.

My database?
Looks a bit like this:
Image
I knnow thats probably the worst way to store it... Any suggestions as to what I should do instead?

Re: PHP select MySQL data if 'subscribed' to it

Posted: Sun Aug 12, 2012 6:16 am
by JelvinJS7
abcedea wrote:
JelvinJS7 wrote:it occurred to me that i may be helping you do something you don't need help with… :oops:

I think you may be... :/

I think i talked to much about my example then how to use it, but Yeah I'm very confused… I'm not sure I can help you with this… at least now.
I can't really understand your db picture. I assume you are using phpMyAdmin, so a screenshot of the page on the structure tab would be more clear.

What you could do is use a subscriptions table like I said: one column with the subscribing user's id, then the other with the subscribed user's id. Then the other table stores all posts, marking the ID of the user that posted it.
I assume by profile you mean homepage, and we aren't subscribed to 'user 2'. In which case you first query all IDs that you are subscribed to in the subscriptions table by using your ID in a session, then it takes all (recent) posts that are from those IDs.

[syntax=php]
<?php
Mysql_query("SELECT `subscribed_id` FROM `subscriptions` WHERE `subscriber_id` = {$session_id}");
//put selected ID into variable
mysql_query("SELECT * FROM `posts` WHERE `poster_id` = {$subscribed_id} LIMIT 5");
//somehow output this
?>
[/syntax]
Would be a generic idea to show the past 5 posts from people you've subscribed to. I'd show pictures of what I mean, but I'm on my iPod and it's 2 in the morning.
Does this help? If not, I'll try to clarify or just pass the baton to someone wiser than me