Forums

Database select query

MajorPayne112 22 Aug, 2010
Hey,

What i want to do is have a form that auto adds the users name, address and phone from a database.

I’ve currently have User Extended Fields for K2 install and with that i've been able to get all the fields required to show up within the profile. All the fields are stored within the table jos_k2_users with their real name stored in the field called "userName" and the address and phone stored within the field "plugins" (see below)

userExtendedFieldsaddress=123 fake street
userExtendedFieldscity=fake town
userExtendedFieldsstateOrProvince=fake state
userExtendedFieldszipCode=1234
userExtendedFieldscountry=fakeville
userExtendedFieldstelephone=1234567890
userExtendedFieldsmobile=1234567890


So what I’m wanting to do is have a form where you need to be logged in to use and the form to auto fill that data into the correct fields to save the user having to type it all out again.

I’m normally very good with html but I have no idea when it comes to databases so any help would be great!
GreyHead 22 Aug, 2010
Hi MajorPayne112,

Hmmm . . . it's a bit tricky. I'm sure that there is some K2 code that would do this for you.

Either you have to get the content of the plugins field and parse it (using a couple of explodes I think) to get the individual info.

Or you could create a new JRegister Object (basically a parameter set) and load the content into that. That's the neater code and probably what I would do.

Once you have the results in an array or an Object then you can add them to the input values in your form.

Bob
MajorPayne112 23 Aug, 2010
Thanks for the reply Bob, you wouldnt be able to provide some example code for a bases would you? As like i said previously when it comes to databases i have no idea.

Thanks
GreyHead 28 Aug, 2010
Hi MajorPayne112,

This is only partial code and may not work completely!! I don't have K2 to test with.

You'd get the data with
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
    SELECT `userName`, `plugins`
        FROM `#__k2_users`
        WHERE `user_id` = '".$user->id."' ;
";
$db->setQuery($query);
$data = $db->loadObject();
$user_params = new JRegistry();
$user_params->loadINI($data->plugins);
?>
. . .
<div>Your zipcode is <?php echo $user_params->get('userExtendedFieldszipCode'); ?></div>

Note: I guessed the column name for the 'user_id'

Bob
MajorPayne112 30 Aug, 2010
Thanks so much Bob, im going to have a play around with it and hopefully i can get it up and running.
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger