如何为我的代码使用html选择标记

时间:2012-07-22 11:19:25

标签: php html html-select

我有这个PHP代码

    <div id="offers-menu">      
   <?php
   function custom_get_categories_options($rootid = 0,$selected = false,$ident = 0) {
    $db = Bux::getDbInstance();
    $q = $db->query("SELECT * FROM ".clix_offers::$table['categories']." WHERE parent_id = ".(int)$rootid);
    $str = '';
    $ident++;
    while($row = $db->fetchAll($q)) {

      $str .= '<li '.(($_GET['cat_id'] == $row['id']) ? ' class="selected"':'' ).' ><a href="?cat_id='.$row['id'].'">'.$row['name'].'</a>';
      $str .= custom_get_categories_options($row['id'],$selected,$ident);
      $str .= '</li>';
    }

    if($str != "") return $ident>0?"<ol>{$str}</ol>":"{$str}";
   }
   ?>       

        <ul>
            <li <?php echo (!isset($_GET['cat_id']) || $_GET['cat_id'] == "all")?' class="selected" ':'';?>><a href="?cat_id=all">All Admin Offers</a></li>
                <?=custom_get_categories_options();?>

        </ul>

    </div>

输出就像那样

enter image description here

我想在此菜单中使用HTML select标记

这样的东西
<select>
  <option value="1">Advertisers</option>
  <option value="2" selected="selected">All Admin Offers</option>
  <option value="3">CPC/CPM</option>
  <option value="4">ect...</option>
</select> 

我自己无法正确地做到这一点

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

    <div id="offers-menu">      
   <?php
   function custom_get_categories_options($rootid = 0,$selected = false,$ident = 0) {
    $db = Bux::getDbInstance();
    $q = $db->query("SELECT * FROM ".clix_offers::$table['categories']." WHERE parent_id = ".(int)$rootid);
    $str = '';
    $ident++;
    while($row = $db->fetchAll($q)) {

      $str .= '<option value="'.$row['id'].'" '.'onClick="window.location = \'?cat_id='.$row['id'].'\'"';
      (($_GET['cat_id'] == $row['id']) ? $str .= 'selected="selected">' : $str .= '>');
      $str .= $row['name'].'</option>';
    }

    if($str != "") return $ident>0?"<ol>{$str}</ol>":"{$str}";
   }
   ?>       

        <select>
            <option onClick="window.location = '?cat_id=all'">All Admin Offers</option>
                <?=custom_get_categories_options();?>
        </select>

    </div>

答案 1 :(得分:0)

也许您可以查看HTML <optgroup /> tag

不幸的是,它不允许嵌套组或多级。