使用Codeigniter搜索表单 [英] Search form with Codeigniter

查看:98
本文介绍了使用Codeigniter搜索表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Codeigniter中创建一个搜索表单。我想给用户4个不同的选项来搜索。对于结果,我想要一个表,显示在我正在搜索的表中的所有11列。这是我到目前为止的代码。

I am trying to create a search form in Codeigniter. I want to give the user 4 different options to search from. For the result I want a table that displays all 11 columns in the table I am searching from. This is the code I have so far.

控制器:

public function index(){
    $this->searchTest();
}
public function searchTest(){
    $this->load->model('reg_model');

$search_term = array(
    'firstName' => $this->input->post('firstName'),
    'lastName' => $this->input->post('lastName'),
    'street' => $this->input->post('street'),
    'dob' => $this->input->post('dob'));


    $data['query'] = $this->reg_model->test($search_term);


    $this->load->view("reg_header");
    $this->load->view("reg_nav");
    $this->load->view("reg_search_form", $data);
    $this->load->view("reg_search", $data); 

型号:

public function test($search_term='default'){



   $this->db->select('*');
   $this->db->from('voterinfo');
   $this->db->like('firstName', $search_term);
   $this->db->or_like('lastName', $search_term);
   $this->db->or_like('street', $search_term);
   $this->db->or_like('dob', $search_term);
   $query = $this->db->get();


   return $query->result_array();

}

查看:

<?php $this->load->library('table'); foreach ($query as $row){ echo $this->table->generate($row); }?>


推荐答案

如果要使用codeigniter table class ,应该是这样的:

If you want to put in table using codeigniter table class , it should be something like this:

$this->load->library('table');

$this->table->set_heading(array('Name', 'Color', 'Size')); //your column name


foreach($query as $row ){
   $this->table->add_row($row);
}

echo $this->table->generate(); 

您还需要修改模型。你的查询是错误的,你忘了密钥,应该是这样:

you need also to modify your model. You're query is wrong you forgot the key , it should be like this:

public function test($search_term='default'){



   $this->db->select('*');
   $this->db->from('voterinfo');
   $this->db->like('firstName', $search_term['firstName']);
   $this->db->or_like('lastName', $search_term['lastName']);
   $this->db->or_like('street', $search_term['street']);
   $this->db->or_like('dob', $search_term['dob']);
   $query = $this->db->get();


   return $query->result_array();

}

这篇关于使用Codeigniter搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆