I'm new to CodeIgniter and PHP.
I have search box and I have two views in my project related to this. My first header.php view has search input box and in second view search.php is displaying results. Here I need to get search value to display like (10 results found for "search value should be here").
Here is my header.php view:
<div class="left">
<div class="content _relative row-fluid">
<a id="menuCollection" role="button" class="dropdown-toggle" data-toggle="dropdown">
<input type="text" id="search" class="span12" placeholder="Search" autocomplete="off"/>
<i class="modern-pictograms-search">s</i>
</a>
</div>
</div>
Here is my search.php view.
<div class="search-result font-large"><?php echo count($value); ?> results found
<span class="font-bold">(this is the place i need to get search value)</span>
</div>
Please help me resolve this.
Follow this steps may be it will help you to solve your problem:
make chances in your files as i mention it.
header.php - view
<html>
...
<form action='controller/method' method='POST'>
<input type='text' name='search' />
<input type='submit' value='search' />
</form>
search.php - view
...
<div class='results'>
<?php foreach($results as $result){ ?>
<div><?php echo $result; ?></div>
<?php } ?>
</div>
...
search.php - Controller
class Search extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('database');
}
public function index(){
$this->db->select('*');
$this->db->from('yourtablename');
$this->db->where('yourcondition'.$this->input->post('search'));
$data['results']=$this->db->get();
$this->load->view('header');
$this->load->view('searchview',$data);
$this->load->view('footer');
}
Related
I'm developing a web application, and I want to pass variable called ID when the form method is post that linked to open other form but in the config/routes I'm using $routes[page_A][get] = 'Controller' not $routes[page_A][post] = 'Controller'.
I'm using Codeigniter framework here, I've tried to change the controller with $this->input->get('id') but it doesn't work and I don't have any idea whats really happen in my codes.
The Sender Form View code
<form action="<?= base_url().'progres_save'; ?>" method="POST">
<div class="form-group">
<div class="form-row">
<label for="idJobOrder">ID Job Order</label>
<input type="text" name="idJobOrder" class="form-control" value="<?php echo $rd[0]->kodejobglobal; ?>" readonly>
</div>
</div>
<div class="form-group">
<div class="form-row">
<a class="btn btn-primary col-xl-1 col-sm-1 mb-1 ml-auto mr-0 mr-md-2 my-0 my-md-3" href="job" id="back" role="button"><i class="fas fa-fw fa-arrow-left"></i> Back</a>
<button class="btn btn-primary btn-block col-xl-1 col-sm-1 mb-1 mr-0 mr-md-2 my-0 my-md-3">Save <i class="fa fa-fw fa-arrow-right"></i></button>
<input type="hidden" name="id" value="<?php echo $rd[0]->kodejobspesifik ?>">
</div>
</div>
</form>
The Sender Form Controller code
public function save()
{
$idglobal = $this->input->post('idJobOrder');
$data = array('jobnya' => $idglobal );
$this->Model_joborder->save_pg($data,'lapharian');
redirect('progres_material');
}
The Config Routes code
$route['progres_save']['get']='error';
$route['progres_save']['post']='save';
$route['progres_material']['get']='matused';
$route['progres_material']['post']='error';
The Recipient Form Controller code
public function matused()
{
$id = $this->input->get('id');
$data['rd'] = $this->Model_joborder->tampil2($id);
$data['fb'] = $this->Model_joborder->data_cbb();
$this->load->view('matused', $data);
}
The Recipient Form View code
<form method="POST" action="<?= base_url().'matsave'; ?>">
<div class="form-group">
<div class="form-row">
<?php if (isset($rd[0])) {?>
<input type="hidden" value="<?php echo $rd[0]->jobspesifiknya; ?>" name="idClient" class="form-control" placeholder="First name" readonly>
<?php } ?>
</div>
</div>
</form>
What I expect is the input id value from Sender will be passed and catch on Recipient form as input idClient. Can anyone her help me to find out the solution? Thank you.
You can use PHP global variable $_REQUEST to capture the data if you are not sure about the request type like this,
public function matused()
{
$id = $_REQUEST['id'];
$data['rd'] = $this->Model_joborder->tampil2($id);
$data['fb'] = $this->Model_joborder->data_cbb();
$this->load->view('matused', $data);
}
You forgot to include the id data on the redirect after the save() method is called, so you will not get anything by calling $this->input->get('id').
To solve this, pass the id data along with the redirect :
redirect('progres_material?id=' . $this->input->post('id'));
But that of course it will gives you an extra parameter on the url. If you don't want additional parameter, you could alternatively use session to pass id data while redirecting, on CodeIgniter there is a method called set_flashdata to do this :
$this->session->set_flashdata('id', $this->input->post('id'));
redirect('progres_material');
And to get the id session data on the matused() method, use the following code :
$id = !empty($this->session->flashdata('id')) ? $this->session->flashdata('id') : $this->input->get('id');
I tried to make a search bar so users will be able to search products by searching on the product name but its not working and I'm not able to search a product.
This is my controller function:
<?php
class SearchController extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Search_model');
}
public function index(){
$data = array();
}
function search_keyword(){
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
//laad de view
$this->load->view('result_view',$data);
}
}
?>
This is my model function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Search_model extends CI_Model {
function __construct(){
parent::__construct();
}
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result_array();
}
}
?>
And this is my view file with searchbar:
<?php include_once('templates/header.php'); ?>
<center>
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?php echo base_url(); ?>SearchController/search_keyword" method="POST">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
<table>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo $row->product_naam?></td>
</tr>
<?php } ?>
</table>
<?php include_once('templates/footer.php'); ?>
I hope someone can help me with this problem!,
thanks
I don't think like() will help you much with your particular needs. Your best bet, I guess, would be to bypass the problem and use a where function containing your LIKE clause:
$this->db->select('product_naam')->from('products')
$this->db->where("product_naam LIKE '%$keyword%'")->get();
Or if you want pure ci you have to change your 'like' line :
$this->db->like('product_naam',$keyword);
For %$query you can use
$this->db->like('product_naam',$keyword,'before');
and for $query% you can use
$this->db->like('product_naam',$keyword,'after');
Because you use $query->result_array();, items should be taken by
<td><?php echo $row['product_naam']?></td>
result_array()
This method returns the query result as a pure array, or an empty
array when no result is produced
CI documentation
Hi i am having a job portal page where i will be displaying the list of the jobs and the users can apply for the jobs posted by clicking on apply now button a form will be where they need to fill their details etc..
The form contains position they are applying for, Name,Email,Mobile Number etc...
Position will be fetched automatically when clicking on applynow button.
Here i need to display all then jobs list in dropdown and the one which i have selected should be the first one in the text box but from my code it is displaying the total page as empty not displaying anything when we click on apply now..
Displaying the appplynow button(View):
<div class="applynow">Apply Now</div>
<div class="moreinfo" id="music" >More Info</div>
Controller(career/apply):
function apply($job_id)
{
$this->load->model('career_model');
$this->load->model('apply_model');
$data['joblist']=$this->apply_model->jobs_dropdown();
$data['records2']= $this->career_model->getcareerdatas($job_id);
$data['mainpage']='apply';
$this->load->view('templates/template',$data);
}
View:
<form name="applynow" id="applynow" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>apply/applynow" >
<div class ="applyus">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="applyposition ">
<input type="text" class="form-control positionapplied" name="positionapplied" id="positionapplied " value="<?php echo $r->job_name ;?>" readonly>
<?php
$joblist['']='--Select Category --';
$jobs_id="id='jobs_id' ";
echo form_dropdown('jobs_id',$joblist,$r->jobs_id,$jobs_id);
?>
</div>
<?php endforeach;endif;?>
<button type="submit" class="btn btn-success successss" id="sub" >Submit</button>
<a class="button cancel cancels" href="<?php echo site_url()?>career">Cancel</a>
<input type="reset" value="Reset" class="reset">
</div>
</form>
Model(apply_model):
function jobs_dropdown()
{
$this->table = 'jobs_list';
$this->where('status',1);
$joblist=$this->dropdown('jobs_id','job_name');
return $joblist;
}
In Error_Log got this error:
PHP Fatal error: Call to undefined method Apply_model::where() in /home/website/public_html/staging/application/models/apply_model.php on line 99
From the CodeIgniter documentation:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
}
This will make the database class available through the $this->db object.
So then in your model, you access the where() clause like so:
$this->db->where($blahblah);
Check their tutorial here: https://www.codeigniter.com/userguide3/tutorial/news_section.html
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Apply_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db = $this->load->database('default', true);
}
function jobs_dropdown()
{
$this->table = 'jobs_list';
$this->where('status',1);
$joblist=$this->dropdown('jobs_id','job_name');
return $joblist;
}
}
Save this as apply_model.php in application/model folder
I am currently trying to create a sample shopping site,The problem came when I created a search page.In my search page I Have two fields one for enter the keyword and other for select type of item
Which means search,keyword in the selected item.but the item defined in DB is 0 and 1 and i Trying to get it my code but fails...could any one help me...
The Gfunction code is
public static function item(){
$optionList='';
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
foreach($CategoryList as $catdata)
{ $optionList.="<option value='".$catdata['type']."'>".$catdata['name']."</option>"; }
return $optionList;
}
and view code is
<form action="<?php echo Yii::app()->baseUrl; ?>/offer?>" method="GET" class="form-inline form-section-2 row fadeInDown animated">
<div class="col-sm-5 form-group">
<input type="text" name="search" class="form-control" id="search" value="" placeholder="Enter Your Keyword">
</div>
<div class="col-sm-4 form-group">
<select name="cat" class="form-control selectpicker">
<option value='0'>Select type</option>
<?php echo GFunctions::item(); ?>
</select>
</div>
<div class="col-sm-3 form-group">
<button type="submit" class="btn btn-default btn-new">Search</button>
</div>
</form>
why don't you use Yii built in functions to handle the dropdown?
I am not sure how your Models looks like but you can choose one of the following functions to achieve what you want instead of trying to return a String $optionList with HTML tags.
CHtml::activeDropDownList() // for directly bind to active record model
CHtml::dropDownList()
Try this code....
Function
public static function item(){
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
$optionList = CHtml::listData($countries, 'type', 'name')
return $optionList;
}
In view
<?php echo CHtml::dropDownList('cat', '', GFunctions::item(), array('prompt'=>'Select type'));?>
I'm click my submit button freely without enter values in the form..then in my database a row inserted that columns contain zeros
controller
function submit_expense()
{
$exp_date=$this->input->post('expense_date');
$expense_ids= $this->input->post('expense');
$expense_ids=substr($expense_ids,0,strlen($expense_ids)-1);
$expense_id_array = explode(',',$expense_ids);
$id=$this->session->userdata('userid');
for($i=0;$i<count($expense_id_array);$i++)
{
$required_id = TRIM($expense_id_array[$i]);
$this->form_validation->set_error_delimiters('<div style="color:#B94A48">', '</div>');
$this->form_validation->set_rules('exp_amount_'.$required_id, 'Expense Amount', 'required|numeric');
$this->form_validation->set_rules('comment_'.$required_id, 'Comments', 'required|alpha');
if ( $this -> form_validation -> run() === FALSE )
{
$this->index();
}
else
{
$amount= $this->input->post('exp_amount_'.$required_id);
$comment=$this->input->post('comment_'.$required_id);
$expense_data=array(
'expense_id'=>$required_id,
'expense_amount'=>$amount,
'user_id'=>$id,
'date'=>$exp_date,
'comments'=>$comment,
);
$this->home_model->insert_expense($expense_data);
$this->session->set_flashdata('message', 'Successfully submitted.');
redirect(base_url().'home/index');
}
}
}
And also my validation is not working
footer_view
var new_String ='<div class="form-group" id="ex_'+unqid1+'">'
+'<label for="exampleInputPassword1">'+name+'</label> </br>'
+'<input name="exp_amount_'+unqid1+'" id="id_'+unqid1+'" type="text" '+
'class="form-control" placeholder="Enter '+name+' expense amount" style="margin-right:20px;" required>'
+'<input name="comment_'+unqid1+'" type="text" id="comment" class="form-con" placeholder="Comments" style="margin-right:20px;" required ></div >' ;
$("#create_exp").append(new_String);
view
this is my view use the id create_exp in my footer
<form role="form" action="<?echo base_url()?>home/submit_expense" method="post">
<?$todays_date=date('Y-m-d');?>
<input id="expense_date" name="expense_date" value="<?echo $todays_date;?>" type="hidden" />
<div class="red">
<? echo $this->session->flashdata('message'); ?>
</div>
<div class="box-body" id="create_exp">
<?php echo validation_errors(); ?>
<span>Choose from the right which all you need to enter for today</span>
<!--here goes the input boxes-->
<!-- /.input boxes -->
</div><!-- /.box-body -->
<span id="errmsg1"></span>
<input id="expenseVal" name="expense" type="hidden" class="form-control">
<div class="box-footer" id="button_id">
<button type="submit" class="btn btn-primary" >Submit</button>
</div>
</form>
As the OP Wants to show the all the Errors inside a div
<div id="error">
<?php echo validation_errors(); ?>
</div>
As the OP Wants some additional info,
To include a view or common page
$this->load->view('yourownpage');
Then you should have yourownpage.php inside your views folder
To have pagination, Have this in your controller's function
$this->load->library('pagination');
$config['base_url'] = 'yourownpage';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
Note :
Make sure to place the error div in the place that didn't hurt your textbox