Unable to search the Data into Datatable using Ajax - Codeigniter - php

Hi Everyone I am creating a web portal. I want to add display a datatable. I use Datatable plugin for this but I am facing some issue. please help me with this.
This is my Contorller code
public function fetchTeams() {
$data = $this->Teams_model->getAllTeams();
echo json_encode(array("data" => $data));
}
My HTML view
<div class="card-body">
<table id="teamDatatable" class="display table table-bordered table-hover" width="100%">
<thead>
<tr>
<th>Employee name</th>
<th>Email</th>
<th>Gender</th>
<th>Salary</th>
<th>City</th>
</tr>
</thead>
</table>
</div>
My Model
public function getAllTeams() {
$query = $this->db->get("teams");
return $query->result_array();
$query1 = $this->db->get('Teams');
$data1 = $query->num_rows();
$response = array(
"iTotalRecords" => $data1,
"aaData" => $data
);
return $response;
}
My view
My View
No record

Hope this helps Check my server side codeigniter script here
Server side datatable codeigniter

Related

how to fetch data from single table and show as multiple datatables using codeignator?

I'm fetching the data from the database. Want to show the data into multiple dataTables separated by column name (certification_scope).
This is the labels table in the database
Here is my Controller Clients.php
public function labels_data(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$query = $this->db->select('*')->where('clientid',get_client_user_id())->order_by('labelid','desc')->get(db_prefix().'labels');
$data = [];
foreach($query->result() as $r) {
$data[] = array(
$r->certification_scope,
$r->test_evidence,
$r->c_scope_desc,
$r->total_quantity,
$r->current_balance,
$r->project_name,
$r->serial_no_from,
$r->serial_no_to,
$r->used_quantity
);
}
$result = array(
"draw" => $draw,
"recordsTotal" => $query->num_rows(),
"recordsFiltered" => $query->num_rows(),
"data" => $data
);
echo json_encode($result);
exit();
}
This is the view labels.php
<div class="table-wrap">
<table id="item-list" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Certification Scope</th>
<th>Test Evidence</th>
<th>Scope Description</th>
<th>Total Quantity</th>
<th>Current Balance</th>
<th>Project Name</th>
<th>Serial No From</th>
<th>Serial No To</th>
<th>Used Quanity</th>
</tr>
</thead>
<tbody> </tbody>
</table>
</div>
This is the dataTable Ajax
$('#item-list').DataTable({
"ajax": {
url : "<?php echo base_url('clients/labels_data'); ?>",
type : 'GET'
}
});
Result for the above code is
Want to show the DataTables like this
Anyone can please help me to fix this.

Search results shows everything in database and show image in results

I have a search function in my laravel project, when I navigate to the search page it shows all the entries in the database. I would like it to show nothing until I put something in the search field. Furthermore, when the results are displayed I would like an image to be dispalyed.
Here are the relavent files:
public function search(Request $request){
$query = $request->input('query');
$result = Photo::search($query)->get();
return view('search.results')->with('result', $result);
}
Here is the model
class Photo extends Model
{
use SearchableTrait;
protected $searchable = [
'columns' => [
'photos.title' => 10,
'photos.description' => 10,
'photos.photo' => 10,
],
];
protected $fillable = array('photo','title','description','album_id');
public function album(){
return $this->belongsTo('App\Album');
}
}
The display.blade.php
<div class="row">
<div class="col-8">
<table class="table table-bordered table-stripped">
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($result as $res)
<tr>
<th><img src="/storage/photos/."{{$album_id}}/{{$res->photo}}</a></th>
<th>{{$res->title}}</th>
<th>{{$res->description}}</th>
</tr>
#endforeach
</tbody>
</table>
In the above code I get an error message about not being able to find $album_id.
As always thank you for your help

Laravel - Get last 5 registered users attributes

I'm trying to make a dashboard and I want to display the last 5 users that are registered on the site, So the most recent 5 registered people. How can I accomplish this? I need to have access to all their attributes, for instance, Name, Lastname etc.
This is my route: Route::get('/adminpanel', 'AdminController#index')->name('adminpanel');
And this is the controller:
public function index()
{
Carbon::setLocale('nl');
$tijd = Carbon::today();
$posts = Post::count();
$users = User::count();
$replies = Reply::count();
return view('admin.dashboard', compact('posts', 'users', 'replies', 'tijd'));
}
They need to be displayed in a table like this:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Rainier</td>
<td>Laan</td>
<td>rainier.laan#home.nl</td>
</tr>
</tbody>
</table>
Thanks in advance
You can use take() to get specific number of records.
$users = User::orderBy('id', 'desc')->take(5)->get();
And on your view-
#foreach($users as $user)
{{$user->name}}
#endforeach
Hope it helps :)
This one will take 5 latest users.
$users = User::latest()->take(5)->get();

How to retrieve data from database using Ajax and OOP php

I am going to develop web page with ajax and php. but there is problem in my get_data_in_table function. it will show data in a table structure. but it shows error when i run this code. others are working properly. it shows error in while loop. but i am unable to find any issue.
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
?>
I guess I found it:
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
} // This was missing
?>

Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag (download report using dompdf )

I'm trying to download my html report into pdf file using dompdf, but I'm getting this error:
Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag::$first_name
here's my controller code so far:
public function monthlypayrollProcess(Request $request)
{
$monthlypayrolls = Driver::join('driver_payables', 'driver_payables.driver_id', '=', 'drivers.id')
->join('driver_payable_details', 'driver_payable_details.driver_payable_id', '=', 'driver_payables.id')
->select(
'drivers.id',
'drivers.first_name',
'drivers.last_name',
'drivers.nric',
'drivers.join_date',
'drivers.basic_salary',
'drivers.uniform_size',
'driver_payables.total_working_hours',
'driver_payables.take_home_pay'
)
->where('driver_payables.payroll_period_id', $request->payroll_period_id)->get();
if ($monthlypayrolls->isEmpty()) {
return $this->sendErrorResponse($request, 'No Data');
}
$view = view('reporting.monthly_payroll.detail')->with(compact('monthlypayrolls'));
return parent::render($view, null, null);
}
public function downloadMonthlyPayrollReport(Request $request)
{
$monthlypayrolls = $this->monthlypayrollProcess($request);
$isPdf = true;
$view = 'reporting.monthly_payroll.pdfview';
$pdf = PDF::loadView($view, compact('monthlypayrolls', 'isPdf'))->setPaper('A4', 'landscape');
return $pdf->stream();
}
and here's my pdf view blade code so far :
<div class="table-responsive text-center">
<table class="table table-hover" id="datatable" style="width: 100%">
<thead>
<tr>
<th class="text-center">Driver Name</th>
<th class="text-center">NRIC</th>
<th class="text-center">Employment Type</th>
<th class="text-center">Hired Date</th>
<th class="text-center">Basic Salary</th>
<th class="text-center">Uniform</th>
<th class="text-center">Total Working Hours</th>
<th class="text-center">Take Home Pay</th>
</tr>
</thead>
<tbody>
#foreach($monthlypayrolls as $monthlypayroll )
<tr>
<td>{{$monthlypayroll->first_name }} {{ $monthlypayroll->last_name}}</td>
<td>{{$monthlypayroll->nric}}</td>
<td>{!! DriverEmployedType::getString($monthlypayroll->employed_type) !!}</td>
<td>{{$monthlypayroll->join_date}}</td>
<td>{{$monthlypayroll->basic_salary }}</td>
<td>{{$monthlypayroll->uniform_size}} {{$monthlypayroll->uniform_quantity}}</td>
<td>{{$monthlypayroll->total_working_hours}}</td>
<td>{{$monthlypayroll->take_home_pay}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
any idea ?
The monthlypayrollProcess() method returns a Response view, then you are assigning it to another variable
$monthlypayrolls = $this->monthlypayrollProcess($request);
Finally in your view
<td>{{$monthlypayroll->first_name }} {{ $monthlypayroll->last_name}}</td>
Something wrong with your logic, the method probably should return an object not a view. The method should be something like that:
public function monthlypayrollProcess(Request $request)
{
$monthlypayrolls = Driver::join('driver_payables', 'driver_payables.driver_id', '=', 'drivers.id')
->join('driver_payable_details', 'driver_payable_details.driver_payable_id', '=', 'driver_payables.id')
->select(
'drivers.id',
'drivers.first_name',
'drivers.last_name',
'drivers.nric',
'drivers.join_date',
'drivers.basic_salary',
'drivers.uniform_size',
'driver_payables.total_working_hours',
'driver_payables.take_home_pay'
)
->where('driver_payables.payroll_period_id', $request->payroll_period_id)->get();
if ($monthlypayrolls->isEmpty()) {
return $this->sendErrorResponse($request, 'No Data');
}
return $monthlypayrolls;
}
I had this very issue when trying to retrieve data as a response->json($myObject) and then iterating through it with a foreach with blade in the view.
I solved it by just returning the object "as is" (i.e. return $myObject instead of return response->json($myObject) ).
Hope this helps!

Categories