普通网友 2012-07-02 05:15
浏览 37

在php中创建下拉树

I have a table structure like following

|id| parent_id |name|

parent_id is referring to the same table recursively I tried to create a drop-down tree but unsuccessful. Please tell it how to create in core php or Cake php.

this is the work i did so far..But it give error 'Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 67025477 bytes) in /viren/webroot/upms/app/controllers/tests_controller.php on line 390';

function admin_takecat(){
$this->layout=false;
$this->render(false);
Configure::write('debug',2);

$firstlevel=$this->cats->find('list',array('fields'=>array('Category.id','Category.cat_name'),'conditions'=>array('Category.parent_id'=>0,'department_id'=>9)));
    $dropbox='<select>';
foreach($firstlevel as $id=>$val){
    $dropbox.='<option value='.$id.'>'.$val.'</option>';
    $count=$this->cats->find('count',array('conditions'=>array('Category.parent_id'=>0,'Category.department_id'=>9,'Category.parent_id'=>$id)));
    if($count>0){
    $dropbox=$this->_recursive($id,$dropbox,1);

    }

}
$dropbox.='</select>';
echo $dropbox;



}
function _recursive($catid,$dropbox,$level){

$listcats=$this->cats->find('list',array('fields'=>array('Category.id','Category.cat_name'),'conditions'=>array('Category.parent_id'=>0,'Category.department_id'=>9,'Category.parent_id'=>$catid)));
$mark='';
for($i=1;$i<=1;$i++){
    $mark.='-';

}
  • 写回答

1条回答 默认 最新

  • doupai6875 2012-07-02 05:25
    关注

    I'm not sure what you mean by "want the parent ids and child ids separated." Understand that the elements in a select are nothing more than a display value and a data value, and the data value will be what gets sent to the server along with the field name.

    So the select can't really distinguish between multiple kinds of data values. Each option can't have multiple IDs, for example. All you could do is format your display value to include IDs (such as you have with "1 - games - 0" and such), and decide which data value should be the value for that option in the select.

    One thing you can do is group option elements, using the optgroup tag. Something like this:

    <select name="myOptions">
      <optgroup label="Category 1">
        <option value="1">First Choice</option>
        <option value="2">Second Choice</option>
        <option value="3">Third Choice</option>
      </optgroup>
      <optgroup label="Category 2">
        <option value="4">Fourth Choice</option>
        <option value="5">Fifth Choice</option>
      </optgroup>
      <optgroup label="Category 3">
        <option value="6">Sixth Choice</option>
        <option value="7">Seventh Choice</option>
      </optgroup>
    </select>
    

    In validation, the browser shouldn't allow the user to select a group, they would have to select an actual option. So you shouldn't have to worry about that. This does present some additional flexibility in displaying your values more hierarchically. Understand, however, that only one level of option nesting is allowed in this case. (So an optgroup can't contain another optgroup.)

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题