'Group by catid SQL result in a selectbox

I have a RSform (3.0.18) in Joommla (3.10.9) on server PHP (7.4.21). One of the fields is a Dropdown (no multiselect) with a query SQL to get article from catid 15 OR 16. Thhe code below is given by RSForm documentation and adapted to my need.

//<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[] = "|Sélectionnez...[c]";
 
// Run the SQL query and store it in $results
$db->setQuery("SELECT id, title FROM #__content WHERE catid = 15 or catid = 16");
$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->title;
  $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>

The query works and I get a result like :

  • Sélectionnez...
  • Introduction aux médias et à la prise de parole en public
  • Réussir une prise de parole
  • Réussir une interview
  • Médiatraining élus

But I would like it group by catid, which could give something like :

  • Sélectionnez...
  • Enterprises formations (not selectable)
    • Introduction aux médias et à la prise de parole en public
    • Réussir une prise de parole
    • Réussir une interview
  • Administrations formations (not selectable)
    • Médiatraining élus

Any advice to help writing this ? Thanks in advance...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source