具有单个表的CodeIgniter的多选中的多级类别 [英] Multi level category in multiselect of CodeIgniter with single table

查看:54
本文介绍了具有单个表的CodeIgniter的多选中的多级类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从$ form-> multiselect()创建一个多级类别。


下面是我的表结构。

I am trying to create a multi level category from $form->multiselect().
Below is my table structure.

| id | parent |  name   |
|----|--------|---------|
| 10 |      0 | 'menu1' |
| 12 |     10 | 'menu2' |
| 13 |     10 | 'menu3' |
| 14 |      0 | 'menu4' |

结果为

The result is

+----+---------+
| id |  name   |
+----+---------+
| 10 | 'menu1' |
| 12 | 'menu2' |
| 13 | 'menu3' |
| 14 | 'menu4' |
+----+---------+



ID的值和名称显示在选择元素中。


the id showed for value and name for display in select element.

<select>
  <option value='10'>menu1</option>
  <option value='12'>menu2</option>
  <option value='13'>menu3</option>
  <option value='14'>menu4</option>
</select>



未显示层次结构。我想在下面显示。


not showed Hierarchy .I want to show below.

<select>
      <option value='10'>menu1</option>
      <option value='12'>menu1 -> menu2</option>
      <option value='13'>menu1 -> menu3</option>
      <option value='14'>menu4</option>
    </select>

我该怎么办?


我想操纵$ form_multiselect()的结构。

What should I do?
I would like to manipulated the structure of $form_multiselect().

推荐答案

我假设您在顶部列出的表格是按照您使用的格式存储在数据库中的。如果这是您尝试的方式(您没有发布表格名称,则必须这样做就是你自己):

I'm assuming the table you listed at the top is in a database by the format you got it in... If this is how you are getting it try (you didn't post your table names so you'll have to do that yourself):

$result = $this->db->select('CASE WHEN b.name IS NOT NULL THEN CONCAT(a.name, ,\' -> \' b.name) ELSE a.name END AS option')
    ->from('yourtable a')
    ->join('yourtable b', 'a.parent = b.id', 'left')
    ->get()->result();

如果您的问题误导了我,我表示歉意...没什么可继续的。

If your question has mislead me, I apologise... Wasn't much to go on...

这篇关于具有单个表的CodeIgniter的多选中的多级类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆