public function search_inv(Request $request)
{
if($request->ajax()) {
$data = inventory::where('p_name', 'LIKE', $request->country.'%')->get();
$output = '';
if (count($data)>0) {
$output = '<ul class="list-group" style="display: block; position: relative; z-index: 1">';
foreach ($data as $row) {
$output .= '<table class="table table-bordered table-hover" id="abc" style="width:100%; float:right;">
<tr style="width:100%;">
<th style="width:17.5%; text-align:center;">Piece Name</th>
<th style="width:15%; text-align:center;">Piece Model</th>
<th style="width:14%; text-align:center;">Piece Quantity</th>
<th style="width:13%; text-align:center;">Piece Price</th>
</tr>';
$output.="<tr><td style=text-align=center>$row->p_name</td><td style=text-align=center>$row->p_model</td><td style=text-align=center>$row->p_Pieces</td><td style=text-align=center>$row->p_amount</td></tr>";
}
$output .= '</table>';
} else {
$output .= '<li class="list-group-item">'.'No results'.'</li>';
}
return $output;
}
}
I don't know what is the issue it is searching perfect but when I erase the search it duplicates the table header.
That's because your headers are within your foreach loop so every time it iterates, it's appending the headers to the output string (also your opening table element).
This should fix your issue:
if($request->ajax()) {
$data = inventory::where('p_name', 'LIKE', $request->country.'%')->get();
$output = '<table class="table table-bordered table-hover" id="abc" style="width:100%; float:right;">';
$output .= '<tr style="width:100%;">
<th style="width:17.5%; text-align:center;">Piece Name</th>
<th style="width:15%; text-align:center;">Piece Model</th>
<th style="width:14%; text-align:center;">Piece Quantity</th>
<th style="width:13%; text-align:center;">Piece Price</th>
</tr>';
if (! $data) {
$output .= '<li class="list-group-item">No results</li>';
}else{
$data->each( function( $row ) use ( &$output ){
$output .= '<tr>
<td style="text-align:center">' . $row->p_name . '</td>
<td style="text-align:center">' . $row->p_model . '</td>
<td style="text-align:center">' . $row->p_Pieces . '</td>
<td style="text-align:center">' . $row->p_amount . '</td>
</tr>';
});
}
$output .= '</table>';
return $output;
}
Related
In laravel, I am using search bar in list view, It will search from the record using ajax and display me the output.
But it's now working, when i write any text in search bar I didn't get any output. All records displays as it is.
My database in mongoDb, and i am coding in laravel.
Here is my view file code.
view
<div class="table-responsive m-t-40">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control" placeholder="Search Department" />
</div>
<table class="table table-bordered table-striped ">
<thead>
<tr>
<th>Department Name</th>
<th>Created By</th>
<th>Created On</th>
#if (App\User::isPermitted(['edit_department', 'update_department', 'delete_department']))
<th>Action</th>
#endif
</tr>
</thead>
<tbody>
#if($listOfDepartment != null)
#foreach($listOfDepartment as $departmentList)
<tr>
<td>{{$departmentList->nameOfDepartment}}</td>
<td>{{$departmentList->createdBy}}</td>
<td>{{$departmentList->created_at}}</td>
#if (App\User::isPermitted(['edit_department', 'update_department', 'delete_department']))
<td>
<i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i>
<i class="fa fa-trash fa-lg" onclick="delete_user(this); return false;" style="color:red" aria-hidden="true"></i>
</td>
#endif
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
script
<script>
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('list_department') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query);
});
});
</script>
and here is my code of controller file
namespace App\Http\Controllers;
use App\Department;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
public function listDepartment(Request $request)
{
$listOfDepartment = Department::all();
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data = Schema::table('department')
->where('nameOfDepartment', 'like', '%'.$query.'%')
->orWhere('createdBy', 'like', '%'.$query.'%')
->get();
}
else
{
$data = Schema::table('department')
->orderBy('nameOfDepartment', 'asc')
->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '<tr>
<td>'.$row->nameOfDepartment.'</td>
<td>'.$row->createdBy.'</td>
</tr>';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($data);
}
return view('pages.department', compact('listOfDepartment'));
}
I am trying to search users in a panel with ajax. But along with that, I also want to edit the selected user.
I have created the button inside the controller to achieve this but the button isn't working.
Here is my controller code:
public function searchTeacher(Request $request){
if($request->ajax()){
$output = '';
$query = $request->get('query');
if($query != '')
{
$data = DB::table('users')->where('name', $query)->get();
}else{
$data = DB::table('users')->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->teacherId.'</td>
<td>'.$row->subject.'</td>
<td> <button href={{route(update-teacher-view), '.$row->id.'}}> Edit</button> </td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($data);
}
}
Here is the ajax for search method-
<script>
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
console.log(query)
$.ajax({
url:"{{ route('search-teacher') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query);
});
});
Here is my html-
<section class="panel">
<table class="table table-striped">
<thead class="team-list chat-list-side info border-less-list">
<th>Teacher Name</th>
<th>Email</th>
<th>Teacher Id</th>
<th>Subject</th>
</thead>
<tbody>
</tbody>
</table>
</section>
A solution will be appreciated.
Thanks in advance.
Maybe it's a wrong string interpolation? Should be like this:
<?php
$output .= '
<tr>
<td>'.$row->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->teacherId.'</td>
<td>'.$row->subject.'</td>
<td> <button href='.route('update-teacher-view', $row->id).'> Edit</button> </td>
</tr>
';
Also instead of echo-ing, just return the value, it will be automatically converted to JSON response:
// echo json_encode($data); // Change this line to below
return $data;
Render the blade file with the data this is the best solution.
In controller
return View::make('ajaxFilter',compact('data'))->render();
On ajaxFilter.blade.php
<section class="panel">
<table class="table table-striped">
<thead class="team-list chat-list-side info border-less-list">
<th>Teacher Name</th>
<th>Email</th>
<th>Teacher Id</th>
<th>Subject</th>
</thead>
<tbody>
#if($data->count() > 0)
#foreach($data as $row)
<tr>
<td>{{ $row->name }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->teacherId }}</td>
<td>{{ $row->subject }}</td>
<td>
Edit //button design
</td>
</tr>
#endforeach
#else
No data Found
#endif
</tbody>
</table>
</section>
On ajax response, you can append HTML data something like that
$('#div_or_section_id').html(data);
Below is how the ci controller looks like.
I'm just following tutorial from Youtube Channel.
How do you add pagination so the page only load 20 results per page ?
<?php
class Product_details extends CI_Controller{
function index(){
$this->load->library('pagination');
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products();
$this->load->view('product_listing',$data);
}
}
View
<table>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Image</th>
</tr>
<?php
foreach ($userArray as $key => $value) {
echo "<tr>
<td>".$value['id']."</td>
<td>".$value['post_id']."</td>
<td>".$value['price']."</td>
<td><img src=".$value['imageUrl']."/></td>
</tr>";
}
?>
</table>
Thank you
you can pass page number in the model.
Controller
<?php
class Product_details extends CI_Controller{
function index($pageNo){
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products($pageNo);
$this->load->view('product_listing',$data);
}
}
Model
public function all($pageNo){
$pageNo -= 1;
$this->db->select("*")
->from('products')
->order_by('id',"ASC")
->limit(20, $pageNo * 20);
$query = $this->db->get();
return $query->result_array();
See the example how to add pagination.
Let's My controller is Dashboard.php and it's method is index.
Now configure pagination.
public function index()
{
$config['base_url'] = site_url('/dashboard/index/');
$config['total_rows'] = $this->dashboard_model->num_rows();
$config['per_page'] = 5;
$config['use_page_numbers'] = FALSE;
$this->pagination->initialize($config);
$data['data'] = $this->dashboard_model->all_blog($config['per_page'], $this->uri->segment(3));
$this->load->view("/dashboard/blog", $data);
}
Model is Dashboard_model.php
public function all_blog($limit, $offset)
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$q = $this->db->get();
$data = $q->result_array();
return $data;
}
public function num_rows()
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$this->db->limit($limit, $offset);
$q = $this->db->get();
$data = $q->num_rows();
return $data;
}
Now it's my view blog.php
<div class="table-responsive">
<table class="footable table table-stripped toggle-arrow-tiny" data-page-size="8" data-filter=#filter; id="filter">
<thead>
<tr>
<th></th>
<th>S.No</th>
<th>Title </th>
<th>Blog Image</th>
<th>Added For</th>
</tr>
</thead>
<tbody>
<?php
$count = $this->uri->segment(3, 0);
foreach ($data as $key) :
?>
<tr>
<td><input type="checkbox" class="i-checks" name="input[]"></td>
<td><?php echo ++$count; ?></td>
<td><?php echo $key['blog_title']; ?></td>
<td><img src="<?php echo $key['blog_image']; ?>" style="width: 150px; height: 80px" /></td>
<td><?php echo $key['user_type']; ?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
</div>
<?php
echo $this->pagination->create_links();
?>
I want to update the quantity of a shopping cart. I've google it a lot but couldn't do it.
Maximum pages are showing which i don't want. Exactly solution i'm not getting. Hopefully i will get here. Please help me to do it. Here is my view page.
**<table class="table table-hover table-bordered table-striped snipcart-details "> <thead>
<tr>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Delete Cart</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Product Name</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Image</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Price</th>
<th style="color:#FF0033; font-weight:bolder; text-align:center;" >Quantity</th>
<th colspan="2" style="color:#FF0033; font-weight:bolder; text-align:center;" >Total</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->cart->contents() as $items) { ?>
<tr>
<td style="color:#000000; font-weight:bolder; text-align:center;" >
<a href="#" class="remove_cart" title="delete" row_id="<?php echo $items['rowid']; ?>" rel="1">
<i class="fa fa-times fa-2x" style="color:red;" aria-hidden="true"></i> </a></td>
<td style="color:#000000; font-weight:bolder; text-align:center;" ><?php echo $items['name']; ?></td>
<td align="center"><img src="<?php echo base_url('resource/allproduct/'.$items['productImage']);?>" height="50px;" /></td></td>
<td style="color:#000000; font-weight:bolder; text-align:center;"><?php echo $items['price']; ?></td>
<td align="center"><input type="number" name="qty" id="qty" value="<?php echo $items['qty']; ?>"></td>
<td colspan="2" style="color:#000000; font-weight:bolder; text-align:center;">TK <?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php } ?>
</tbody>
<tbody>
<tr>
<th scope="row"> <input type="button" name="submit" value="Update" class="button" /> </th>
<td align="center"> <input type="button" name="submit" value="Continue Shopping" class="button" /></td>
<td colspan="3" class="button" align="center" >
<?php if(!empty($userid)){?>
<input class="button" type="submit" value="Place Order">
<?php } else {?>
<input class="button" type="submit" value="Place Order">
<?php }?>
</td>
<td align="center" style="color:#000000;" > <h4>Grand Total</h4> </td>
<td style="font-size:24px; font-weight:800; color:green;">TK <?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</tbody>
</table>**
And here is controler.
public function index()
{
$data['basicinfo'] = $this->M_cloud->basicall('basic_info');
$where = array('status' => 1);
$data['categoryinfo'] = $this->M_cloud->categoryinfo('item_manage', $where);
$data['rows'] = count($this->cart->contents());
$data['userid'] = $this->session->userdata('user_id');
$data['subcategoryinfo'] = $this->M_cloud->findAll2('sub_category', array('status' => 1));
$data['menuinformation'] = $this->M_cloud->findReport('our_service', array('serviceType'=> 2), 'menuname asc');
$data['menuservice'] = $this->M_cloud->findReport('our_service', array('serviceType'=> 1), 'menuname asc');
$data['socialmedia'] = $this->M_cloud->findAll('social_tbl', 'name asc');
$data['newsinfo'] = $this->M_cloud->findAll('news_table', 'newstitle DESC');
$this->load->view('cartPage', $data);
}
public function buy()
{
$proId = $this->input->post('proId');
$Qty = $this->input->post('Qty');
$prosize = $this->input->post('prosize');
$result = $this->M_cloud->find('product_manage', array('proid' => $proId));
$data2 = array(
'id' => $proId,
'qty' => $Qty,
'name' => $result->proName,
'price' => $result->price,
'prosize' => $prosize,
'productImage' => $result->proimg1,
'product_code' => $result->procode
);
$this->cart->insert($data2);
redirect('cart');
}
public function deleteCartItem() {
$row_id = $this->input->post('row_id');
$data = array(
'rowid' => $row_id,
'qty' => 0
);
$this->cart->update($data);
}
Please help me how to update cart quantity. Thanks in Advance.
To update cart Item :
function updateCartItem(){
$data=array(
'rowid'=>$this->input->post('rowid',TRUE),
'qty'=> $this->input->post('quantity',TRUE)
);
if ($this->cart->update($data)) {
echo "Success";
}else{
echo "Faliure";
}
}
To remove the item from cart :
function deleteCartItem(){
$row_id = $this->input->post('row_id',TRUE);
if ($rowid) {
return $this->cart->remove($rowid);
}
}
I have data in the view like so
<div class="box-body table-responsive no-padding">
<div id="users-table-wrapper">
<table id="users_table" class="table table-hover table-striped">
<tbody>
<tr>
<th>#lang('app.soldto')</th>
<th>#lang('app.soldby')</th>
<th>#lang('app.network')</th>
<th>#lang('app.plan')</th>
<th>#lang('app.sales_date')</th>
</tr>
#if (count($sales))
#foreach ($sales as $sale)
<tr>
<td>{{ $sale->soldto ?: trans('app.n_a') }}</td>
<td>{{ $sale->first_name . ' ' . $sale->last_name }}</td>
<td>{{ $sale->name }}</td>
<td>{{ $sale->plan }}</td>
<td>{{ $sale->salesdate }}</td>
</tr>
#endforeach
#else
<tr>
<td colspan="6"><em>#lang('app.no_records_found')</em></td>
</tr>
#endif
</tbody>
</table>
{!! $sales->render() !!}
</div>
</div>
</div>
I use pagination to display 10 rows per page. Like below
I want to download all this data to CSV. Without running the db query again.
Datatable itself provide the buttons to export csv, xls ,pdf etc. Here is the code
$('#example').DataTable( {
dom: 'Bfrtlp',
buttons: ['csv','pdf']
} );
'#example' is the id of your table
You can use working code below:
public static function exportCsv()
{
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$rows = DB::select( DB::raw("select * from users
;") );
$rows = json_decode(json_encode((array) $rows), true);
//\Log::info('query'.print_r($rows,true));
ob_end_clean();
$out = fopen('php://output', 'w');
foreach($rows as $key=>$values)
{
$arr = [];
foreach ($values as $key=>$value) {
array_push($arr, $key);
}
$line = $arr;
fputcsv($out, $line);
break;
}
//fputcsv($out, array('Registration', 'Name', 'Checkin Time'));
foreach($rows as $lines)
{
$line = $lines;
fputcsv($out, $line);
}
die;
fclose($out);
}