• 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: Autopopulate list with data from multiple fields

Autopopulate list with data from multiple fields 10 years 1 month ago #30789

I have the following code used to populate a drop down list from a table that has a column "name" that stores the persons name in the Last, First format all in a single column.

//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Please Select[c]";
 
// Run the SQL query and store it in $results
$db->setQuery("SELECT id, name FROM #__cemeterydata_cemeterylots");
$results = $db->loadObjectList();
 
// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. m|M-sized T-shirt
foreach ($results as $result) {
  $value = $result->id;
  $label = $result->name;
  $items[] = $value.'|'.$label;
}
 
// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);
 
// Now we need to return the value to the field
return $items;
//</code>

This works fine if the database table stores the persons name in a single database table column as spoken of above; however, what about when you have a database table that has one column "Last" for last name and a second column "First" for first name. How would change the code above to take the $label and have it equal "Last" with a ", " then "First" so that the output in the drop down list would show something similar to the following:

Benson, George
Kennedy, Robin
Marr, Kenneth

Thanks in advance for your help!

Sincerely,

George Benson
Sincerely,

George Benson
President - The Service Center, INC.
The administrator has disabled public write access.

Autopopulate list with data from multiple fields 10 years 1 month ago #30797

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
Basically, you need to select the third column as well and in the foreach include it as you wish.

e.g.
 foreach ($results as $result) {
  $value = $result->id;
  $label = $result->name.' '.$result->lastname;
  $items[] = $value.'|'.$label;
}

PS: in the Query make sure you get the Last Name as well.
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.
The following user(s) said Thank You: servicecenterinc

Autopopulate list with data from multiple fields 10 years 1 month ago #30801

I am not sure how to format the Query? Would I do a second line like below?
$db->setQuery("SELECT id, name FROM #__cemeterydata_cemeterylots");
$db->setQuery("SELECT id, lastname FROM #__cemeterydata_cemeterylots");

Thanks in advance for your help!

Sincerely,

George
Sincerely,

George Benson
President - The Service Center, INC.
The administrator has disabled public write access.

Autopopulate list with data from multiple fields 10 years 1 month ago #30806

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
here you are taking data from 2 columns :
$db->setQuery("SELECT id, name FROM #__cemeterydata_cemeterylots");
$results = $db->loadObjectList();

You can add another column as per that syntax:
$db->setQuery("SELECT id, name, lastname FROM #__cemeterydata_cemeterylots");
$results = $db->loadObjectList();
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.
The following user(s) said Thank You: servicecenterinc

Autopopulate list with data from multiple fields 10 years 1 month ago #30821

I should have noticed that!! Well I feel rather silly now!! Hey thanks for helping a newbie out!!

Sincerely,

George Benson
Sincerely,

George Benson
President - The Service Center, INC.
The administrator has disabled public write access.

Autopopulate list with data from multiple fields 10 years 1 month ago #30836

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
Glad that I could help!
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.

Autopopulate list with data from multiple fields 10 years 2 weeks ago #31038

I noticed an issue today with the resulting list. When you click on the drop down list it shows the data but it does not appear to be in alphabetical order. You can see the issue when you go to my website at: http://town.thomaston.me.us/public/index.php/community/cemetery-data-menu/form-to-report-error and look at the "What Gravesite" drop down list.

Any suggestions would be greatly appreciated!!

Sincerely,

George Benson
Sincerely,

George Benson
President - The Service Center, INC.
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!