Search form with Codeigniter - php

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

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

Related

How to pass data controller to view in codeigniter

I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout.
this is my model
function fetchProfile($id,$page){
$query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
return $query->result();
}
this is my controller:
public function edit($id,$page){
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
$this->load->view('template_master',$data);
}
I am also trying to find a solution. I am passing user Id and another by URL (get method).
You are overwriting $data['query'] when you assign the array next:
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
Either do:
$data= array('content'=>'view_staff_edit');
$data['query'] = $this->StaffModel->fetchProfile($id,$page); // note position
Or:
$data = array(
'content' = 'view_staff_edit',
'query' => $this->StaffModel->fetchProfile($id,$page),
);
Access in view via $query and $content.
Unrelated:
You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){
Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time.
Controller
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id,$page),
'content' => 'view_staff_edit'
);
$this->load->view('template_master',$data);
}
Then access it on your View file
<h1><?php echo $content ?></h1>
<?php foreach($query as $result): ?>
<p><?php echo $result->id ?></p>
<?php endforeach; ?>
Try doing something like this:
public function edit($id,$page) {
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data['content']= 'view_staff_edit';
$this->load->view('template_master',$data);
}
YOUR MODEL
function fetchProfile($id){
return $this->db->get_where('Id' => $id)->result_array();
}
YOUR CONTROLLER
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id),
'content' => 'view_staff_edit',
);
$this->load->view('template_master',$data);
}
YOUR "template_master" VIEW
<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?>

Pre-populate update form with data from the database

UPDATE:
If, in the view, I do <?php echo $customer->first_name; ?> it outputs the first-name correctly.
On the same view file 'value' => set_value($customer->first_name) outputs nothing.
I am making a "Customers" CRUD application in CodeIgniter 3. I have an update form that I want to pre-populate with the data corresponding to the customer, already existent in he database.
The model looks like this:
class Customer extends CI_Model {
/*Lots
of
code*/
public function getAllCustomers($customer_id) {
$query = $this->db->get_where('customers', array('id' => $customer_id));
if ($query->num_rows() > 0) {
return $query->row();
}
}
}
The controller looks like this:
class Home extends CI_Controller {
/*Lots
of
code*/
public function edit($customer_id){
$this->load->model('Customer');
$customer = $this->Customer->getAllCustomers($customer_id);
$this->load->view('update', ['customer'=>$customer]);
}
}
In the view file (update.php) I have:
<?php echo form_input('first_name', '', [
'type' => 'text',
'id' => 'first_name',
'class' => 'form-control',
'value' => set_value($customer->first_name),
'placeholder' => 'First name',
]);
?>
The customer's first_name, although existent in the the database column called "first_name" does not pre-populate the form.
Why is this?
always debug using$this->db->last_query() to check your query executing correctly
if last_query() doesnot return nothing make sure your set 'save_queries' => TRUE in app/config/database.php
your query might return more than one result
change your model like this
public function getAllCustomers($customer_id) {
$q = $this->db->get_where('customers', array('id' => $customer_id));
log_message("error",$this->db->last_query()); //use this to get the complied query for debugging purpose
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
return FALSE;
}
if you are sure that your query might return only one result then check $customer_id has data in it
public function getAllCustomers($customer_id) {
if($customer_id){
$q = $this->db->get_where('customers', array('id' => $customer_id),1); //specify the limit if you want to get only one row
log_message("error",$this->db->last_query()); //use this to get the complied query for debugging purpose
if ($q->num_rows() > 0) {
return $q->row();
}
}
return FALSE;
}
That's how to do it:
<?php echo form_input('first_name', set_value('first_name', $customer->first_name),[
'id' => 'first_name',
'class' => 'form-control'
]);
?>

How to create a method where all crud operation will perform in codeigniter

This method does only save but i want it will do insert, update and delete in codeigniter
//Gallery Category CRUD Module
public function galleryCategory(){
if (!empty($_POST['gallery_cat_name'])){
$data = $this->input->post();
$data['gallery_cat_date'] = date("Y-m-d", strtotime(str_replace('/', '-', $this->input->post('gallery_cat_date'))));
//Data save
$response = $this->MyModel->save('gallery_category', $data);
if ($response) {
$sdata['success_alert'] = "Saved successfully";
}else{
$sdata['failure_alert'] = "Not Saved successfully";
}
$this->session->set_userdata($sdata);
redirect('back/galleryCategoryCreate');
}else{
$sdata['failure_alert'] = "Try again";
$this->session->set_userdata($sdata);
redirect('back/galleryCategoryCreate');
}
}
You don't need to create Model with your queries for basic crud operations.
CodeIgniter provides those as Query Builder class.
From CodeIgniter Documentation
Selecting Data
The following functions allow you to build SQL SELECT statements.
$this->db->get()
Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:
$query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable
Inserting Data
$this->db->insert()
Generates an insert string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
Updating Data
$this->db->update()
Generates an update string and runs the query based on the data you supply. You can pass an array or an object to the function. Here is an example using an array:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
// Produces:
//
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
Deleting Data
$this->db->delete()
Generates a delete SQL string and runs the query.
$this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id
The first parameter is the table name, the second is the where clause. You can also use the where() or or_where() functions instead of passing the data to the second parameter of the function:
$this->db->where('id', $id);
$this->db->delete('mytable');
// Produces:
// DELETE FROM mytable
// WHERE id = $id
You must refer the documentation once, there are a lot of helpers.
<?php
class Crud_Model extends CI_Model{
public function get_where($table,$where){
return $data = $this->db->where($where)->get($table)->result_array();
}
public function select($table){
return $data = $this->db->get($table)->result_array();
}
public function insert($table,$data){
return $query = $this->db->insert($table,$data);
}
function delete($table,$where){
return $del = $this->db->where($where)->delete($table);
}
public function edit($table,$where){
return $data = $this->db->where($where)->get($table)->result_array();
}
public function update($table,$data,$where){
return $query = $this->db->where($where)->update($table,$data);
}
}
?>

inserting data into database with condition/ codeigniter

I am a beginner in CodeIgniter and I want to insert a data in codeigniter with a where clause. Something like a query like this:
insert into tbl_check_up (bill_id) values ("bill_id") where check_up_id = "'.$check_up_id.'" ;
how can i convert it into codeigniter
Here is my codeigniter model:
var $tbl_check_up = 'tbl_check_up';
public function save_bill_checkup($check_up_id,$data) {
$this->db->insert($this->tbl_check_up,$data);
return $this->db->insert_id();
}
Here is my controller :
public function bill_id() {
$check_up_id = $this->input->post('check_up_id');
$data1 = array(
'bill_id' => $bill_id
);
$insert1 = $this->billing_model->save_bill_checkup($check_up_id,$data1);
}
Instead of insert, use update query.
$data = array(
'bill_id' => $bill_id
);
$this->db->where('check_up_id', $check_up_id);
$this->db->update('tbl_check_up ', $data);

cannot make relation in codeigniter

I want to make a relation table in table pelatih and table absen. But when I try to make a relation id_pelatih it always input number "3".
This is the controller:
function absen()
{
$this->load->library('user_agent');
$nim = $this->uri->segment(4);
$id_user = $this->session->userdata('user_id');
$id_pelatih = $id_pelatih;
$this->load->model('mabsensi');
$data = array(
'id_pelatih' => $this->mlogin->get_data_pelatih_by_id_user($id_user)->id_pelatih,
'nim' => $nim,
'tanggal' => date('Y-m-d H:i:s'),
'keterangan' => 'Hadir'
);
$this->mabsensi->insert_absensi($data);
redirect($this->agent->referrer());
}
This is the model
function get_data_pelatih_by_id_user($id_user)
{
$this->db->select('*')
->from('pelatih as a, user as u')
->where('a.id_user = u.id_user')
->limit(1);
return $this->db->get()->row();
}
Please tell me how to solved this.
Your model isn't correct. If you want to get data from different tables you should use JOIN. And you have to say which record you need. So, add a WHERE statement to your query.
function get_data_pelatih_by_id_user($id_user)
{
$this->db->SELECT('*')
->FROM('user')
->WHERE('id_user', $id_user) // your parameter, you did not use it
->JOIN('pelatih', 'pelatih.id_user = user.id_user');
return $this->db->get();
}
Also have a look at the documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html

Categories