在Codeigniter的下拉列表中打印选定的值

时间:2016-09-14 13:22:14

标签: php codeigniter continuous-integration dropdown

好的,所以我最近从PHP Switted到Codeigniter。我是CI框架的新手。这是我的PHP代码:

<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代码像ditto一样工作如上所述PHP。 深刻地说:我不知道如何在CI中提供条件。

1 个答案:

答案 0 :(得分: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);
?>