• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: 1DB query to populate several fields?IsItPossible?

1DB query to populate several fields?IsItPossible? 11 years 10 months ago #23794

  • anthony.burge
  • anthony.burge's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 16
  • Thank you received: 3
Question
Is it possible to auto-populate several fields while only making one DataBase query?

For example, I'd like to get an array and use it to pre-fill several fields.

I know how to pre-populate a field based on the documentation here http://www.rsjoomla.com/support/documentation/view-article/77-display-php-variables-by-default-when-form-is-shown.html

but I've been unable to access the array from multiple fields. Any help would be much appreciated.
The administrator has disabled public write access.

1DB query to populate several fields?IsItPossible? 11 years 10 months ago #23835

  • anthony.burge
  • anthony.burge's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 16
  • Thank you received: 3
I've found a way to populate several fields with minimal DB queries.

In the section "Script called on form display" I do my database call and get the values I need
then I need to edit $formLayout. I've done that with the below code.
<?php		
/* 
		Below code allows you to pre-populate fields from a DB. $replace and $with can be arrays, allowing you to make many changes at once. //Use id="nameofcomponent' value="desired value" for textinput
*/
$replace=array('id="firstname"','value="via mail"' ,'value="I have a classroom."');
$with=array('id="firstname" value="value gotten from the database"', 'value="via mail" checked="checked"','selected="selected" value="I have a classroom."');
$formLayout = str_replace($replace, $with, $formLayout);
 
?>

CODE EXPLANATION
There are 3 different field types being altered in the code above. A textinput field, a checkbox field and a dropdown field.
'firstname' is the name of a textinput field. 'via mail' is one of the options that you can tick in the checkbox field. By adding 'checked="checked" ' for 'via mail' you are ticking the 'via mail' option.
'I have a classroom' is one of the options in the dropdown field. By adding 'selected="selected" value="I have a classroom."' you are causing the option 'I have a classroom' to be selected.

We search $formLayout for 'id="firstname"' then replace it with 'id="firstname" value="value gotten from the database"'

To understand how $formLayout looks prior to editing, I recommend installing WebDeveloper in Google Chrome.
Then choose 'View Source' once the form has loaded.

I tried Viewing the source in Firefox with WebDeveloper but for some reason it didn't work properly.
Hope this helps someone.
Last Edit: 11 years 10 months ago by anthony.burge. Reason: Added extra information.
The administrator has disabled public write access.
The following user(s) said Thank You: avalonshelties

1DB query to populate several fields?IsItPossible? 11 years 10 months ago #23924

  • houfton
  • houfton's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
  • Thank you received: 3
I have used another approach. I create session variables to store values for each field in the table and then return them as default values for various fields in the form.

This has to been done in an external script rather than in the form's PHP blocks because the session variables must be created before $formLayout is.

I have used the RSForm! Pro Content plugin to place the form in an article (I think there is a non-Pro version) and the Jumi extension (which I use anyway) to insert the session script before it is called.

I have also added a script in the form to unset the variables once they have been used.

This may be a bit laborious but it avoids lots of database queries and you do not need to list every field / value individually. I have made the Jumi script easily re-usable for other forms.
The administrator has disabled public write access.
The following user(s) said Thank You: anthony.burge, avalonshelties, kdurrum

1DB query to populate several fields?IsItPossible? 11 years 10 months ago #23930

  • anthony.burge
  • anthony.burge's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 16
  • Thank you received: 3
That's an interesting idea. I hadn't thought of using session variables.
Thanks for that!
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!