在这里,我想动态输入字段

时间:2018-01-23 08:40:30

标签: php codeigniter dynamic

我希望根据代码中数据库中的字段名称动态获取输入字段。

查看页面

<?php echo form_open('index.php/product/save_edit') ?>
    <?php echo form_hidden('id',$product['id']) ?>
    <div class="values">Name</div><br>
    <?php echo form_input('name',$product['name']) ?>
    <br>

    <div class="values">Lenssensor</div><br>
    <?php echo form_input('lenssensor',$product['lenssensor']) ?>
    <br>

    <div class="values">Price</div><br>
    <?php echo form_input('price',$product['price']) ?>
    <br>

    <div class="values">Thickness</div><br>
    <?php echo form_input('thick',$product['thick']) ?>
    <br><br><br>
     <?php echo form_submit('submit', 'Submit'); ?> 
    <br>
<?php echo form_close(); ?>

控制器编辑代码功能

public function edit(){
  $this->load->database();
  $this->load->model('product_model');
  $data['product'] = $this->product_model->product($this->uri->segment(3))[0];
  $this->load->view('product_edit',$data);
}

1 个答案:

答案 0 :(得分:0)

将代码更改为以下

    <?php echo form_open('index.php/product/save_edit')

         $obj = new $this->product_model;
         foreach ($obj as $key => $value) {
         if($key == "id"){
         echo form_hidden($key,$value) 
          }
           else{    
         echo "<div class='values'>$key</div><br>".form_input($key,$value)."<br>" ;
        }
} ?>

                <br><br><br>
         <?php echo form_submit('submit', 'Submit'); ?> 
        <br>
    <?php echo form_close(); ?>