【问题标题】:CakePHP show parent category of sub category in select boxCakePHP 在选择框中显示子类别的父类别
【发布时间】:2011-11-21 05:42:49
【问题描述】:

我正在尝试在 Cake 的选择框中的第一个子类别实例的顶部显示类别。

子类别属于类别,类别有很多子类别。有没有办法做到这一点?

我已经在选择框中尝试了 showParents=>true。

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    假设我正确理解您的问题,您想使用optgroup 标签?

    我不确定仅使用 belongsTo 关系时是否有自动执行此操作的方法。以下解决方案有点难看,但可以满足您的要求:

    控制器:

    /**
     * Get all categories and subcategories and declare selectBox array
     */
    $categories = $this->Subcategory->Category->find('all');
    $selectBox = array();    
    
    /**
     * Iterate over categories & subcategories creating a formatted 
     * array that works with Form->input() to create a select box with 'optgroups'
     */
    foreach ($categories as $category) {
      foreach ($category['Subcategory'] as $subcategory) {        
        $selectBox[$category['Category']['title']][$subcategory['id']] = $subcategory['title'];        
      }
    }
    
    $this->set('selectBox', $selectBox);    
    

    查看:

    <?php echo $this->Form->input('subcategory_id', array('options' => $selectBox)); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多