Welcome Guest, Not a member yet? Register   Sign In
Help turning dataset into Form Dropdown
#1

[eluser]Chad Crowell[/eluser]
Code:
<?=$options = array(
     '' => 'In All Categories',
     foreach($categories as $category):
       '$category->shortname' => '$category->category_name',;
     endforeach;
     );
     echo form_dropdown('searchcat', $options);?>

My code is above- I am creating a dropdown with one static choice at the top, then I want to add all the records from the dataset $categories, using the shortname as the value and the category_name as the label. How do I code this so the two can interact?
#2

[eluser]Glen Swinfield[/eluser]
I'd make a helper probably like this;

Code:
function dropdown($categories){
  
   $ret = '<option value="">In All Categories</option>';

   foreach($categories as $category){
      $ret .= "<option value=\"{$category->shortname}\">{$category->category_name}</option>\n";
    } // end foreach
    
    return $ret;

} //end function

// Call function

echo dropdown($categories);

Just wrap the dropdown function in the correct html to begin and end the select box. - i think this answers your question?




Theme © iAndrew 2016 - Forum software by © MyBB