6

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.

Controller:

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

Model:

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

View:

<?php
$this->load->library('table');
foreach ($query as $row) {
    echo $this->table->generate($row);
}
?>
1
  • so what's the problem? Commented Nov 20, 2013 at 2:12

2 Answers 2

4

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

}
Sign up to request clarification or add additional context in comments.

4 Comments

this created the table but I am still unable to get the information from my database from the forms I created.
Thanks that worked. Another question I have is how would I go about choosing a voter and editing the information in a form
@medellin81391 that should be in another question :)
I created a new question here is the link: stackoverflow.com/questions/20330770/…
0

According to the CodeIgniter documentation, you can pass the whole result set object or a 2d array into the generate() method of the system\libraries\Table class.

Generally, I do not return result set objects from my model, so I'll demonstrate passing a 2d array.

Your $search_term is an associative array, but your model method's lone parameter falls back to a string value and the whole parameter is being used as the LIKE value for each column. This just doesn't make any sense at all. Fortunately, or_like() will happily receive an associative array and produce the intended logic.

$this->input->post() is quite versatile. If you pass it a whitelist array of field names, it will populate an associative array with null values where the whitelisted key was not found in the payload.

Controller:

public function index()
{
    $this->load->model('reg_model', 'regModel');
   
    $data['voters'] = $this->regModel->voters(
        $this->input->post(['firstName', 'lastName', 'street', 'dob'])
    );

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

Model - returning the CI_DB_result or an array of arrays is fine, but an array of objects will not work:

public function voters(array $searchTerms): array
{
    return $this->db
        ->or_like($searchTerms)
        ->get('voterinfo')
        ->result_array();
}

View - with customized table tag attributes and column headings from database table: (Demo)

<?php
if (!$voters) {
    echo "<p>No voters found from search terms</p>";
} else {
    $this->load->library('table', [
        'table_open' => '<table border="1" cellpadding="4" cellspacing="0">'
    ]);
    echo $this->table
             ->set_heading(array_keys($voters[0]))
             ->generate($voters);
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.