codeigniter框架使用笔记,查询功能,搜索功能php版。自己留个记录
控制器Zhaopin.php
<?php session_start(); class Zhaopin extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); $this->load->model('zhaopin_model'); //引入CI的分页类 $this->load->library('pagination'); } //默认显示招聘列表页面,默认从第一天数据开始 public function index($start = 0) { //每页显示20条数据 $per_page = 20; //分段取出数据 $data['content'] = $this->zhaopin_model->getzhaopin($id = NULL, $per_page, $start); //获得数据总条目 $count_all = $this->zhaopin_model->getrow(); //配置分页参数 $config['base_url'] = base_url() . 'index.php/zhaopin/index/'; $config['total_rows'] = $count_all; $config['per_page'] = $per_page; //设置url参数是页数,不是默认的数据条数 //$config['use_page_numbers'] = TRUE; $config['full_tag_open'] = '<ul>'; $config['full_tag_close'] = '</ul>'; $config['first_link'] = '首页'; $config['last_link'] = '末页'; $config['first_tag_open'] = '<li>'; $config['first_tag_close'] = '</li>'; $config['last_tag_open'] = '<li>'; $config['last_tag_close'] = '</li>'; $config['next_link'] = '>'; $config['next_tag_open'] = '<li>'; $config['next_tag_close'] = '</li>'; $config['prev_link'] = '<'; $config['prev_tag_open'] = '<li>'; $config['prev_tag_close'] = '</li>'; $config['num_tag_open'] = '<li>'; $config['num_tag_close'] = '</li>'; //这个有点意思,开始没想到直接把class加进来,选中的css样式一直调不好 $config['cur_tag_open'] = '<li><a class="current">'; $config['cur_tag_close'] = '</a></li>'; $config['attributes'] = array('class' => 'myclass'); //根据配置,初始化分页类 $this->pagination->initialize($config); //输出分页,优化样式查参考手册 $data['pagination'] = $this->pagination->create_links(); //渲染试图输出 $this->load->view('zhaopin', $data); } //查看具体一条数据页面 public function view($id = NULL) { $data['content'] = $this->zhaopin_model->getzhaopin($id); if (empty($data['content'])) { show_404(); } $data['error'] = ''; $this->load->view('xiangqing', $data); } //ajax读取数据 public function search($id = NULL) { $data = $this->zhaopin_model->getzhaopin($id); //ajax获取结果需要json格式 echo json_encode($data, JSON_UNESCAPED_UNICODE); } //搜索功能,带分页 public function lookingfor($start = 0) { //搜索结果页,继续翻页POST值不会传递,选中用session存结果传递, //没有if判断还是会由于POST没有值而导致赋值语句出错。 if ($_POST) { $_SESSION['search_type'] = $_POST['search_type']; $_SESSION['search_content'] = $_POST['search_content']; } //设置每页20条 $per_page = 20; //分段取出数据, //返回值$data['content']['count']为查询结果数量 //返回值$data['content']['content']为查询结果数组 $data['content'] = $this->zhaopin_model->search($_SESSION['search_type'], $_SESSION['search_content'], $per_page, $start); //配置分页参数 $config['base_url'] = base_url() . 'index.php/zhaopin/lookingfor/'; $config['total_rows'] = $data['content']['count']; $config['per_page'] = $per_page; //设置url参数是页数,不是默认的数据条数 //$config['use_page_numbers'] = TRUE; $config['full_tag_open'] = '<ul>'; $config['full_tag_close'] = '</ul>'; $config['first_link'] = '首页'; $config['last_link'] = '末页'; $config['first_tag_open'] = '<li>'; $config['first_tag_close'] = '</li>'; $config['last_tag_open'] = '<li>'; $config['last_tag_close'] = '</li>'; $config['next_link'] = '>'; $config['next_tag_open'] = '<li>'; $config['next_tag_close'] = '</li>'; $config['prev_link'] = '<'; $config['prev_tag_open'] = '<li>'; $config['prev_tag_close'] = '</li>'; $config['num_tag_open'] = '<li>'; $config['num_tag_close'] = '</li>'; //这个有点意思,开始没想到直接把class加进来,选中的css样式一直调不好 $config['cur_tag_open'] = '<li><a class="current">'; $config['cur_tag_close'] = '</a></li>'; $config['attributes'] = array('class' => 'myclass'); //根据配置,初始化分页类 $this->pagination->initialize($config); //输出分页,优化样式查参考手册 $data['pagination'] = $this->pagination->create_links(); //渲染输出 $this->load->view('search', $data); } }
模型Zhaopin_model.php
<?php class Zhaopin_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } //快速读取所有数据 public function getzhaopin($id = NULL, $per_page = 20, $start = 0) { if ($id === NULL) { $query = $this->db->get('zhaopin', $per_page, $start); return $query->result_array(); } else { $query = $this->db->get_where('zhaopin', array('id' => $id)); return $query->row_array(); } } //搜索带分页 public function search($search_type, $search_content, $per_page = 20, $start = 0) { //手写语句 // $sql = "SELECT * FROM `zhaopin` WHERE ".$search_type." LIKE "."'%".$search_content."%'"; //直接得到结果集 // $query = $this->db->query($sql); //结果集转换为数组形式 //$query->result_array(); //使用CI快捷模式,SQL语句引号有点麻烦 $this->db->like($search_type, $search_content); $this->db->limit($per_page, $start); $this->db->from('zhaopin'); $query = $this->db->get(); $array = $query->result_array(); //获取查询结果条数 //有个坑,count_all_results()使用后select会清空,文档说明可以设置第二参数为FALSE,保留select,但测试没成功 $this->db->like($search_type, $search_content); $this->db->from('zhaopin'); $count = $this->db->count_all_results(); //返回多个结果,利用数组,在控制器中在数组里提取 return array('count' => $count, 'content' => $array); } //获取表行数 public function getrow() { $query = $this->db->count_all('zhaopin'); return $query; } }
前端view/zhaopin.php
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>佳源集团2018年中高层人才招聘</title> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/common.css"> <script src="<?php echo base_url(); ?>resource/js/jquery-3.3.1.min.js"></script> </head> <body class="red"> <div class="container-fluid"> <h1 align="center">佳源集团2018年中高层人才招聘</h1> <div class="pagination"><?php echo $pagination; ?></div> <form class="form-inline" action="http://localhost/ci/index.php/zhaopin/lookingfor" method="post"> <div class="form-group"> <label for="input">搜索</label> <select class="form-control" name="search_type"> <option name="id" value="id" selected="selected">序号</option> <option name="postion" value="postion">岗位</option> <option name="location" value="location">地点</option> <option name="num" value="num">人数</option> <option name="request" value="request">任职资格</option> </select> <input id="id" type="text" name="search_content" class="form-control" placeholder="搜索内容"> </div> <button type="submit" class="btn btn-default" id="search">搜索</button> <button type="button" class="btn btn-default" id="add">添加</button> </form> <div id="result"></div> <div class="add"><p>AJAX内容</p></div> <table class="table"> <tr> <th>序号</th> <th>岗位</th> <th>人数</th> <th>任职资格</th> </tr> <?php foreach ($content as $item): ?> <tr onclick="window.location = 'http://localhost/ci/index.php/zhaopin/view/<?php echo $item['id']; ?>'"> <td><?php echo $item['id']; ?></td> <td><?php echo $item['postion']; ?></td> <td><?php echo $item['num']; ?></td> <td>详情点击</td> </tr> <?php endforeach; ?> </table> </div> <script> // $("#add").click(function(){ // $(".add").html('<h1>添加成功</h1>'); // }); // $(document).ready($("#search").click(function(){ // var ids = $("#id").val(); // $.ajax({ // method: "POST", // url: "http://localhost/ci/index.php/zhaopin/search/"+ids, // //data: {"id" : 2}, // dataType: 'json', // // dataType:"array", // success:function(e){ // //优化:使用ajax选择搜索项,搜索结果成列表,有分页 // $(".add").append('<p>序号:'+e.id+'</p><p>岗位:'+e.postion+'</p><p>人数:'+e.num+'</p><p>要求:'+e.request+'</p>'); // // alert(e); // // return true; // }, // error:function(e){ // $(".add").html('<p>fail</p>'); // // alert(e); // // return false; // } // })})); </script> </body> </html>
前端view/search.php 使用bootstrap框架,资源文件存放在根目录resource下
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>佳源集团2018年中高层人才招聘</title> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/common.css"> <script src="<?php echo base_url(); ?>resource/js/jquery-3.3.1.min.js"></script> </head> <body class="red"> <div class="container-fluid"> <h1 align="center">佳源集团2018年中高层人才招聘</h1> <div class="pagination"><?php echo $pagination; ?></div> <?php echo "共" . $content['count'] . "条" ?> <form class="form-inline" action="<?php echo base_url() ?>/index.php/zhaopin/lookingfor" method="post"> <div class="form-group"> <label for="input">搜索</label> <select class="form-control" name="search_type"> <option name="id" value="id" selected="selected">序号</option> <option name="postion" value="postion">岗位</option> <option name="location" value="location">地点</option> <option name="num" value="num">人数</option> <option name="request" value="request">任职资格</option> </select> <input id="id" type="text" name="search_content" class="form-control" placeholder="搜索内容"> </div> <button type="submit" class="btn btn-default" id="search">搜索</button> <button type="button" class="btn btn-default" id="add">添加</button> </form> <div id="result"></div> <div class="add"><p>AJAX内容</p></div> <table class="table"> <tr> <th>序号</th> <th>岗位</th> <th>人数</th> <th>任职资格</th> </tr> <?php foreach ($content['content'] as $item): ?> <tr onclick="window.location = 'http://localhost/ci/index.php/zhaopin/view/<?php echo $item['id']; ?>'"> <td><?php echo $item['id']; ?></td> <td><?php echo $item['postion']; ?></td> <td><?php echo $item['num']; ?></td> <td>详情点击</td> </tr> <?php endforeach; ?> </table> </div> <script> // $("#add").click(function(){ // $(".add").html('<h1>添加成功</h1>'); // }); // $(document).ready($("#search").click(function(){ // var ids = $("#id").val(); // $.ajax({ // method: "POST", // url: "http://localhost/ci/index.php/zhaopin/search/"+ids, // //data: {"id" : 2}, // dataType: 'json', // // dataType:"array", // success:function(e){ // //优化:使用ajax选择搜索项,搜索结果成列表,有分页 // $(".add").append('<p>序号:'+e.id+'</p><p>岗位:'+e.postion+'</p><p>人数:'+e.num+'</p><p>要求:'+e.request+'</p>'); // // alert(e); // // return true; // }, // error:function(e){ // $(".add").html('<p>fail</p>'); // // alert(e); // // return false; // } // })})); </script> </body> </html>
前端view/xiangqing.php
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>佳源集团2018年中高层人才招聘</title> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>resource/css/common.css"> <script src="<?php echo base_url(); ?>resource/js/jquery-3.3.1.min.js"></script> <script> $(document).ready(function () { $("#submit").click(function () { //alert($("#file").val()); if ($("#file").val()) { return true; } return false; }) }) </script> </head> <body class="red"> <div class="container-fluid"> <h1 align="center">佳源集团2018年中高层人才招聘</h1> <h4 align="center"><?php echo $content['id']; ?></h2> <h4>招聘岗位</h4> <p><?php echo $content['postion']; ?></p> <h4>工作地点</h4> <p><?php echo $content['location']; ?></p> <h4>任职资格</h4> <p><?php echo nl2br($content['request']); ?></p> <hr /> <a href="http://www.zjjiayuan.com.cn/apply.html" class="btn btn-primary btn-lg margin" role="button">投递简历</a> </div> </body> </html>
样式common.css
h1 { font-size: 4rem; padding-top: 3rem; padding-bottom: 2rem; } .center { width: 100%; text-align: center; } .red { background-color: rgb(138,32,36); color: white; font-size: 1.7rem; } h4 { padding-top: 2rem; font-size: 3rem; } p{ font-size: 2.7rem; } .margin { font-size: 4rem; height: 8rem; width: 20rem; margin: 50px auto; display: block; color: rgb(138,32,36); background-color: white; border-color: white; } .pagination{ /* border: 1px solid white;*/ } .myclass{ } .pagination ul{ /*因为使用display:flex;布局子元素margin就居中了, 以前的布局需要居中必须设置width*/ margin: 0 auto; } .pagination ul li{ float: left; list-style-type: none; margin: 0 4px; } .pagination ul li a{ display: block; padding: 3px 15px; background-color: #fff; } .pagination ul li a.current{ background-color: #4CAF50; } .pagination ul li a:hover:not(.current){ background-color: #ddd; text-decoration: none; }