想要在codeigniter中的动态表行

时间:2016-02-25 18:44:52

标签: php codeigniter

我是codeigniter的新手。我正在尝试在搜索框下创建一个产品信息表,其中表行将根据每个产品型号搜索从数据库动态增加。现在以前的搜索数据被新搜索结果替换,只显示一个产品数据,但我想在表格行中显示所有以前的搜索结果。我尝试使用会话但不工作。谢谢你的帮助。

Model:
class Product_Model extends CI_Model {


  function product_search($model)
    {

        $this->db->like('modelno',$model);
        $query  =   $this->db->get('alliteam');



        if($query->num_rows() > 0){

      foreach ($query->result() as $row)
      {
        $data[] = $row;
      }
      return $data;
    }   


    }
}


Controller:

 public function sell(){
        $this->load->library('session');
               $data = array(
            'title' => 'New Customer',
            'action' => site_url('Product/sell'),
            'button' => 'Add Item'
        );

        $model = $this->input->post('model',TRUE);

        $data['productinfo'] = $this->Product_Model->product_search($model);



    $this->session->set_userdata($productinfo);

    $this->load->view('customer',$this->session->all_userdata());



      }

查看

  <form  action="<?php echo $action;?>" method="post">
                  <fieldset>
                    <div class="control-group">

                        <label class="control-label" >Model No :</label>
                        <div >
                          <input type="text"  name="model" value="" >
                          <span> Enter a product model no</span>
                        </div>

                    <div class="form-actions">

                      <input type="submit"  name="add_item" value="<?php echo $button;?>"/>

                    </div>
                  </fieldset>
    </form>   



                <table >
                  <thead>
                      <tr>
                          <th>Name</th>
                          <th>Model Number</th>
                          <th>Description</th>

                      </tr>
                  </thead>   
                  <tbody>


                    <?php 
                    if($this->input->post('add_item')){
                    foreach($productinfo as $product):?>
                    <tr>

                        <td class="center"><?php echo $product->name ?></td><br>                                
                        <td class="center"><?php echo $product->modelno ?></td>
                        <td class="center"><?php echo $product->product_description?></td>
                </tr>
                     <?php endforeach; } ?>
                  </tbody>
              </table>            

1 个答案:

答案 0 :(得分:0)

您无需在此处使用会话。

编辑您的控制器

public function sell(){
    $data = array(
            'title' => 'New Customer',
            'action' => site_url('Product/sell'),
            'button' => 'Add Item'
        );

        $model = $this->input->post('model',TRUE);

        $data['productinfo'] = $this->Product_Model->product_search($model);



    $this->load->view('customer',$data);



      }