category_select_options.php 618 B

12345678910111213141516
  1. <?php
  2. function category_select_options( $categories, $current=[], $prefix="" ){
  3. $response = "";
  4. $current = (array) $current;
  5. foreach( $categories as $category ){
  6. $selected = ( in_array( $category->id, $current ) ) ? "selected='selected'" : "";
  7. $title = $category->title;
  8. if( $prefix != "" )
  9. $title = $prefix . " " . $title;
  10. $response .= "<option value='{$category->id}' {$selected}>{$title}</option>";
  11. if( $category->has('children') )
  12. $response .= category_select_options($category->children, $current, $prefix . "-");
  13. }
  14. return $response;
  15. }