List Members based on extended profile fields in buddypress - buddypress

I'm working on getting a list of members based on the fields they selected on the extended profiles fields in buddypress. Here is my code:
<?php
$membership_group = "Orange Membership";
$db_query = "SELECT user_id FROM wp_bp_xprofile_data WHERE field_id = 33 AND value = \"" .$membership_group ."\"";
$match_ids = $wpdb->get_var($db_query);
$get_these_members = 'include=' .$match_ids;
if (bp_has_members($get_these_members, 'per_page optional=9')) {
//Some Codes here
}
?>
The result is returning just the first member it gets from the query instead of a list of members. Please say what I'm doing wrong.
Thanks

I think you should dive into the class BP_Core_User and its method get_users. It supports meta_key and meta_value.
You can also try to make just a search by field value. So pass an argument s to bp_has_members.
And per_page optional=9 is a wrong syntax.

This:
$wpdb->get_var($db_query);
returns a single var !
This is what you want:
$wpdb->get_col($db_query);
Then fix the syntax error mentioned by slaFFik

Related

Django: how to assign a RawQuerySet to a Selectfield in forms

In a lot of situations I add a custom queryset to a selectfield in django form.
form.fields[fieldname] = model.objects.all()
Now I have a selectfield where I need to assign as choices the result of a RawQueryset.
I tried this (queryset is simplified):
sql = "SELECT * FROM table WHERE value = %s"
param.append (1234)
qs = model.objects.raw (sql, param)
form.fields[fieldname] = qs
The page is correctly displayed and the select field contains the data from the queryset.
When I try to save the form, form validation (form.is_valid()) throws the error:
*** Attribut error: 'RawQuerySet' object has no attribut 'get'
How can I fix this?
Thanks
You cannot assign a queryset to a form field, like this:
# this is wrong
form.fields[fieldname] = qs
you need to define the field using one of the fields django has, like:
form.fields[fieldname] = forms.ModelChoiceField(queryset=qs)
django doc.

django "use_natural_foreign_keys=True" issue

I currently use the well documented "use_natural_foreign_keys=True" to return the relevant field data required instead of the id:
all_orders = Orders.objects.all()
resp = serializers.serialize('json', all_orders, use_natural_foreign_keys=True)
What I don't know how to do is return both the id AND the field data required as typically returned by the "use of use_natural_foreign_keys=True".
Anyone know of a quick fix to return both?
Many thanks, Alan.
define a "natural_key" method in your model class, whose id and field_name you like to get. e.g
def natural_key(self):
return (self.id, self.field_name)

SQL: Use a predefined list in the where clause

Here is an example of what I am trying to do:
def famlist = selection.getUnique('Family_code')
... Where “””...
and testedWaferPass.family_code in $famlist
“””...
famlist is a list of objects
‘selection’ will change every run, so the list is always changing.
I want to return only columns from my SQL search where the row is found in the list that I have created.
I realize it is supposed to look like: in ('foo','bar')
But no matter what I do, my list will not get like that. So I have to turn my list into a string?
('\${famlist.join("', '")}')
Ive tried the above, idk. Wasn’t working for me. Just thought I would throw that in there. Would love some suggestions. Thanks.
I am willing to bet there is a Groovier way to implement this than shown below - but this works. Here's the important part of my sample script. nameList original contains the string names. Need to quote each entry in the list, then string the [ and ] from the toString result. I tried passing as prepared statement but for that you need to dynamically create the string for the ? for each element in the list. This quick-hack doesn't use a prepared statement.
def nameList = ['Reports', 'Customer', 'Associates']
def nameListString = nameList.collect{"'${it}'"}.toString().substring(1)
nameListString = nameListString.substring(0, nameListString.length()-1)
String stmt = "select * from action_group_i18n where name in ( $nameListString)"
db.eachRow( stmt ) { row ->
println "$row.action_group_id, $row.language, $row.name"
}
Hope this helps!

In yii how to access other tables fields

I am creating project in yii framework. I am having table as-
Qbquestion QbquestionOption
-questionId -optionId
-question -questionId
-userId -option
-isPublished -isAnswer
In QbquestionOption controller i want to access Qbquestion tables fields. I had written quesry as-
$Question=Qbquestion::model()->findAllByAttributes(array("questionId"=>$number));
where $number is some random number.
When i am using its fields as= $Question->isPublished then its giving error as "trying to get access to property of non-object"
Statement var_dump($Question) is showing all record and all values of Qbquestion table. So how can i access records?
Please help me
$Question is an array of models you cannot call model function on that..
what you can do is:
$questions = Qbquestion::model()->findAllByAttributes(array("questionId"=>$number));
foreach($questions as $question)
$question->isPublished()
or you can use findByAttribute to get single result..
findAllByAttributes returns an array of objects.
If you just want one question, use findByAttributes instead, then it should work as you want.
Try As I don't know your relation n all. Just give a try. I'll post other answers if it don't works. Also tel me in which table isPublished field is? If it in other table as your title mentioned you need to change it as echo $qRec->OtherTableRelationArrayKey->isPublished;
foreach($Question AS $qRec)
{
echo $qRec->isPublished;
echo "<br />";
}

Does CDbcommand method queryAll() in yii return indexed entries only?

I am trying to retrieve data from a simple mySql table tbl_u_type which has just two columns, 'tid' and 'type'.
I want to use a direct SQL query instead of the Model logic. I used:
$command = Yii::app()->db->createCommand();
$userArray = $command->select('type')->from('tbl_u_type')->queryAll();
return $userArray;
But in the dropdown list it automatically shows an index number along with the required entry. Is there any way I can avoid the index number?
To make an array of data usable in a dropdown, use the CHtml::listData() method. If I understand the question right, this should get you going. Something like this:
$command = Yii::app()->db->createCommand();
$userArray = $command->select('tid, type')->from('tbl_u_type')->queryAll();
echo CHtml::dropdownlist('my_dropdown','',CHtml::listData($userArray,'tid','type'));
You can also do this with the Model if you have one set up for the tbl_u_type table:
$users = UType::model()->findall();
echo CHtml::dropdownlist('my_dropdown','',CHtml::listData($users ,'tid','type'));
I hope that gets you on the right track. I didn't test my code here, as usual, so watch out for that. ;) Good luck!