欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

在循环里如何使用标签

程序员文章站 2022-03-31 09:43:27
...
如何在循环里插入<optgroup>?
$data = array (
  0 => 
  array (
    'id' => '1',
    'name' => 'WEB编程',
    'parentid' => '0',
  ),
  1 => 
  array (
    'id' => '2',
    'name' => 'PHP',
    'parentid' => '1',
  ),
  2 => 
  array (
    'id' => '3',
    'name' => 'Ajax',
    'parentid' => '1',
  ),
  3 => 
  array (
    'id' => '4',
    'name' => 'java',
    'parentid' => '1',
  ),
  4 => 
  array (
    'id' => '5',
    'name' => 'WinForm编程',
    'parentid' => '0',
  ),
  5 => 
  array (
    'id' => '6',
    'name' => 'VB',
    'parentid' => '5',
  ),
  6 => 
  array (
    'id' => '7',
    'name' => 'VC',
    'parentid' => '5',
  ),
);

这么一个数组,想得到下面的效果。

<select name="categorys">
    <optgroup label="WEB编程">
        <option value="2" >PHP</option>
        <option value="3" >Ajax</option>
        <option value="4" >java</option>
    </optgroup>
 
    <optgroup label="WinForm编程">
        <option value="6" >VB</option>
        <option value="7" >VC</option>
    </optgroup>
</select>

难点在于如何在foreach里判断把<option>包含在<optgroup>里,折腾了半天也没出办法。请高手指点如何完成。

$data = array (
  0 =>  array (
    'id' => '1',
    'name' => 'WEB编程',
    'parentid' => '0',
  ),
  1 => array (
    'id' => '2',
    'name' => 'PHP',
    'parentid' => '1',
  ),
  2 => array (
    'id' => '3',
    'name' => 'Ajax',
    'parentid' => '1',
  ),
  3 => array (
    'id' => '4',
    'name' => 'java',
    'parentid' => '1',
  ),
  4 => array (
    'id' => '5',
    'name' => 'WinForm编程',
    'parentid' => '0',
  ),
  5 => array (
    'id' => '6',
    'name' => 'VB',
    'parentid' => '5',
  ),
  6 => array (
    'id' => '7',
    'name' => 'VC',
    'parentid' => '5',
  ),
);
 
echo '<select name="categorys">';
foreach($data as $v) {
  if($v['parentid'] == 0)
    echo "<optgroup label='$v[name]'>";
  else
    echo "<option value='$v[id]' >$v[name]</option>";
}
echo '</select>';

唠叨大哥,你上面的操作还是缺了结束符啊</optgroup>

我就是在苦闷如何把这个</optgroup>加进去。

echo "<optgroup label='$v[name]'>$v[name]</optgroup>";

稍微改下就行了。

$data = array (
  0 => 
  array (
    'id' => '1',
    'name' => 'WEB编程',
    'parentid' => '0',
  ),
  1 => 
  array (
    'id' => '2',
    'name' => 'PHP',
    'parentid' => '1',
  ),
  2 => 
  array (
    'id' => '3',
    'name' => 'Ajax',
    'parentid' => '1',
  ),
  3 => 
  array (
    'id' => '4',
    'name' => 'java',
    'parentid' => '1',
  ),
  4 => 
  array (
    'id' => '5',
    'name' => 'WinForm编程',
    'parentid' => '0',
  ),
  5 => 
  array (
    'id' => '6',
    'name' => 'VB',
    'parentid' => '5',
  ),
  6 => 
  array (
    'id' => '7',
    'name' => 'VC',
    'parentid' => '5',
  ),
);
$tempArray = array();
foreach($data as $item){
    $tempArray[$item['parentid']][$item['id']] = $item['name'];
}
echo '<select name="categorys">';
foreach($tempArray[0] as $parentid=>$name){
    echo "<optgroup label=\"$name\">";
    foreach($tempArray[$parentid] as $id=>$name){
        echo "<option value=\"$id\" >$name</option>";
    }
    echo '</optgroup>';
}
echo '</select>';

只是foreach了三次怕效率较差,不知还没有有别的方法。

重做数组的话……

$data = array (
  array (
    'id' => '1',
    'name' => 'WEB编程',
    'parentid' => '0',
    'sub'=>
     array(
          array(
            'id' => '2',
            'name' => 'PHP',
            'parentid' => '1',
          ),
          array (
            'id' => '3',
            'name' => 'Ajax',
            'parentid' => '1',
          ),
          array (
            'id' => '4',
            'name' => 'java',
            'parentid' => '1',
          )
    )
  ),
  array (
    'id' => '5',
    'name' => 'WinForm编程',
    'parentid' => '0',
    'sub'=>
    array(
          array (
            'id' => '6',
            'name' => 'VB',
            'parentid' => '5',
          ),
          array (
            'id' => '7',
            'name' => 'VC',
            'parentid' => '5',
          )
    )
  )
);
echo '<select style="width:200px;">';
for($i=0,$il=count($data);$i<$il;$i++){
    echo '<optgroup label="'.$data[$i]['name'].'">';
    for($j=0,$jl=count($data[$i]['sub']);$j<$jl;$j++){
        echo '<option>'.$data[$i]['sub'][$j]['name'].'</option>';
    }
    echo '<optgroup>';
}
echo '</select>';

是吗?
哦,是少了个 </optgroup>
不过我测试代码时并没有感觉出有与没有的差异

一定要有的话,就加个开关变量。要多次循环干什么?

echo '<select name="categorys">';
$k = false;
foreach($data as $v) {
  if($v['parentid'] == 0) {
    echo "<optgroup label='$v[name]'>";
    $k = true;
  }else {
    if($k) echo '</optgroup>';
    echo "<option value='$v[id]' >$v[name]</option>";
    $k = false;
  }
}
echo '</select>';


以上就是在循环里如何使用<optgroup>标签的详细内容,更多请关注其它相关文章!