Made a form self populating from VirtueMart database,.. but

alful 24 Feb, 2011
I am a newbie to PHP and Chronoforms but dedicated! After much research, I have managed to make a form which populates fields from the VirtueMart user info table, at first using a specific registered users ID to link to the relevant fields in the database.

Having completed this I then wanted to make this dynamic to the logged in users ID and although I have trawled the forum and tried "many" suggestions none seem to work.

I know I am very close and ask if anyone can advise what I am doing wrong. I have given the relevant extract of part of the form below, showing part my PHP code. The first line is my attempt to make it dynamic and the following 2 lines are some of the "specific" ID reference which populate perfectly.

<?php 
$db =& JFactory::getDBO();  
$my =& JFactory::getUser(); 
 
$db->setQuery('SELECT company FROM #__vm_user_info WHERE user_id = "$my->id"');  
$dbCompany = $db->loadResult(); 
$db->setQuery('SELECT first_name FROM #__vm_user_info WHERE user_id="62"');  
$dbfirstname = $db->loadResult();
$db->setQuery('SELECT last_name FROM #__vm_user_info WHERE user_id="62"');  
$dblastname = $db->loadResult();


I would really appreciate any response to this.
Thanks
GreyHead 24 Feb, 2011
Hi alful,

I think that there's a problem with the quotes here:
WHERE user_id = "$my->id"'
Please try
WHERE user_id = '.$my->id.';'
or, if you prefer
WHERE user_id = {$my->id};'

Bob
alful 24 Feb, 2011
The suggestion of WHERE user_id = '.$my->id.';' worked perfectly.

Thank you very much!
attilio_sal 09 Oct, 2012

<?php 
$db =& JFactory::getDBO();  
$my =& JFactory::getUser(); 
 
$db->setQuery('SELECT company FROM #__vm_user_info WHERE user_id = "$my->id"');  
$dbCompany = $db->loadResult(); 
$db->setQuery('SELECT first_name FROM #__vm_user_info WHERE user_id="62"');  
$dbfirstname = $db->loadResult();
$db->setQuery('SELECT last_name FROM #__vm_user_info WHERE user_id="62"');  
$dblastname = $db->loadResult();
?>



Thanks for the tip,
this is an improvement of your code
<?php 
$db =& JFactory::getDBO();  
$my =& JFactory::getUser(); 
$query='SELECT company,first_name,last_name FROM #__vm_user_info WHERE user_id = '.$my->id;
$db->setQuery($query);
$row = $db->loadObject();
?>


you can populate your fields whit this
<?php
$form->data['name'] = $row->first_name.' '.$row->last_name;
?>
GreyHead 09 Oct, 2012
Hi attilio_sal,

Very neat, thank you.

You can simplify one more step if you only need the full name:
$query = "
  SELECT `company`, CONCAT(`first_name`, ' ', `last_name`) AS `name` 
    FROM #__vm_user_info 
    WHERE `user_id` = {$my->id} ;
";

Bob
This topic is locked and no more replies can be posted.