I need the form to populate username, email and school from my CB table.
The Profile plugin is not linked to the user database, or limited to a given user. Instead, it allows you to retrieve any data from any available database table, and insert these data into the generated form. It will expect the user to provide some kind of key value to limit which row from the database table to use, using a GET-style form field (http://www.myhost.tld/myform.html?somekey=thevalue).
If you just need a few values from the CB-users table for the currently logged in user, I believe the Profile plugin is overkill. Also, the data you requested is actually split across the CB-users and the Joomla-users tables, so we'll actually need to gather data from two tables, which rules the profile plugin out. Use some php-code such as below instead:
<?
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
/* Make sure the user is logged in... */
if ($user->id == 0) {
echo "User not logged in...";
return;
}
$query = 'SELECT u.username AS username, u.email AS email, c.cb_school AS school FROM #__users AS u LEFT JOIN #__comprofiler AS c ON (u.id = c.user_id) WHERE u.id = ' . $user->id;
$db->setQuery($query);
$data = $db->loadObject();
?>
You are currently logged in as <? echo $data->username ?>@<? $data->school ?> (<? $data->email ?>)...
Edit: Removed stray ' in SQL-query
/Fredrik
I don't know php but I'm using some codes provided to me from last year when I didn't use CB. So I have my form pulling the username and email from the jos_users table but now I'm lost when trying to add a CB field(cb_schoolloyalty).
I'm trying the code you supplied but I'm getting this error when putting it in to my form code
Parse error: syntax error, unexpected '<' in /home/cfbpredi/public_html/components/com_chronocontact/chronocontact.html.php(180) : eval()'d code on line 36
Where should I put it?
Also, only registered and logged in users will be able to access the form so verifying login isn't necessary.
The first part would be this:
<?
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
/* Make sure the user is logged in... */
if ($user->id == 0) {
echo "User not logged in...";
return;
}
$query = 'SELECT u.username AS username, u.email AS email, c.cb_schoolloyalty AS school FROM #__users AS u LEFT JOIN #__comprofiler AS c ON (u.id = c.user_id) WHERE u.id = ' . $user->id;
$db->setQuery($query);
$data = $db->loadObject();
?>This would go at the very top of your form code (adjusted the SQL query to use the field name you provided in your last post).
The second part is where you write the data.. this mostly depends on how you created your form, and how you'd like to use the retrieved data, but very simply put;
<? ?> indicates a block of php-code which will be evaluated.
$data is the object that holds the data we retreived, it's properties are 'username', 'email', and 'school'. You access these like $data->school.
To populate a text input, you'd use something like this:
<input type="text" name="school" value="<? echo $data->school; ?>" />The error is probably because I forgot to terminate the command using ; in my previous example.
Edit: Removed stray ' in SQL-query.
/Fredrik
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/cfbpredi/public_html/components/com_chronocontact/chronocontact.html.php(180) : eval()'d code on line 45
line 45 on the form in my Dreamweaver is:
$query = 'SELECT u.username AS username, u.email AS email, c.cb_schoolloyalty AS school FROM #__users AS u LEFT JOIN #__comprofiler AS c ON (u.id = c.user_id) WHERE u.id = ' . $user->id';There must be some character in there that's not needed or something.
It's the single quote right at the end - you already exited the quotes before the $user.
It's better practice to more explicitly quote the SQL too (and I break up the lines for clarity):
$query = "
SELECT `u`.`username` AS `username`, `u`.`email` AS `email`, `c`.`cb_schoolloyalty` AS `school`
FROM `#__users` AS `u`
LEFT JOIN `#__comprofiler` AS `c` ON (`u`.`id` = `c`.`user_id`)
WHERE `u`.`id` = '".$user->id."';
";I think this is technically correct - though I probably wouldn't add all these back-ticks!Bob
$user = CFactory::getUser($userId);
$data = $user->getInfo('FIELD_CODE');http://www.jomsocial.com/docs/User_Object
The problem is when I plug this into the php tags on my form in CF and try and save, it rejects that code.
Fatal error: Class 'CFactory' not found in root/administrator/components/com_chronocontact/admin.chronocontact.php(2736) : eval()'d code on line 41
...so not sure how to proceed
btw, thanks again for helping me with this stuff last year.
I am unfortunately not that familiar with JomSocial. Roughly though, what you need to do, is include whichever file(s) from JomSocial that defines the CFactory (and other needed) classes. You'd usually use the "require_once()" function for custom files/libraries, and jimport for Joomla native classes.
Perhaps Bob or someone else can be of better help pointing out the exact files needed to be included.
/Fredrik
<?php
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$name = $user->getDisplayName();
$data = $user->getInfo('FIELD_JS_CB_SCHOOLLOYALTY');
echo '<span>username: ' . $name . '</span>';
echo '<span>school: ' . $data . '</span>';
?>
<label>
<input name="username" type="hidden" id="username" value="<?php echo $name->username; ?>" />
</label>
<label>
<input name="school" type="hidden" id="school" value="<?php echo $data->school; ?>" />
</label>
For JomSocial users, the first two lines are what you need to include to define the CFactory library.
<?php
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$name = $user->getDisplayName();
$data = $user->getInfo('FIELD_JS_CB_SCHOOLLOYALTY');
?>
<input name="username" type="hidden" id="username" value="<?php echo $name?>" readonly="readonly" />
<input name="school" type="hidden" id="school" value="<?php echo $data?>" readonly="readonly" />
JomSocial 3rd party component integration
Support JomSocial Messaging
$jspath = JPATH_ROOT.DS.'components'.DS.'com_community';
include_once($jspath.DS.'libraries'.DS.'core.php');
include_once($jspath.DS.'libraries'.DS.'messaging.php');
// Add a onclick action to any link to send a message
// Here, we assume $usrid contain the id of the user we want to send message to
$onclick = CMessaging::getPopup($userid);
echo '<a href="javascript:void(0)" onclick="'. $onclick .'">Send message</a>';Using JomSocial avatar
$jspath = JPATH_ROOT.DS.'components'.DS.'com_community';
include_once($jspath.DS.'libraries'.DS.'core.php');
// Get CUser object
$user =& CFactory::getUser($userid);
$avatarUrl = $user->getThumbAvatar();
echo '<img src="'. $avatarUrl .'"/>';Getting a user's friend count
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$count = $user->getFriendCount();
echo '<span>Total friends: ' . $count . '</span>';Getting a user's status
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$status = $user->getStatus();
echo '<span>User Status: ' . $status . '</span>';Getting a user's display name
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$name = $user->getDisplayName();
echo '<span>User name: ' . $name . '</span>';Get user's online status
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$isOnline = $user->isOnline();
if( $isOnline )
{
echo '<span>User is online now!</span>';
}Get user's view count
$jspath = JPATH_ROOT . DS . 'components' . DS . 'com_community';
include_once($jspath. DS . 'libraries' . DS . 'core.php');
// Get CUser object
$user = CFactory::getUser( $userid );
$count = $user->getViewCount();
echo '<span>Views: ' . $count . '</span>';Link to user personal profile page
$jspath = JPATH_ROOT.DS.'components'.DS.'com_community';
include_once($jspath.DS.'libraries'.DS.'core.php');
// Get CUser object
$link = CRoute::_('index.php?option=com_community&view=profile&userid='.$userid);
echo '<a href="'. $link .'">View user profile</a>';
