简体   繁体   English

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

[英]How to retrieve data and display in the textarea of view after Search the data in Codeigniter

I am new to Codeigniter. 我是Codeigniter的新手。 I would like to create a module that after search the id, than it will retrieve data from the database and display the data in the textarea. 我想创建一个模块,在搜索ID之后,它将从数据库中检索数据并在textarea中显示数据。 I have no idea that how to retrieve data from database, and display the data in the textarea after click the 'Search' button. 我不知道如何从数据库中检索数据,并在单击“搜索”按钮后在文本区域中显示数据。 Hope someone can help me. 希望可以有人帮帮我。 Thank You. 谢谢。

This is the Model: 这是模型:

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();
   }
}

This is the Controller: 这是控制器:

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());
    }
}

This is the View: 这是视图:

<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>

try to Use Model Code for searching : 尝试使用模型代码进行搜索:

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

with the ajax you can achive your goal here is the some changes 使用ajax,您可以达到目标,这里是一些更改
Model : 型号:

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();
   }
}

Controller : 控制器:

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());
    }
}

Use the following code. 使用以下代码。 It works... 有用...

Controller: 控制器:

 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());
        }
    }

Model: 模型:

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

View page: 查看页面:

<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 } } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM