Filtering and searching in codeigniter - codeigniter

I am doing a filtering and searching in codeigniter but no idea! how to do that?
but i try my best but fail!!!
here i show the list which is shown by ajax that sucess
MY DATA BASE ARE
MY CONTROLLER IS
public function get_quick_search()
{
$sepcli= $this->input->post('spec');
$distct= $this->input->post('dist');
$locat= $this->input->post('locat');
$data['list'] = $this->Doctor_model->search_listing();
$data['quck_search'] = $this->search_model->get_quick_list($sepcli,$distct,$locat);
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$this->load->helper(array('form', 'url'));
$this->load->view('customer/header');
$this->load->view('customer/side_view',$data);
$this->load->view('customer/quick_search',$data);
$this->load->view('customer/footer');
}
MY MODEL LIKE
public function get_quick_list($locat,$distct,$sepcli)
{
$this->db->select('*');
$this->db->from('tbl_doctor');
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(district LIKE '$distct' AND place LIKE '$locat' AND spec_specialise LIKE '$sepcli')");
$query=$this->db->get()->result_array();
return $query;
}
HERE NO ERROR SHOW BUT THE RETURN QUERY IS NULL SHOW LIKE THIS (array(0) { })

you might try like this ,Your Controller function code
public function get_quick_search()
{
$s_data['sepcli'] = $this->input->post('spec');
$s_data['distct'] = $this->input->post('dist');
$s_data['locat'] = $this->input->post('locat');
$data['quck_search'] = $this->search_model->get_quick_list($s_data);
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$this->load->helper(array('form', 'url'));
$this->load->view('customer/header');
$this->load->view('customer/side_view',$data);
$this->load->view('customer/quick_search',$data);
$this->load->view('customer/footer');
}
your model function code
public function get_quick_list($s_data)
{
$this->db->select('td.*, ts.*')
$this->db->from('tbl_doctor as td');
$this->db->join('tbl_specialisation as ts', 'ts.spec_id = td.spec_id','left');
if($s_data['sepcli'] !="")
$this->db->like('ts.spec_specialise',$s_data['sepcli'],'both');
if($s_data['distct'] !="")
$this->db->like('td.district',$s_data['distct'],'both');
if($s_data['locat'] !="")
$this->db->like('td.place', $s_data['locat'], 'both');
$query=$this->db->get()->result_array();
return $query;
}
sure it will helps , you should try it !!!

Related

How to make a filter with query string in laravel

I want to make a filter with query params, here I want to make 3 where, but if one of them is not there, then it will not be a problem because it will display according to the filter only, and if there is no query string then it will display all data
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d')
->where('d.status','=',$request->status)
->orderBy('d.id')
->get();
return response()->json($vendor);
}
Take as reference, exact code might not work for you.
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d');
if (!empty($request->status_one)) {
$vendor = $vendor->where('d.status','=', $request->status_one);
}
if (!empty($request->status_two)) {
$vendor = $vendor->where('d.status','=', $request->status_two);
}
if (!empty($request->status_three)) {
$vendor = $vendor->where('d.status','=', $request->status_three);
}
if (empty($request->status_one) && empty($request->status_two) && empty($request->status_three)) {
$vendor= $vendor->where('d.status','=', $request->status_one)->where('d.status','=', $request->status_two)->where('d.status','=', $request->status_three);
}
$result = $vendor->orderBy('d.id')
->get();
return response()->json($result);
}
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d')
->when($request->status, function ($q, $status) {
return $q->where('d.status','=', $status);
})
->when($request->status_two, function ($q, $status_two) {
return $q->where('d.status_two','=', $status_two);
})
->orderBy('d.id')
->get();
return response()->json($vendor);
}

how to update file in laravel

I update two data one text data and two file data but it's not working this. How to update file?
My code is:
public function update(Request $request){
$news=new News();
$news->newstitle=$request->newstitle;
$url1=$this->imageExistStatus1($request);
$news->save();
return redirect()->back()->with('sms','insert successful');
}
public function imageExistStatus1($request){
$newsByid1=News::where('id',$request->newsid)->first();
$fimage1=$request->file('imageone');
if ($fimage1) {
unlink($newsByid1->imageone)
$thisName1= $fimage1->getClientOriginalName();
$uplodePath1='public/up/';
$fimage1->move($uplodePath1,$thisName1);
$url1=$uplodePath1.$thisName1;
}else{
$url1=$newsByid1->imageone;
}
return $url1;
}
First, change your code to the following
public function update(Request $request, $id)
{
$news = News::findOrFail($id);
$news->newstitle = $request->newstitle;
$fimage1 = $request->file('imageone');
if ($fimage1) {
unlink($news->imageone);
$news->imageone = $this->imageExistStatus1($request, $id);
}
$news->save();
return redirect()->back()->with('sms', 'insert successful');
}
public function imageExistStatus1($request)
{
$fimage1 = $request->file('imageone');
$thisName1 = $fimage1->getClientOriginalName();
$uplodePath1 = 'public/up/';
$fimage1->move($uplodePath1, $thisName1);
$url1 = $uplodePath1 . $thisName1;
return $url1;
}
I hope your problem is resolved
tip:
Thanks to "Oluwatobi Samuel Omisakin".
please use the route:
Route::patch('/news/{id}/update', 'NewsController#update')

laravel many to many with chaining where clause

I have a given table :
tools toolparts parts part_details
----- --------- ----- ------------
id* id* id* id*
name tool_id name part_id
part_id total (int)
----- --------- ----- ------------
the relation between Tools and Parts is ManyToMany. and the relation between parts and part_details is one to many.
with Laravel model, how can I get tool with part that has the biggest part_details.total ??
//tool model
public function parts()
{
return $this->belongsToMany('App\Part', 'tool_part');
}
//part model
public function tools()
{
return $this->belongsToMany('App\Tool', 'tool_part')
}
public function details(){
return $this->hasMany('App\Part_detail');
}
//partDetail model
public function part(){
return $this->belongsTo('App\Part');
}
Controller
public function index()
{
$tools = Tool::with('parts', 'parts.details')->has('parts')->get();
return $tools;
}
what I expected is something like :
Controller
public function index()
{
$tool = Tool::with('SinglePartThatHasHigestTotalInPartDetail');
}
Any Idea ??
You can use Laravel aggregates for querying to get the desired result,
In your code use max() function,
public function index()
{
$tool = Tool::with(['parts.part_details' => function ($query) {
$max = $query->max('total');
$query->where('total',$max);
})->first();
}
I haven't tested this but you can do like this.
Comment if you will get any errors.
I Manage my problem with "hacky" ways. if someone have a better and more elegant solution, please tell me.
//tool model
public function partWithHighestTotalDelivery($trans_date = null){
if (is_null($trans_date)) {
$trans_date = date('Y-m-d');
}
$parts = $this->parts;
$highest_total_delivery = 0;
foreach ($parts as $key => $part) {
$part->detail;
$total_delivery = $part->first_value;
if (isset( $part->detail->total_delivery )) {
$total_delivery += $part->detail->total_delivery;
}
if ($highest_total_delivery < $total_delivery ) {
$highest_total_delivery = $total_delivery;
$part->total_delivery = $highest_total_delivery;
$result = $part;
}
}
if (!isset($result)) {
$result = null;
}
$this->part = $result;
}
In controller i have :
public function index(Request $request){
$tools = Tool::has('parts')
->get();
$tools->each(function($tool){
$tool->partWithHighestTotalDelivery();
});
return $tools;
}
with this, I need to run tool->partWithHighestTotalDelivery() tools.count times. which is take noticeable process if the tools is many.
and also, the code I post and the question I ask has a slightly difference.that's for a simplicity sake's
Use the the hasManyThrough Relationship to get the all part details related to tool and then you can check the one by one record and get the highest total of the tool part.
// Tool Model
public function partsdetails()
{
return $this->hasManyThrough('App\PartDetail', 'App\Part','tool_id','part_id');
}
In Your controller
$data = Tool::all();
$array = [];
if(isset($data) && !empty($data)) {
foreach ($data as $key => $value) {
$array[$value->id] = Tool::find($value->id)->partsdetails()->max('total');
}
}
if(is_array($array) && !empty($array)) {
$maxs = array_keys($array, max($array));
print_r($maxs);// This array return the max total of the tool related parts
}
else{
echo "No Data Available";
}
You can start with the part detail and get the tool(s) from there:
$maxPartDetail = Part_detail::orderByDesc('total')->first();
$tools = $maxPartDetail->part->tools;

unset session on direct access to a function in codeigniter

I have 2 functions in my controller that I'm sending a session using the first one to the second function. What I am talking about is:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result/');
}
and the second function:
function search_result()
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
Everything is fine but, the problem is that, I want to prevent direct access to search_result() function. I mean, I want to unset search_term session when the user calls search_result() directly. What should I do?!
You can use flashdata: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages
And this is the code that do what are you looking for:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_flashdata('search_term', $search_term);
redirect('search_result/');
}
function search_result()
{
$search_term = $this->session->flashdata('search_term') ? $this->session->flashdata('search_term') : '';
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
There are many ways you can do it. Here are some
append a get parameter if it exists then search else redirect back
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result?status=1');
}
public function search_result()
{
$status = $this->input->get('status');
if(status == 1){
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}else{
$this->session->unset_userdata('search_term');
redirect('search');
}
}
Protect you seach_result function and dont let user direct call it.
Using _ will do it for you.
Here is the link you can read.
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
$this->_search_result($search_term);
}
function _search_result($keyword)
{
if(strlen($keyword)>0){
$data['search_term'] = $keyword;
$this->load->view('includes/template', $data);
}else{
redirect('search');
}
}
in the search function
redirect('search_result/'.$this->session->userdata('search_term'));
in the search_result function
function search_result()
{
if($this->uri->segment(3))
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
else
{
redirect('search_result/','refresh');
}
}
hope it will help you.please let me know if you face any problem.

Fetching “id” from a view and pass to another view in Grocery Crud CodeIgniter

I am new in Grocery Crud with Code Igniter and need a help. I have table vaboteni (emploees) and it work well. But I get stuck with code add more action. When I click to add action button I got error 404 Page Not Found. I want to fetch "id" from one row in table and pass to another view in order to display data for only one employee. I have site in local server, address localhost/bis_resursi/index.php/vraboteni/vraboteni_managment
Here is my Controller vraboteni.php
function vraboteni_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('vraboteni');
$crud->set_subject('вработен');
.....
$crud->add_action('Преглед', '', 'vraboteni/vraboten_managment/pregled','ui-icon-plus');
function pregled($id)
{
$this->load->model("vraboteni_pregled_model");
$data["result"] = $this->getVraboteniPregled($vrabotenID);
$this->load->view("pregled", $data);
}
$output = $crud->render();
$this->_example_output($output);
}
and Models: vraboteni_pregled_model.php
<?php
class Vraboteni_Pregled_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function getVraboteniPregled($id){
$query = $this->db->query("SELECT * FROM vraboteni WHERE vraboteID = '$id' ");
return $query->result();
}
and in view vraboten_view.php I put
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>
I managed to find solution. Right code is:
Controller vraboteni.php
$crud->add_action('Преглед', '', 'vraboteni/get','ui-icon-plus');
$output = $crud->render();
$this->_example_output($output);
}
function vraboteni()
{
$crud = new grocery_crud();
$crud->set_table('vraboteni');
$output = $crud->render();
print_r($output);
}
function getall()
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_getall();
$this->load->view('vraboten_view',$data);
}
function get($vrabotenID)
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_get($vrabotenID);
$this->load->view('vraboten_view',$data);
}
Models vraboten_model.php
<?php
class Vraboten_model extends CI_Model{
function vraboten_model(){
parent::__Construct();
}
function vraboten_getall(){
$this->load->database();
$query=$this->db->get(' vraboteni');
return $query->result();
}
function vraboten_get($vrabotenID){
$this->load->database();
$query=$this->db->get_where(' vraboteni',array('vrabotenID'=>$vrabotenID));
return $query->row_array();
}
}
and view vraboten_view.php
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>

Resources