2017-04-26 86 views
1

我正在使用codeigniter樹枝 - 我試圖創建一個動態選擇框,其中包含數據庫中的數據。從樹枝陣列創建樹枝變量

這裏是我現在做

 // animals is $data['animals']=$this->loadmodel->some_function() 
     // animals passed from the controller ... 


      <select id="foo" class="form-control"> 
       <option selected="true" >Animals</option> 
        {% for animal_key in animals %} 
         <option > 
        {{ animal_key ["animal_description"] }} 
        </option> 
        {% endfor %} 
      </select> 

因此,所有上述運作良好。但是,如果我想使它動態呢?每一個選擇框讓控制器來自不同方法的數據 - 假設我有其他方法的數據 - 喜歡這裏

 // orders is $data['orders']=$this->loadmodel->some_function() 
     // orders passed from the controller ... 

      <select id="foo" class="form-control"> 
       <option selected="true" >orders</option> 
        {% for order_key in orders%} 
         <option > 
        {{ order_key ["order_description"] }} 
        </option> 
        {% endfor %} 
      </select> 

      <select id="foo" class="form-control"> 
       <option selected="true" >Animals</option> 
        {% for animal_key in animals %} 
         <option > 
        {{ animal_key ["animal_description"] }} 
        </option> 
        {% endfor %} 
      </select> 

我腦子裏想的是什麼做這樣的事情:

設置的數組控制器的名稱:

  {% set controller_names = ['animals','orders']%} 

設置控制器的陣列變量ND它們的鍵:

  {% set controller_vars = 
      ['animals'=>'animal_description','orders'=>'order_description']%} 

然後遍歷它這樣

  {% for names in controller_names %} 
       <select id="foo" class="form-control"> 
       <option selected="true" >{{ name }}</option> 
        {% for controller_key in controller_vars %} 
         <option > 
        {{ controller_vars [ controller_key] }} //suppose to be Twig variables 
        </option> 
        {% endfor %} 
      </select> 
     {% endfor %} 

所以我需要的是轉換集controller_vars數組變量樹枝(只要有可能).....

回答

0

你可以使用attribute功能:

   {% for controller_key in controller_vars %} 
        <option > 
        {{ attribute(controller_key, controller_vars) }} 
       </option> 
       {% endfor %} 

希望這有助於

+0

釷anks - 但它不起作用......請你給我一個擴展的例子嗎? – RoyBarOn

+0

嗨@RoyBarOn抱歉,我在示例中犯了一個錯誤:我顛倒了屬性函數的參數(我更新了我的答案),您可以試試嗎? – Matteo

+0

對不起 - 不起作用 - 我試圖「告訴」枝條,關鍵和值是PHP變量.... – RoyBarOn