在Codeigniter中搜索数据后,如何检索数据并在视图的文本区域中显示

时间:2019-02-21 04:41:44

标签: php codeigniter

我是Codeigniter的新手。 我想创建一个模块,在搜索ID之后,它将从数据库中检索数据并在textarea中显示数据。 我不知道如何从数据库中检索数据,并在单击“搜索”按钮后在文本区域中显示数据。 希望可以有人帮帮我。谢谢。

这是模型:

function search($code){
   $this->db->select('name','telno','address','introducer');
   $this->db->from('customer');
   $this->db->like('code',$code);
   $query = $this->db->get();
   if($query->num_rows() > 0){
    return $query->result();
   }else{
    return $query->result();
   }
}

这是控制器:

public function searchcus(){ 
    $this->load->model('Ordering_model');
    $pgcode = $this->input->post('search');
    if(isset($code) && !empty($code)){
        $data['customer'] = $this->Ordering_model->search($code);
        $this->load->view('ordering/index',$data);
    }else{
        redirect($this->index());
    }
}

这是视图:

<div class="container">
            <div class="col-md-6">
                <div class="form-group">
                    <div class="col-md-9">
                            <input type="text" class="form-control" name="search" id="search" placeholder="<?php echo "Customer's PG Code";?> " required />
                    </div>
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit" name="submit" value="Search" ><span class="glyphicon glyphicon-search"><?php echo "Search"; ?></span></button>
                    </span>
                    </div>
            </div>
            </form><br/>
    </div>
    <table style="width: 2000px;" class="table table-striped">
                 <div class="col-xs-4">
                    <div class="input-group">
                    <span class="input-group-addon">Name</span>
                    <input id="msg" type="text" class="form-control" name="msg" >
                    </div>
                </div>
                <br><br>
                <div class="col-xs-4">
                <div class="input-group">
                    <span class="input-group-addon">Tel</span>
                    <input id="msg" type="text" class="form-control" name="msg" >
                </div>
                </div>
                <br><br>
                <div class="col-xs-4">
                <div class="input-group">
                    <span class="input-group-addon">Introducer</span>
                    <input id="msg" type="text" class="form-control" name="msg" >
                </div>
                </div>
                <br><br>
                <div class="col-xs-4">
                <div class="input-group">
                    <span class="input-group-addon">Address</span>
                    <textarea class="form-control" rows="5" id="address"></textarea>
                </div>
                </div>
                <br>


    </table>

3 个答案:

答案 0 :(得分:0)

尝试使用模型代码进行搜索:

if($ code && $ code!=''){             $ where =“(TABLE_NAME.COLUMN LIKE'%”。$ code。“%')”;         }

答案 1 :(得分:0)

使用Ajax,您可以达到目标,这里是一些变化
型号:

function search($code){
   $this->db->select('name','telno','address','introducer');
   $this->db->from('customer');
   $this->db->like('code',$code);// by default it will run '%$code%' no change here
   $query = $this->db->get();
   if($query->num_rows() > 0){
        return $query->result();
   }else{
        return array();
   }
}

控制器:

public function searchcus(){ 
    $this->load->model('Ordering_model');
    $code = $this->input->post('search');//changes
    if(!empty($code)){//changes
        $data['customer'] = $this->Ordering_model->search($code);
        $this->load->view('ordering/index',$data);
    }else{
        redirect($this->index());
    }
}

答案 2 :(得分:0)

使用以下代码。可以...

控制器:

 public function searchcus() {
        $pgcode = $this->input->post('search');
        if (isset($pgcode) && !empty($pgcode)) {
            $data['customerr'] = $this->Ordering_model->search($pgcode);
            $this->load->view('viewpage', $data);
        } else {
            redirect($this->index());
        }
    }

型号:

   function search($pgcode) {
        $this->db->select('*');
        $this->db->from('customer');
        $this->db->like('code',$pgcode);
        return $this->db->get()->result();
    }

查看页面:

<div class="container">
    <form method="post" action="<?php echo base_url(); ?>index.php/welcome/searchcus">
            <div class="col-md-6">
                <div class="form-group">
                    <div class="col-md-9">
                            <input type="text" class="form-control" name="search" id="search" placeholder="<?php echo "Customer's PG Code";?> " required />
                    </div>
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit" name="submit" value="Search" ><span class="glyphicon glyphicon-search"><?php echo "Search"; ?></span></button>
                    </span>
                    </div>
            </div>
            </form><br/>
    </div>
<?php if(empty($customerr)) { } else { foreach($customerr as $row) { ?>
    <table style="width: 2000px;" class="table table-striped">
                 <div class="col-xs-4">
                    <div class="input-group">
                    <span class="input-group-addon">Name</span>
                    <input id="msg" type="text" class="form-control" name="msg" value="<?php echo $row->name ?>" >
                    </div>
                </div>
                <br><br>
                <div class="col-xs-4">
                <div class="input-group">
                    <span class="input-group-addon">Tel</span>
                    <input id="msg" type="text" class="form-control" name="msg" value="<?php echo $row->telno ?>" >
                </div>
                </div>
                <br><br>
                <div class="col-xs-4">
                <div class="input-group">
                    <span class="input-group-addon">Introducer</span>
                    <input id="msg" type="text" class="form-control" name="msg" value="<?php echo $row->introducer ?>" >
                </div>
                </div>
                <br><br>
                <div class="col-xs-4">
                <div class="input-group">
                    <span class="input-group-addon">Address</span>
                    <textarea class="form-control" rows="5" id="address"> <?php echo $row->address ?></textarea>
                </div>
                </div>
                <br>


    </table>
<?php } } ?>