Page 1 of 1

If statement/SQL (display different RSS feeds in sidebar)

Posted: Tue Nov 01, 2011 11:56 am
by hogfan24
I'm using Buddypress to run a social network. I'd like to display different RSS feeds in the sidebar based upon a user setting in their profile. For example, if a user was interested in a particular subject, they would select it from the dropdown list in their profile and then my if statement would display the appropriate Yahoo Pipes script. If they haven't selected anything, I have a generic feed that I'd like to display.

I know I need to have PHP access the database, but I struggle with how to query the correct table to find what the user has selected. There are 120 options in the list and are stored in bp_xprofile_fields and group id #55 (I assume I'd need this). The users are stored in the bp_xprofile_data table and have unique user ids.

While I have limited programming experience, I think I know the logic. For example:

Get user id for active user and store it in a variable.

If option for user id is birds, display birds RSS feed script;
else dogs, display dogs RSS feed script;
else default, display default RSS feed script

I'm pretty sure this is the way to go, but if you have any better suggestions, I'm all ears! I didn't know if having a giant php if statement would be the most efficient way. I imagine there is probably a way to store all of the Yahoo Pipes scripts in the database and call them with fewer lines of code, right?

Thanks in advance for the help!

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Tue Nov 01, 2011 1:22 pm
by manohoo
Your logic is sound. However, you should consider a switch, take a look at this example:

Code: Select all

switch ($state) { 
	case 'AL': { do AL stuff here}; break;
	case 'CA': { do CA stuff here}; break;
	default: { do default stuff here };
}

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Tue Nov 01, 2011 2:04 pm
by hogfan24
manohoo wrote:Your logic is sound. However, you should consider a switch, take a look at this example:

Code: Select all

switch ($state) { 
	case 'AL': { do AL stuff here}; break;
	case 'CA': { do CA stuff here}; break;
	default: { do default stuff here };
}
Ah, that does look like a good way to go! Thanks for the suggestion!

Okay, so how would I actually query the database? Since the feed depends upon the user's selection, how do I query the correct field for the logged in user? I obviously wouldn't want to check every user or every feed.

I assume I'd do something like this...

Code: Select all

$con = mysql_connect("database","username","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$feedoption = mysql_query("SELECT ??Help here?? FROM table");

switch ($feedoption) { 
        case 'AL': { do AL stuff here}; break;
        case 'CA': { do CA stuff here}; break;
        default: { do default stuff here };
}
Thanks again!

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Tue Nov 01, 2011 5:11 pm
by manohoo
Please post the structure of the relevant tables so that we can take a look.

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Tue Nov 01, 2011 7:13 pm
by hogfan24
The bp_xprofile_data table is the list of users and has the following fields:

Field / Type
id / bigint(20)
field_id / bigint(20)
user_id / bigint(20)
value / longtext
last_updated / datetime


The bp_xprofile_fields table has the following fields:

Field / Type
id / bigint(20)
group_id / bigint(20)
parent_id / bigint(20)
type / varchar(150)
name / varchar(150)
description / longtext
is_required / tinyint(1)
is_default_option / tinyint(1)
field_order / bigint(20)
option_order / bigint(20)
order_by / varchar(15)
can_delete / tinyint(1)

The available options for the user to select are stored in the bp_xprofile_fields table. When browsing the database, the id field is unique and the parent_id field is "2" for all. The group_id field simply pertains to how the profile settings are organized into groups. In this case, all of the options are in group 1.

From what I can tell, I'd need to find the user listed in the bp_xprofile_data table and then query the setting for that particular user in the bp_xprofile_fields table, but this is where I get stuck.

Thanks again for all of the help!

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Wed Nov 02, 2011 11:10 am
by manohoo
Good. Now, in order to build the query we need to know the foreign key in table bp_xprofile_feeds. You might want to explore the INFORMATION_SCHEMA database to find it.
Also, I presume that bp_xprofile_fields.is_default_option is the field containing the default group you are after, correct?

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Wed Nov 02, 2011 2:24 pm
by hogfan24
manohoo wrote:Good. Now, in order to build the query we need to know the foreign key in table bp_xprofile_feeds. You might want to explore the INFORMATION_SCHEMA database to find it.
Also, I presume that bp_xprofile_fields.is_default_option is the field containing the default group you are after, correct?
I haven't built a table for the feeds as I wasn't sure if that was the best way to go, yet. Originally I thought of just putting the script for each feed into the php, but if you suggest using a database to store all of them, I'm all ears.

As for the bp_xprofile_fields.is_default_option, if that = 1, that means the drop down list will have that particular option listed until the user changes it.

Just to give you a little more information on how everything is organized on the site, there is a separate tab for each group of user profile options and the options can be moved from group to group if I were to reorganize it.

For example, the first tab, called "Basic Profile," could have all of the group one fields:

Name
State
Hometown
Zip Code

And the second tab, called "User Settings," could have all of the group two fields:

RSS Feed
Display Color
etc.


So, with that said, I don't believe I'm really looking for a default group. Instead, it looks like the parent_id is what determines the setting to which a particular row/option belongs. So, I'd need to find the logged in user, then figure out which field that particular user has selected among all of the fields with the correct parent_id.

For example, in group one above, each of the 50 states would be listed with parent_id = 2 so that it would tie the option to the State setting. While I'm not currently using two separate groups, using what I showed you above, all of the RSS Feed options would have group=2 and parent=4. Each option would have a separate id, of course, but the group_id and parent_id fields would be what ties it back to the setting.

Does this make any sense? This is all from how Buddypress operates so I'm trying to explain how they've structured it.

Re: If statement/SQL (display different RSS feeds in sidebar

Posted: Sun Nov 06, 2011 8:31 pm
by hogfan24
I hate to bump thread, but I'm still pretty much stuck where I was. Any help is greatly appreciated!