Keyword

Programmatically grabbing a list of item titles

  • Kelsey Brookes
  • Kelsey Brookes's Avatar Topic Author
  • Offline
  • Elite Member
More
7 years 11 months ago #155564 by Kelsey Brookes
Programmatically grabbing a list of item titles was created by Kelsey Brookes
Hi there,

I need to create a dropdown in RSForm pro with the names of available doctors on a medical site I'm doing.

While I could just build it manually in RSForm Pro, it means building a rod for the client's back as they (or I) will need to update it every time the staffing changes. They'll already have to make changes to the 'staff' section of the website in K2, so why not reduce the workload and do it programatically?

Ideally I'd pull the list of item titles from the Staff category programatically and add this to the form layout. I'm not sure if I'll need a hidden field to populate or if I can use an existing field type.

Any suggestions?

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 11 months ago #155574 by Krikor Boghossian
Replied by Krikor Boghossian on topic Programmatically grabbing a list of item titles
Is it possible to create fields or field types for RSForm?
Generally speaking it would be easy to do this in a PHP/HTML form through an SQL query and a foraech statement.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Kelsey Brookes
  • Kelsey Brookes's Avatar Topic Author
  • Offline
  • Elite Member
More
7 years 10 months ago #155672 by Kelsey Brookes
Replied by Kelsey Brookes on topic Programmatically grabbing a list of item titles
It can, but I can't see a way to programmatically create a field in RSForm Pro.

What I can do however is inject an option-select "manually" into the layout. All I really need is to be able to generate a list of item titles from a specific category and all its sub-categories.

On selection, the choice would then be mapped to a hidden field via javascript and this would be submitted with the rest of the form.

Or at least, that's the idea :)

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #155689 by Krikor Boghossian
Replied by Krikor Boghossian on topic Programmatically grabbing a list of item titles
You would have to query the db, I will create a small starting point for you.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Kelsey Brookes
  • Kelsey Brookes's Avatar Topic Author
  • Offline
  • Elite Member
More
7 years 10 months ago #155715 by Kelsey Brookes
Replied by Kelsey Brookes on topic Programmatically grabbing a list of item titles
Thanks Krikor, that's much appreciated!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago - 7 years 10 months ago #155726 by Krikor Boghossian
Replied by Krikor Boghossian on topic Programmatically grabbing a list of item titles
Something like this will do the trick. Remember to change the category's id.
<?php 
	$db = JFactory::getDBO();
	$query = "SELECT * FROM #__k2_items WHERE catid=1 AND published=1"; 
	$db->setQuery($query);
	$rows = $db->loadObjectList();
?>

<select>
	<option value="">- Select an item -</option>
	<?php foreach($rows as $row): ?>
	<option value="<?php echo $row->id; ?>"><?php echo $row->title; ?></option>
	<?php endforeach; ?>
</select>

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Last edit: 7 years 10 months ago by Krikor Boghossian.

Please Log in or Create an account to join the conversation.

  • Kelsey Brookes
  • Kelsey Brookes's Avatar Topic Author
  • Offline
  • Elite Member
More
7 years 10 months ago #155779 by Kelsey Brookes
Replied by Kelsey Brookes on topic Programmatically grabbing a list of item titles
Thanks Krikor, I'll see about making that work - much appreciated!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #155789 by Krikor Boghossian
Replied by Krikor Boghossian on topic Programmatically grabbing a list of item titles
You 're welcome Kelsey :)

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Kelsey Brookes
  • Kelsey Brookes's Avatar Topic Author
  • Offline
  • Elite Member
More
7 years 10 months ago #155820 by Kelsey Brookes
Replied by Kelsey Brookes on topic Programmatically grabbing a list of item titles
Thought I'd quickly update this with a note that this was even more easily achieved than I thought. RS Joomla came back to me with some suggestions:


- the PHP code can be added in the dropdown itself, within the items area. You can find an example on this topic here:

www.rsjoomla.com/support/documentation/rsform-pro/custom-scripting/auto-populate-a-list-from-a-table.html

- submitting the form will also capture the selected dropdown value, so the hidden field won't be needed, unless you're planning on using it for something else. More on the dropdown element here:

www.rsjoomla.com/support/documentation/rsform-pro/form-fields/dropdown.html

----

Using the above links as guides and with your code, Krikor, I was able to do away with the hidden field (as they suggested) and simply modify your code for a drop-down item.

Works perfectly!

The only thing I needed to modify was to change the catid=x statement to be catid IN (3,4,5) to specify multiple subcategories.

Thanks again!

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 10 months ago #155823 by Krikor Boghossian
Replied by Krikor Boghossian on topic Programmatically grabbing a list of item titles
I 'm so glad I could help :)

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum