how to get the value of a field of the current connected user in buddyPress ?
ex: How to print out :
My gender is : {gender}
Get the current user id: get_current_user_id()
Then use xprofile_get_field_data
My gender is : <?php echo xprofile_get_field_data('gender', get_current_user_id(), 'comma'); ?>
http://hookr.io/functions/xprofile_get_field_data/
Related
I created a customer attribute in backend magento, but I want to show this attribute to the user so that he can alter its value in the frontend. My input field is being displayed in the frontend, but the value is not there. I am not able to get this value. I found that the value that I need to display is in the apscustomer_entity_varchar table and the column is called 'value'. How can I get that value from that table? I was trying this:
$collection = Mage::getModel('eav/entity_attribute')->getCollection();
foreach ($collection as $data) {
return $data;
}
but it was not working, so I used SQL code and it worked. However I know that's not a nice way to do that in Magento. What I did was something like:
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * FROM `apscustomer_entity_varchar ` WHERE `entity_id`='$id'";
$rows = $connection->fetchAll($sql);
How can I get the value column from my apscustomer_entity_varchar table in the magento way, using the getModel?
You need to retrieve the customer of the session and then you can get the attribute you want :
Mage::getSingleton('customer/session')->getData('attributeName');
Mage::getSingleton('customer/session')->getAttributeName(); //Magic getter
Where attributeName is the name of the attribute you want to get the value.
I found out how to do that :D
It's actually very simple. Even if this is a custom customer attribute (created in Magento admin panel), I need to get the attribute not using the 'eav/entity_attribute' model, but using the 'customer/customer' model. So I just need to do that:
$customer_data = Mage::getModel('customer/customer')->load($customer_id)->getData();
echo $customer_data['attributeThatYouWant'];
If you are not sure what is the name of your attribute you can look at Magento Admin Panel under Customer/Manage-Attributes, or you can use that after getting the model:
var_dump($customer_data);
Now you are able to get the name of all customer attributes.
I am using a component for parsing a country api in yii. So in the form drop down list call the function for listing country. The function returned country list as array.
form.php
<?php echo $form->labelEx($model,'country'); ?>
<?php $cty= Country::getCountry();
echo $form->dropdownList($model,'country', $cty , array('style'=>'width: 175px','empty'=>array('empty'=>Yii::t('app','Select Country'))));?>
Now the country list loaded correctly in drop down, but when on saving time the corresponding id of country is saved. i want to save the country name in db.How it solved?
You have to build your own custom array with the needed keys/values, e.g. :
$cty = Country::getCountry();
$cty = array_combine(array_values($cty), $cty);
You can use this way (in case if you need all items of Country table)
$cty = CHtml::listData(Country::model()->findAll(), 'name', 'name');
In magento, when a person becomes a member, the date "created_at" is saved with the profile.
How can I retrieve this information from magento. I can get the address info etc. but can't work out how to display / get the date a user created its account.
I tried the following:
echo Mage::getResourceModel('customer/customer_collection')->addAttributeToSelect('created_at');
But doesn't work.
Thanks.
Did you try this:
$customer = Mage::getModel('customer/customer')->load(IDHERE);
//or
$customer = Mage::getModel('customer/customer')->loadByEmail('test#test.com');
echo $customer->getCreatedAtTimestamp();
where exactly are you trying to get this? from within the customers session? or are you trying to get an arbitrary customer, rather than the current customer?
I have a case where user can select multiple values in the list box and save it to the database using model.
Here is the table structure
user_id int(11) , cars_id int(5)
Here is the snippet of my view
<?php echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20) );?>
<?php echo CHtml::dropDownList("targetCars", '', array(),array('size'=>20) );?>
User selects the cars from sourceCars and moves into targetCars using Jquery ( This part is done) and
clicks on Save or Submit button .
Now I should be able to save all the cars he/she selected in the targetCars list. Moreover in model I should put a condition that user can't save more than 10 cars and at least one car should be selected . Also user can select 5 cars at one time and next time when he comes he should be able to select max 5 cars only since he already save 10 records .
Could you please throw me some idea to implement this ? any Links that can guide me ?
your question is to limit selection of cars between 1-10.
You need validate user input both client and server.
At server,you can custom a ActiveRecord validation
public function rules()
{
return array(
array('cards_id', 'limitSelect','min'=>1,'max'=>10),
);
}
public function limitSelect($attribute,$params)
{
//and here your code to get the count of selection of cars for a user
...
if($count<=$params['min'])
$this->addError('cards_id','at least one car should be selected');
if($count>=$params['max'])
$this->addError('cards_id',' can't select more than 10 cars');
}
//and for mutiple select you can code this:
echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20,'multiple'=>true) );
//anyway you can implement it in several way
Sounds like you want to use scenarios, see docs here. You can dynamically set the scenario with CModel::setScenario based on the user flow.
I’m searching for a way to display on a member profile page, the number of contributions in some content types. Basically it has to display something like this:
Blog(10)
Articles(10)
Questions(19)
Comments(30)
Tips(3)
I’ve installed some different modules (like “user stats”) that I though could help me but haven’t been successful.
I’m wondering if it would be easiest just to hard-code it into my template file by starting taking the uid and just run some queries with the content types I want to display but I’m not sure on how to do that either.
Any help og suggestions would be very much appreciated.
Sincere
- Mestika
Edit:
I found a solution to do it manually with a query for each content type but I'm still very interested in a solution that's more elegant and smoother.
I use this code:
global $user;
$userid = $user->uid;
$blog_count = db_result(db_query("SELECT COUNT(0) AS num FROM {node} n where n.type = 'blog' AND status = 1 AND n.uid = {$userid}"));
If you are using the core Profile module, you could use something like below. It will show the nodes created by the user whose profile is being viewed. As an added benefit, it only needs to execute one custom database query.
Insert this snippet into template.php in your theme's folder and change "THEMENAME" to the name of your theme:
function THEMENAME_preprocess_user_profile(&$variables) {
// Information about user profile being viewed
$account = $variables['account'];
// Get info on all content types
$content_types = node_get_types('names');
// Get node counts for all content types for current user
$stats = array();
$node_counts = db_query('SELECT type, COUNT(type) AS num FROM {node} WHERE status = 1 AND uid = %d GROUP BY type', $account->uid);
while ($row = db_fetch_array($node_counts)) {
$stats[] = array(
'name' => $content_types[$row['type']],
'type' => $row['type'],
'num' => $row['num'],
);
}
$variables['node_stats'] = $stats;
}
Now, in user-profile.tpl.php can add something similar to:
// If user has created content, display stats
<?php if (count($node_stats) > 0): ?>
// For each content type, display a DIV with name and number of nodes
<?php foreach ($node_stats as $value): ?>
<div><?php print $value['name']; ?> (<?php print $value['num']; ?>)</div>
<?php endforeach; ?>
// Message to show for user that hasn't created any content
<?php else: ?>
<?php print $account->name; ?> has not created any content.
<?php endif; ?>
This is just a general idea of what you can do. You can also add restrictions to the content types you look for/display, check permissions for users to see these stats, use CSS to change the look of the stats, etc.
If you are using Content Profile, you could use THEMENAME_preprocess_node() and check that the node is a profile node before executing this code.
Given your simple requirement and the fact that you have the SQL statement in-hand, I'd say just use that. There's no reason to add yet another module to your site and impact it's performance for the sake of a single query.
That said, from a "separation of concerns" standpoint, you shouldn't just drop this SQL in your template. Instead, you should add its result to the list of available variables using a preprocess function in your template.php file, limiting its scope to where you need it so you're not running this database query on any pages but the appropriate profile page.