在Codeigniter中每次搜索后向表中添加行

时间:2014-12-24 09:29:44

标签: codeigniter loops

这是我的模型invoice.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class invoice extends CI_Model{

     function __construct(){
        parent::__construct();
    }

    public function search($keyword){
        $this->db->like('code',$keyword);
        $query = $this->db->get('products');
        return $query->result();
    }
}

和我的控制器search.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class search extends CI_Controller{

    function __construct(){  
        parent:: __construct();  
        $this->load->model('frontend/invoice');  
        $this->load->helper(array('form', 'url','html'));
    }  

    public function index(){
        $keyword = $this->input->post('keyword');
        $data['results'] = $this->invoice->search($keyword);
        $this->load->view('header');  
        $this->load->view('frontend/navafterlogin');
        $this->load->view('frontend/dashboard');
        $this->load->view('frontend/results_view',$data);
        //$this->load->view('frontend/invoicetable');
        $this->load->view('footer');
    }

}

和回显搜索结果的视图results_view.php

<table>
<?php
foreach($results as $row){ ?>
    <tr>
        <td><input type="text" value="<?php echo $row->name?>"/></td>
        <td><input type="text" id="rate" value="<?php echo $row->perunitprice?>"/></td>
        <td><input type="number" id="quantity"value="" placeholder="quantity"/></td>
        <td><input type="number" id="result" value="" placeholder="total"/></td>
    </tr>
<?php   
}
?>
</table>

和我搜索dashboard.php

的表单
<div class="row">
       <?php echo form_open('frontend/search');?>
            <div class="col-sm-2">
                <input type="text" class="form-control" name="keyword" placeholder="productcode">
            </div>

        <div class="col-sm-2">
             <button class="btn btn-warning">Find</button>
        </div>
    </div>

我想要的是每当我执行搜索时,我想在我的桌子上添加新行,在这种情况下是results_view并在单独的行中回显结果。目前我只能搜索一次和第二次搜索,第一个结果被第二个结果取代,我不希望它发生

1 个答案:

答案 0 :(得分:0)

您是否尝试使用“会话”,如果没有,请在您的控制器索引功能中尝试:

public function index(){
     $this->load->library('session');
     $keyword = $this->input->post('keyword');
     $result = $this->invoice->search($keyword);
     $search = $this->session->userdata('search') ? (array)$this->session->userdata('search') : array();
     array_push($search, $result);
     $this->session->set_userdata('search', $search);
     $data['results'] = $search;

     $this->load->view('header');  
     $this->load->view('frontend/navafterlogin');
     $this->load->view('frontend/dashboard');
     $this->load->view('frontend/results_view',$data);
     //$this->load->view('frontend/invoicetable');
     $this->load->view('footer');
}