Search Data in Codeigniter [closed]

2019-02-11 09:44发布

问题:

Can you help to create a simple search in Codeigniter. i don`t know how. please.

this is my database name tbl_dash

tbl_dash

id  |   name    |   Note
_________________________
1   |   erwin   |   Baker
2   |   flores  |   Coke

please help me!:(

回答1:

Here is how you can do it.
View

<form action="<?php echo site_url('search/search_keyword');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" value = "Search" />
</form>

Controller

Class Search Extends CI_Contrller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mymodel');
    }

    function search_keyword()
    {
        $keyword    =   $this->input->post('keyword');
        $data['results']    =   $this->mymodel->search($keyword);
        $this->load->view('result_view',$data);
    }

}

Model

Class Mymodel Extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

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

EDITS :
Here is the view to display data. result_view.php

<table>
<?php foreach($results as $row){ ?>
    <tr>
        <td><?php echo $row->Company?></td>
        <td><?php echo $row->Source?></td>
        <td><?php echo $row->SavePitch?></td>
        <td><?php echo $row->Results?></td>
        <td><?php echo $row->Status?></td>
        <td><?php echo $row->user_id?></td>
    </tr>
<?php } ?>
</table>


回答2:

Steps to achieve this:

1) Create a view which should contain search form with text field name and submit button.

2) Validate the field and post the value into a controller.

3) Get the posted value in controller and try to validate in server side.

4) Pass the value to a model.

5) In model, generate a select query which should fetch the required information according to the search input and return the fetched values to controller.

6) From controller pass the results to whatever the view you want.

7) Display the results in view page.



回答3:

You need to read active record class docs,

$this->db->where('name','erwin');
$result=$this->db->get('tbl_dash');
$this->load->library('table');
//HTML table in $table now;
$table=$this->table->generate($result);


标签: codeigniter