如何使用ajax和codegniter进行条件搜索

时间:2018-03-17 18:41:27

标签: php mysql ajax codeigniter

我想使用codeigniter框架在特定条件下进行搜索 当我按下我的观点时,情况确实不会发生 ($this->db->where('del_status','1');) 我认为我在查询数据库时遇到的问题请帮助我 我的数据库模型

public function SearchData($tablename,$data)
        {
            //select * from users where username like'%ss%' AND del_status ='1'

            $this->db->select("*");
            $this->db->from($tablename);
            $this->db->like('username', $data);
            $this->db->or_like('fullname', $data);
            $this->db->or_like('email', $data);
            $this->db->where('del_status','1');
            $sql = $this->db->get();
            return $sql->result();

        }

这是我的控制器

public function Search()
        {
            if($this->input->post("action") =="searchonusers" )
            {
                $data=$this->input->post("data");
                $this->load->model("DatabaseModel");

                $serachdata=$this->DatabaseModel->SearchData("users",$data);
                if(is_array($serachdata)&&count($serachdata)>0)
                {
                    echo '<div class="col-lg-12">
                <div class="userdataa table-responsive">
                    <table class=" table table-responsive table-bordered table-hover ">
                        <tr>
                            <th>id</th>
                            <th>user name</th>
                            <th>full name</th>
                            <th> image</th>
                            <th>email</th>
                            <th>type</th>
                            <th>status</th>
                            <th>date</th>
                            <th>time</th>
                            <th>delete</th>
                            <th>edit</th>
                            <th>Activate</th>
                        </tr>';

                    foreach ($serachdata as $user):
                        echo'<tr id="'."user".$user->id.'">
                            <td>'.$user->id.'</td>
                            <td>'.$user->username.'</td>
                            <td>'.$user->fullname.'</td>
                            <td><img  class="img-responsive" height="40%" width="40%" src="'.base_url()."upload/".$user->image.'"></td>
                            <td>'.$user->email.'</td>';
                        $usertype='';
                        if($user->type== '1'){
                            $usertype=" <label class='label label-warning'>User</label>";
                        }else{
                            $usertype="<label class='label label-danger '> Admin</label>";
                        }
                        echo '<td>'.$usertype.'</td>';


                        $acativate='';
                        $status='';
                        $regstatus=$user->status;
                        if($regstatus == '0'){
                            $acativate='<button class="btn btn-success btn-xs showsta" status="'.$user->status.'" id="'.$user->id.'">enable</button>';
                            $status='<label class="label label-danger">disactive</label>';

                        }else{
                            $acativate='<button class="btn btn-xs btn-danger showsta" status="'.$user->status.'" id="'.$user->id.'">disable</button>';
                            $status='<label class=" label label-success">active</label>';

                        }



                        echo' <td>'.$status.'</td>

             <td>'.$user->date.'</td>
             <td>'.$user->time.'</td>
            <td><button class="deleteuserfirst btn btn-danger btn-xs"  id="'.$user->id.'"><span class="glyphicon glyphicon-trash"></span>delete</button></td>
            <td><button type="button" class="btn btn-primary btn-xs edituser" id="'.$user->id.'" data-toggle="modal" data-target="#usereditorwe"><span class="glyphicon glyphicon-edit">Edit</button>
            <td> '.$acativate.'</td>






                        </tr>';

                    endforeach;


                   echo'</table>
                </div>
            </div>';



                }else{
                    echo '<div class="col-lg-12">
                <div class="userdataa table-responsive">
                    <table class=" table table-responsive table-bordered table-hover">
                        <tr>
                            <th>id</th>
                            <th>user name</th>
                            <th>full name</th>
                            <th> image</th>
                            <th>email</th>
                            <th>type</th>
                            <th>status</th>
                            <th>date</th>
                            <th>time</th>
                            <th>delete</th>
                            <th>edit</th>
                            <th>Activate</th>
                        </tr>
                        <tr>
                        <td colspan="12">No Data Found</td>
                        </tr>

                    </table>
                    </div>';


                }

            }else{
                exit();
            }
        }


    }

这是我的ajax代码

$(&#34; .serchdata&#34;)。on(&#34; click&#34;,function(){

var action="searchonusers";
var data=$("#dataa").val();
if(data ==""){
    alert("Please Insert Your Search Data ... ")
}else{
    $.ajax({
        url:"<?php echo base_url("Users/Search")?>",
        method: "post",
        data:{action:action,data:data},
        success:function (data) {
            $(".userdata").remove();
            $(".box").remove();
            $("#pagination_link").remove();
            $(".serchresult").html(data)
            Table();
        }
    })
}

});

2 个答案:

答案 0 :(得分:0)

您正在检查对象类型is_array上的result()

if(is_array($serachdata)&&count($serachdata)>0)  

要检查数组,请改用result_array()

return $sql->result_array();

答案 1 :(得分:0)

请替换为您的模型

public function SearchData($tablename,$data)
        {
            //select * from users where username like'%ss%' AND del_status ='1'

            $this->db->select("*");
            $this->db->from($tablename);
            $this->db->like('username', $data);
            $this->db->or_like('fullname', $data);
            $this->db->or_like('email', $data);
            $this->db->where('del_status','1');
            $sql = $this->db->get()->result_array();
            return $sql;
        }