Multi level category in multiselect of CodeIgniter

2019-08-02 07:27发布

问题:

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' |
+----+---------+


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>

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

回答1:

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...