Advertisement
cdsatrian

grouping sub kategory list

Dec 20th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2. // query data from database
  3. $pilihan=mysql_query(
  4.     "SELECT a.id_sub_kategori,b.id_kategori,b.nama_kategori,a.nama_subkategori
  5.     FROM subkategori a INNER JOIN kategori b ON a.id_kategori=b.id_kategori
  6.     WHERE b.aktif='Y'
  7.     ORDER BY b.id_kategori,a.id_subkategori"
  8. );
  9. // store given data to an array
  10. $opts=array();
  11. while($r=mysql_fetch_array($pilihan)){
  12.   $opts[]=$r;
  13. }
  14. $its_group=0;// its option new group?
  15. $group='';   // group label
  16. // build a list of options
  17. ?>
  18. <select name="subkategori>
  19. <option value="0" selected="selected">-- pilih kategori</option>
  20. <?php  
  21. for($i=0;$i<count($opts);$i++){
  22.   if($group==''|| $group!=$opts[$i]['nama_kategori']){
  23.     $group=$opts[$i]['nama_kategori'];
  24.     $its_group=1;
  25.     echo "<optgroup label=\"".$opts[$i]['nama_kategori']."\">\n"
  26.   }
  27.   echo "<option value=\"".$opts[$i]['id_subkategori']."\">"
  28.       .$opts[$i]['nama_subkategori']
  29.       ."</option>\n";
  30.   if($its_group){
  31.     echo "</optgroup>\n";
  32.     $its_group=0;
  33.   }
  34. }
  35. ?>
  36. </select>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement