Posted By

cshaiku on 06/30/09


Tagged

menu select html array


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

cshaiku
vali29


Create Select Menu from an Array


 / Published in: PHP
 

URL: http://code.cshaiku.com/code_php_create_select_menu.php

  1. function createSelectMenu($menuName, $menuArray, $selectedID = '1')
  2. {
  3. $csmBuffer ="<select name=\"$menuName\" class=\"select-control\">\n";
  4. settype($menuArray, 'array');
  5. $arrayCount = count($menuArray);
  6.  
  7. for ($i = 0; $i < $arrayCount; $i++) {
  8. if ($selectedID == $menuArray[$i]->id)
  9. $csmBuffer .= '<option class="select-current" value="' . $menuArray[$i]->id . '" SELECTED>' . stripslashes($menuArray[$i]->name) . '</option>' . "\n";
  10. else
  11. $csmBuffer .= '<option value="' . $menuArray[$i]->id . '">' . stripslashes($menuArray[$i]->name) . '</option>' . "\n";
  12. }
  13.  
  14. $csmBuffer .= "</select>\n";
  15. return $csmBuffer;
  16. }
  17.  
  18. $item1->id = 0; $item1->name = 'Item 1';
  19. $item2->id = 1; $item2->name = 'Item 2';
  20. $item3->id = 2; $item3->name = 'Item 3';
  21.  
  22. $menuArray[0] = $item1;
  23. $menuArray[1] = $item2;
  24. $menuArray[2] = $item3;
  25.  
  26. $menu = CreateSelectMenu('menu', $menuArray, 1); // Select second item for example
  27. echo $menu;

Report this snippet  

You need to login to post a comment.