how make to form search in CodeIgniter using two fields - codeigniter

I want to make a search form in Codeigniter using two fields.
But I'm confused about making the controller and model.
View:
echo form_open("home/pencarian","class='form-inline navbar-search'");
<input id="srchFld" class="srchTxt" name="keyword" type="text" value=" echo $this->session->userdata("keyword");" />
<select class="srchTxt" name="kategori">
<option>All</option>
foreach ($kat as $kategori){
$id = $kategori['id'];
$nama_kategori = $kategori['nama_kategori'];
$created_at = $kategori['created_at'];
<option value=" echo $id; ?>"><?php echo $nama_kategori; </option>
}
</select>
echo form_submit('action', 'Cari', "class='btn btn-primary'");
echo form_close();
How do I make the controller and model?

Model example_model.php
public function search($search_txt, $search_category) {
$this->load->database();
$sql = "select * from search_table where search_field = ? and search_category = ?;";
$query = $this->db->query($sql, array($search_txt, $search_category));
return $query->result();
}
Controller home
public function pencarian() {
$this->load->model('example_model', 'm');
$search_txt = $this->input->post('srchFld');
$search_category = $this->input->post('kategori');
$data['search_result'] = $this->m->search($search_txt, $search_category);
$this->load->view('example_view', $data);
}
View example_view
<?php foreach($search_result as $r): ?>
<div><?php echo $r->field1 . ' ' . $r->field2; ?></div>
<?php endforeach; ?>

Related

How do i retrieve images from folder which is linked to an item id?

The main issue is with the model, i cant figure out how to join the two tables so that i can display an item with its image which are stored in different locations.
model
$this->db->select('bs_items.id, bs_items.description, bs_items.title,
bs_items.touch_count,bs_items.price');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$this->db->from('bs_items');
Controller:
this->load->model('Popular_car');
$this->load->model('Recent_car');
$data['popular_cars']= $this->Popular_car->popular_car();
$data['recent_cars']=$this->Recent_car->get_listing();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('pages/home_recent', $data);
$this->load->view('templates/footer');
Please update your question with some code you have done , or database structure. i'm giving you the answer based on what i understood using your given code snippet.
Model
function get_items()
{
$this->db->select('*');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$query = $this->db->get();
return $query->result();
}
Controller
function get_allitems()
{
$this->load->model('model_name');
$data['items'] = $this->model_name->get_items();
$this->load->view('view_name', $data);
}
View
<?php foreach ($items as $row): ?>
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->touch_count;?></p>
<p><?php echo $row->price;?></p>
<p><?php echo $row->description;?></p>
<img src="<?php echo base_url().'path_of_image/'.$row->image_from_db; ?>
<?php endforeach; ?>
$myString = $this->db->get_where('profile_table' , array('id' => $edit ) )->row()->file_name_galery;
// $json11 = '{ "1":"s1.jpg", "2":"s2.jpg", "3":"s4.jpg", "4":"s4.jpg", "5":"s5.jpg" }';
$array = json_decode($myString);
if (empty($array)) {
echo '<p class="tx-danger"> NO IMAGE..</p>';
}
foreach($array as $key => $photo) { <div class="column_galery text-center zoom_img_app">
<a onclick="selected_photo(' echo $photo; ')" data-toggle="modal" data-target="#edit_image_mod" >
<img class="" src="http://yourapp.com/uploads/profile_gallery/ echo $photo; " style="width:100%"> <span class="tx-danger">DELETE</span> </a>
</div> }

How to autocomplete select option in CodeIgniter

I'm trying to make a select option in CodeIgniter, and want to make it autocomplete fill. How do I do that?
This is my code:
Model:
public function get_nama_customer(){
$data = array();
$query = $this->db->get('tabel_customer');
if($query->num_rows() > 0){
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Controller:
public function add_data()
{
$this->load->model('model_tabel_material');
$data['tabel_customer'] = $this->model_tabel_material->get_nama_customer();
$this->load->view('datamaster/tabel_material/v_add_material', $data);
}
public function do_add()
{
$nama_customer = $_POST['nama_customer'];
$data_add = array(
"nama_customer" => $nama_customer,
);
$temp = $this->model_tabel_material->insert_data('tabel_material', $data_add);
if($temp >=1){
redirect('tabel_material/controller_tabel_material/index');
}
}
View:
<select class="form-control" name="nama_customer" id="nama_customer" required>
<?php if(count($tabel_customer)){ ?>
<option value=''>--Pilih Customer--</option>
<?php foreach ($tabel_customer as $list){ ?>
<?php
echo "<option value='".$list['nama_customer']."'>".$list['nama_customer']."</option>";
?>
<?php } ?>
<?php } ?>
</select>

A PHP error was encountered severity:trying to get non-object file name:

view part where the error occured
<select name="standard" class="form-control">
<option <?php if($course->medium =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course->medium =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>
</select>
model part
public static function getStandard($id){
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
foreach($res->result() as $value) {
return $value;
}
}
controller part
public function add_standard($action='',$id='') {
$this->load_syles();
$course = array();
if ($action == 'edit' and $id != '' and is_numeric($id)) {
$course = $this->news_model->getStandard($id);
}
$this->data['course'] = $course ;
$this->data['title'] = 'Add Standard';
$this->data['page_header'] = "Add Standard";
$this->data['page_header_desc'] = "Add Standard";
$details=$this->news_model->viewAllStandard();
$this->data['details']=$details;
$this->data['count']=count($details);
$this->data['is_edit']=$action;
You wrote
$course = array()
and then used
$course->medium
as if it was an object
you should use $course['medium'] in the view(html)
Change your model like
public static function getStandard($id)
{
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
return $res->row_array();
}
Change $course->medium to $course['medium']
<select name="standard" class="form-control">
<option <?php if($course['medium'] =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course$course['medium'] =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>

submit multiple inputs within a forloop in codeigniter

My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is :
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result();
}
My view is:
foreach ($questions as $id => $row) :
?>
<?php
echo "<h5>".$row->question;
echo "<br>";
echo "from : ".$row->user_name."</h5>";
echo date('Y-m-d H:i');
echo "<br>";
$q_no='save'.$row->id;
$ans_no='answer'.$row->id;
echo "<h4> Answer:</h4>";
echo form_open('control_panel');
?>
<textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
<input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
<input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
<?php
echo form_error($ans_no);
echo "
<div class='form-group'>
<div >
<label class='checkbox-inline'>
<input type='checkbox' name='add_faq' value='yes' />
Adding to FAQ page .
</label>
</div>
</div>
<p>";
?>
<input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
<?php
echo 'answer'.$row->id;
?>
<hr>
<?php endforeach; ?>
and my controller is :
$this->load->model('control_panel');
$data['questions']=$this->control_panel->get_questions();
$data['no_of_questions']=count($data['questions']);
if($this->input->post($q_no))
{
$this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');
if($this->form_validation->run())
{
/* code to insert answer in database */
}
}
of course it did not work with me :
i get errors :
Severity: Notice
Message: Undefined variable: q_no
i do not know how to fix it
I am using codeigniter as i said in the headline.
In your controller on your post() you have a variable called q_no you need to set variable that's why not picking it up.
I do not think name="" in input can have php code I think it has to be text only.
Also would be best to add for each in controller and the call it into view.
Please make sure on controller you do some thing like
$q_no = $this->input->post('q_no');
$ans_no = $this->input->post('ans_no');
Below is how I most likely would do lay out
For each Example On Controller
$this->load->model('control_panel');
$data['no_of_questions'] = $this->db->count_all('my_table');
$data['questions'] = array();
$results = $this->control_panel->get_questions();
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'q_no' => $result['q_no'],
'ans_no' => $result['ans_no']
);
}
//Then validation
$this->load->library('form_validation');
$this->form_validation->set_rules('q_no', '', 'required');
$this->form_validation->set_rules('ans_no', '', 'required');
if ($this->input->post('q_no')) { // Would Not Do It This Way
if ($this->form_validation->run() == TRUE) {
// Run Database Insert / Update
// Redirect or load same view
} else {
// Run False
$this->load->view('your view', $data);
}
}
Example On View
<?php foreach ($questions as $question) {?>
<input type="text" name="id" value="<?php echo $question['question_id'];?>"/>
<input type="text" name="q_no" value"<?php echo $question['q_no'];?>"/>
<input type="text"name="a_no" value="<?php echo $question['a_no'];?>"/>
<?php }?>
Model
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result_array();
}

create a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec
Model:
function get_sec_info(){
$this->db->select('sec_id,name');
$records = $this->db->get('section');
return $records->result();
}
Controller:
function sec_ifo(){
$this->load->model('mymodel');
$this->data['red'] = $this->mymodel->get_sec_info();
$this->load->view('article',$this->data);
}
View:
<select name="section">
<?php foreach($red as $row) { ?>
<option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
Model
public function getClasse() {
$query(`enter code here`);
$result = $this->db->query($query)->result_array();
foreach ($result as $key => $rows) {
$resultado[] = $rows['DescricaoClasse'];
}
return $resultado;
}
Controller:
public function getClasse() {
$this->load->model('Decisao_monocratica_model');
return $this->Decisao_monocratica_model->getClasse();
}
View
<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
<option value="0">Todos os Tipos</option>
<? foreach ($classeProcesso as $key => $classe) { ?>
<option value="<? echo $classe ?>"><? echo $classe ?></option>
<? } ?>
</select>

Resources