Forums

Populate Fields from Database

melvins138 28 Mar, 2017
This question seems to be asked a lot, but I am not finding an answer written simply enough to help a daft person like myself.

I am using Community Builder (CB), and have a donation form created in CFv5. When a person logs onto the site, then goes to the donation form, I would like certain information to load from CB for that user.

I have successfully been able to pull in information from the joomla users database table, using scripts I have found.


I used a custom html box and added:


	<div class="gcore-subinput-container" id="fitem">
		<label for="contact_name" class="control-label gcore-label-top">Full Name:</label>
			<div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-custom">
				<?php
				$user =& JFactory::getUser();
				$readonly = '';
					if ( $user->id > 0 ) {
						  $name = $user->name;
						  $readonly = "readonly='readonly'";
					}
				?>

				<div class="ccms_form_element cfdiv_text" id="email_container_div" style="">
  					<input name="ContactName" id="contact_name"  placeholder="" class="validate['required'] form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" value=" <?php echo $name; ?> " name="name" type="text" <?php echo $readonly; ?> />
					  <div class="clear"></div>
				</div>
			</div>
	</div>


This works great for the username from the joomla users table.

But now I want to pull in information from a different table. I have street address and other information in a CB table, that I would like to populate if they have information in that field.

I have looked at this article, but I am not certain how to actually implement it in a form.

Can someone walk me through the steps to populate information from a database table besides "users".

What additional information would be needed to help answer this question.

Thanks,
Melvins138
GreyHead 28 Mar, 2017
Hi Melvins,

Please follow the FAQ and let us know where you get stuck. If the CB Table uses the User ID to identify the records then you can set a Condition like this:
<?php
$user = \JFactory::getUser();
return array( 'user_id' => $user-id );
?>
Note: replace user_id with the correct column name if necessary.

Bob

[[>> later: corrected $user->id <<]]
melvins138 29 Mar, 2017
Hi Bob,

Okay here are my steps, and where I get confused or things go wrong.

1) Under "Setup", I add DB Read before HTML (Render Form)
Give it an action label "Street Address"
Enabled = "Yes"
Choose table name, in this case "joom_comprofiler"
Multi read = "No"
Enables Model ID = "Yes
Model ID = Data0
Fields = user_id,cb_streetaddress
Order = blank and Group = blank
Condition =
<?php
$user = \JFactory::getUser();
return array( 'cb_streetaddress' => $user-id() );
?>

At this point, If I check the form. It broken. I get an error that reads:
Whoops\Exception\ErrorException thrown with message "Call to undefined function id()"

Stacktrace:
#15 Whoops\Exception\ErrorException in site/administrator/components/com_chronoforms5/chronoforms/actions/db_read/db_read.php:85
#14 in site/administrator/components/com_chronoforms5/chronoforms/actions/db_read/db_read.php:85


If I remove the code from conditions, the form will load. Yes the form is blank, because I haven't added anything to "Designer".

So, I continue to step two.

2) Under "Designer", I add a custom field... I am not sure what I should here:

Should I add:


<div class="gcore-subinput-container" id="fitem">
		<label for="contact_name" class="control-label gcore-label-top">Full Name:</label>
			<div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-custom">
				<?php
				$user =& JFactory::getUser();
				$readonly = '';
					if ( $user->id > 0 ) {
						  $name = $user->name;
						  $readonly = "readonly='readonly'";
					}
				?>

				<div class="ccms_form_element cfdiv_text" id="email_container_div" style="">
  					<input name="ContactName" id="contact_name"  placeholder="" class="validate['required'] form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" value=" <?php echo $name; ?> " name="name" type="text" <?php echo $readonly; ?> />
					  <div class="clear"></div>
				</div>
			</div>
</div>


This is what I have on the other form where I just need the username from joom_users.

joom_comprofiler has a field for user_id that matches joomla's user_id... and I want to pull in the information in the cb_streetaddress field.

I'm so confused.

Thanks for the help,
Melvins138
GreyHead 29 Mar, 2017
Hi Melvis138,

Sorry that should be $user->id without the () at the end - I've corrected my earlier post.

You don't need all the custom code - if you add an ordinary text element and give it the same name as the data item that you are loading in the DB Read the value will be set for you e.g. Data0[username] - the Data0 is because you have a Model ID set, you may not need this.

Bob
Max_admin 30 Mar, 2017
Hi,

Another route would be to use the new v6, here are the steps:
#1- create a new "Empty form", drag a "Read data" action above the "Display section" action under "Events".
#2- in the new "Read data" action, set the "Model" to "Profile" (or whatever), select your table, set the "Select type" to "First matching record", and the where conditions to:
Profile.user_id:{user:id}

#3- Under the "Designer" add a new text field, in the "Value" field add this:
{var:read_data2.Profile.name}


Where "read_data2" is the name of the "Read data" function (its shown in a black background), and "name" is the name of the table field you want to load.

Best regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.