2016-09-14 84 views
0

好吧,所以我最近從PHP轉換到Codeigniter。我是新手,CI framework.This是我的PHP代碼:在Codeigniter的下拉列表中打印選定的值

<td> 
        <?php 
        include 'template_category.php'; 
        $categoryList = fetchCategoryTree(); 
        ?> 
        <select name="make"> 
        <?php foreach($categoryList as $cl) 
        { 
         if($cl["id"]!==$PRODUCTCATEGORY) 
         { 
          echo "Hey Matched!"; 
         } 
         else 
         {?> 
          <option selected value="<?php echo $cl["id"] ?>"><?php echo $cl["name"]; ?></option> 
         <?php } 

         ?> 
         <option value="<?php echo $cl["id"] ?>"><?php echo $cl["name"]; ?></option> 
        <?php } ?> 
        </select> 
       </td> 

現在,我怎麼轉換成CI的語法。我的意思是我不知道如何在Dropdown中提供條件。

<tr> 
    <td align="right"><? echo form_label('Category');?></td> 
    <?php $this->load->view('template_category'); $options= array(''=>'',''=>'');$categoryList = fetchCategoryTree(); ?> 
    <td><? echo form_dropdown(array('id' =>'address','name' =>'address')); ?><br/></td> 
</tr> 

如何讓我的CI代碼像上面提到的PHP一樣工作。深深地感受到:我不知道如何給CI裏面的下拉條件提供條件。

+0

什麼是同上? .. – Exception

+0

我的意思是完全相同 –

+0

請參閱https://www.codeigniter.com/docs – Exception

回答

1

希望這會幫助你。進一步參考 https://www.codeigniter.com/userguide3/helpers/form_helper.html

<?php 
include 'template_category.php'; 
$categoryList = fetchCategoryTree(); 
$select_values = array(); 
foreach($categoryList as $cl){ 
    $select_values[$cl["id"]] = $cl["name"]; 
} 
echo form_dropdown('make', $select_values, $PRODUCTCATEGORY); 
?>