I created a management system in codeigniter that loads data from the database in datatables for specific suppliers. When the user clicks at any of these data rows he is navigating to each supplier's company products. My problem is that I want to find a session or something else that will keep the data of the previous selected rows when the page reloads. At the moment I am using flashdata but when I go back a step or return to the previous page all the data from the database disappear. What I want is something that will keep my data even if I am reloading the page or if I go back a step.
That's my controller:
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('dash_match_model');
$this->session->keep_flashdata('supplier_id');
$this->session->keep_flashdata('segment');
$this->session->keep_flashdata('class');
$this->session->keep_flashdata('commodity');
$this->load->library('session');
$this->load->helper('url');
}
public function index() {
$arr['page']='dash1';
$user_id = $this->session->userdata('id');
$supplier = $this->dash_match_model->dash_present_all_suppliers($user_id);
$arr['dash_present_all_suppliers'] = $supplier;
$this->load->view('clients/clDashboard',$arr);
}
public function select_supplier()
{
$supplier_name = $this->input->get('name', TRUE);
$supplier_sel = $this->dash_match_model->selected_supplier_id($supplier_name);
foreach ($supplier_sel->result() as $row){
$this->session->set_flashdata('supplier_id', $row->supplier_id);
}
$selected_supplier = $this->dash_match_model->unspsc_matched_skus($this->session->flashdata('supplier_id'));
$arr['dash_present_all_selected_suppliers'] = $selected_supplier;
$this->load->view('clients/unspscSegment', $arr);
}
public function select_segment(){
$segment = $this->input->get('segment', TRUE);
$supplier_id = $this->session->flashdata('supplier_id');
$this->session->set_flashdata('segment', $segment);
$segment_sel =$this->session->flashdata('segment');
$selected_segment = $this->dash_match_model->unspsc_class($supplier_id, $segment_sel);
$arr['dash_present_all_selected_segments'] = $selected_segment;
$this->load->view('clients/unspscClass', $arr);
}
public function select_class(){
$class = $this->input->get('class', TRUE);
$supplier_id = $this->session->flashdata('supplier_id');
$segment_sel =$this->session->flashdata('segment');
$this->session->set_flashdata('class', $class);
$class_sel = $this->session->flashdata('class');
$selected_class =$this->dash_match_model->unspsc_commodity($supplier_id, $segment_sel, $class_sel);
$arr['dash_present_all_selected_class'] = $selected_class;
$this->load->view('clients/unsspscCommodity', $arr);
That's one of my first table view file with suppliers:
<div class="row-fluid sortable">
<div class="box span12">
<div class="box-header" data-original-title>
<div class="box-icon">
<i class="halflings-icon wrench"></i>
<i class="halflings-icon chevron-up"></i>
<i class="halflings-icon remove"></i>
</div>
</div>
<div class="box-content">
<table id="suppliertable" class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>Supplier</th>
<th>Open Range</th>
<th>Fill Content</th>
<th>Total Match</th>
</tr>
</thead>
<tbody>
<?php foreach($dash_present_all_suppliers as $v): ?>
<tr>
<td class="center" style="color:#0c595b;"><?php echo $v['supplierCompany']?> </td>
<td class="center">70%</td>
<td class="center">12%</td>
<td class="center">82%</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div><!--/span-->
</div>
Instead of using $this->session->flashdata() user the normal session i.e. $this->session->userdata() to store the temp browse data and write a cron which will delete this session in a regular interval or at the time of logout or any desired specific action.
Hope this will give better approach n help you in solving your problem
Related
My team and I are making a project wherein we need to pass on variables to a modal. How do I pass the data to the modal using loan id as parameter from the table row that I selected?
example:
loan id
name
view
0964
charles
button
0562
ultor
button
when you press the button of that particular row, a modal will show up wherein the details of that row will be loaded.
In addition, the modal will also have information that would be needed, but is from a different database table, but the parameter is name instead of loan id
Is there a way to do that? I don't have a lot of experience in jquery and ajax, but i read that it might be a solution to my problem.
here is my table view that shows the button and the row information.
<?php foreach($open_applications as $row): ?>
<tr>
<td><?php echo $row->col_full_name; ?></strong><td>
<td><?php echo $row->col_loan_assignid; ?></td>
<td>
<button class="btn btn-secondary btn-xs" data-bs-toggle="modal" data-bs-target="#loanDecision">Pending</button>
<a href="<?php echo site_url('Users/borrowerdetails')?>" role="button" class="btn btn-link btn-xs">View</button>
</td>
</tr>
Here is my modal where it would need to show information that are in the row and information from a different table
<div class="modal fade" id="loanDecision" tabindex="-1" aria-labelledby="loanDecisionLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header border-0">
<h5 class="modal-title" id="loanDecisionLabel">This would be where the name is from the row</h5><br>
</div>
<div class="modal-header">
<h5 class="modal-title" id="loanDecisionSubtitle">this would be where the person's job is from the row</h5>
</div>
<div class="modal-body">
<div class="table-responsive">
<table class="table table-hover table-bordered">
<thead class="thead">
<tr>
<th scope="col">title of the column from a different table</th>
<th scope="col">title of the column from a different table</th>
<th scope="col">title of the column from a different table</th>
</tr>
</thead>
<tbody>
<tr>
<td>data from the column from the different table</td>
<td>data from the column from the different table</td>
<td>data from the column from the different table</td>
<td>data from the column from the different table</td>
<td>data from the column from the different table</td>
<td>data from the column from the different table</td>
<td>data from the column from the different table</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
I have created a controller and a model, but I don't know how to connect them to the modal, but here is my controller and model:
public function fetchDetails($col_loan_assignid, $col_borrower_id)
{
$data['getloandetails'] = $this->Users_model->getingloandetails($col_loan_assignid);
$data['getactiondetails'] = $this->Users_model->getingactiondetails($col_borrower_id);
}
public function getingloandetails($col_loan_assignid){
$sql = "select * from $this->tbl_loan_application where col_loan_assignid like '%$col_loan_assignid%'";
$query = $this->db->query($sql);
return $query->result();
}
public function getactiondetails($col_borrower_id){
$sql = "select * from $this->tbl_action where col_loan_assignid like '%$col_borrower_id%'";
$query = $this->db->query($sql);
return $query->result();
}
Step 1 : set unique id to data attributes in view button.
Example data-loanid='".$row->loanid."'
Step 2 : add a common class to view button and add event-listner to it.
Example view-btn
Step 3 : after adding event listener get data attributes from its. So you can able to access unique.
$(".view-btn").click(function(){
let loan_id=$(this).data('loanid');
console.log(loan_id);
});
Step 4 : after getting unique id so can able send a request via ajax or fetch api.
Step 5: on ci3 create a route and validate data in controller and pass data to model and fetch data from database and return response.
Fetch api Example
let ApiRequest = async function(Url,param) {
try {
const url = Url;
const options = {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: param,
//body:field1=Value1&field2=value2
}
const response = await fetch(url, options);
// response as json
return await response.json();
} catch (error) {
console.error('Error:', error);
return false;
}
}
let url="http://www.example.com/api/getdata";
let parameter=field1=Value1&field2=value2;
let Result=ApiRequest(url,parameter);
console.log(Result);
I have two eloquent tables, vendor and work, in the work table there is a total score column and I want to get the average of the total score based on the vendor ID (foreign Key) and save it to the vendor table. May I know how to achieve this?
Here is my vendor table blade file
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Penilaian Penyedia</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpenyedia" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Nama Penyedia</th>
<th>Nilai</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
#php $no = 1; #endphp
#foreach ($penyedia as $penyedias)
<tr>
<td>{{$no++}}</td>
<td>{{$penyedias->nama}}</td>
<td></td>
<td>
Beri Penilaian
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
Here is the AdminController
public function update_nilaipekerjaan(Request $request, $id){
$pekerjaan = Pekerjaan::find($id);
//$pekerjaan->update($request->all());
//$total = Pekerjaan::select(DB::raw('avg(nilai_1 + nilai_2 + nilai_3 + nilai_4) as nilai_total'))->get();
$pekerjaan->nilai_1=$request->nilai_1;
$pekerjaan->nilai_2=$request->nilai_2;
$pekerjaan->nilai_3=$request->nilai_3;
$pekerjaan->nilai_4=$request->nilai_4;
$pekerjaan->nilai_total=$request->nilai_1+$request->nilai_2+$request->nilai_3+$request->nilai_4;
$pekerjaan->update();
return redirect()->back()->with('message','Nilai Pekerjaan Berhasil diupdate!');
}
public function tabelnilai_penyedia(){
$penyedia = Penyedia::all();
$pekerjaan = Pekerjaan::all();
return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan'));
} //I WANT TO SHOW THE AVERAGE OF THE TOTAL SCORE HERE.
Database https://imgur.com/a/N6NlcwJ
Here is what I want to achieve https://imgur.com/a/fk4Yq92
what I have tried
updating the AdminController
public function tabelnilai_penyedia($id){
$penyedia = Penyedia::all();
$pekerjaan = Pekerjaan::all();
$average = Pekerjaan::where('penyedia_id', $id)->avg('nilai_total');
Penyedia::where('id', $id)->update(['nilai'=>$average]);
return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan'));
}
But it got me an error,
Too few arguments to function App\Http\Controllers\AdminController::tabelnilai_penyedia(), 0 passed in C:\Users\ASUS TUF\webpenyedia\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
You can use the MySQL AVG function to get the average of a column, which in Eloquent is neatly wrapped in an aggregate function
Assuming your models are called "Work" and "Vendor"
// Get average of 'nilai' based on Vendor ID
$average = Work::where('penyedia_id', $vendorId) // Assuming this is the right column
->avg('nilai_total');
// Update Vendor directly in database (No model events)
Vendor::where('id', $vendorId)->update(['nilai' => $average]);
I am trying to get confirmation message before deleting the data, and also I would like to maintain my data in database while deleting them in frontend .
here is my (project) index.php view
<div class="main-sec-contant">
<div class="ProjectsDetails">
<h2 class="heading">Projects Details</h2>
<div class="row">
<div class="col-md-12">
<table id="table_id" class="display">
<thead>
<tr>
<th>Project Name</th>
<th>Client Name</th>
<th>Company</th>
<th>Project Manager</th>
<th>Support Staff</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($project as $n)
{
?>
<tr>
<td><?php echo $n->project_name;?></td>
<td><?php echo $n->client_name;?></td>
<td><?php echo $n->company;?></td>
<td><a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>" data-toggle="tooltip" data-placement="bottom" title="<?php echo $n-> project_manager;?>" data-original-title="Click to deactivate the user"></a>   </td>
<td><a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>" data-toggle="tooltip" data-placement="bottom" title="<?php echo $n-> support_staff;?>" data-original-title="Click to deactivate the user"></a>  
<a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>"></a>  
<a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>"></a></td>
<td><span class="icoact"></span> Active</td>
<td><a class="edit" href="<?php echo site_url('admin/project/edit/'.$n->id);?>"><i class="fa fa-pencil-square-o" ></i></a>  <a class="delete" href="<?php echo site_url('admin/project/delete/'.$n->id);?>"><i class="fa fa-trash-o"></i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
Here is my Project.php controller
public function index ()
{
$data['company_name'] = $this->project_model->getAllCompanyName();
$data['project'] = $this->project_model->getProjectDetails();
$this->load->view('admin/project/index',$data);
}
function delete($id)
{
$this->project_model->delete($id);
$this->session->set_flashdata ('success','Project Deleted Sucessfully');
redirect('admin/project/index');
}
And here is my Project_model.php , delete function
function getProjectDetails()
{
//table (projects)
$delete_flag=0;
$data['project'] = $this->db->get_where('projects',array('delete_flag!='=>$delete_flag))->result_array();
}
function getById($id)
{
return $this->db->get_where('projects',array('id'=>$id))->row();
}
function delete($id)
{
$delete_flag=0;
$this->db->where('id',$id)->update('projects',array('delete_flag'=>$delete_flag));
}
Right now I am able to delete the data successfully from both frontend and database, I would appreciate if you can help me how I can get the confirmation message, and also maintain my data in database while deleting them from frontend ?
Here is the database table
Delete in Frontend only:
Add a boolean Column "delete_flag" to your project database table and update it to TRUE when delete is clicked in frontend.
Then change your load Method in Project model to only get Projects where delete_flag = 0
Delete Confirmation:
Search for Modal Dialog. You can easily set up a message and confirm button link with a modal Dialog. Maybe you are even using a Template which already provides it.
See an example here: https://www.w3schools.com/howto/howto_css_modals.asp
For Data Delete from Frontend only but not from database:-
You should add a Column name delete_flag int datatype to your project database table and update it to 0, while deleting the record.
Data Delete Confirmation Alert Message:-
<td><a class="delete" href="<?php echo site_url('admin/project/delete/'.$n->id);?>" onclick="return confirm('Are you sure want to Delete this Record?')"><i class="fa fa-trash-o"></i></a></td>
controller Code Project.php :-
function delete($id)
{
$this->project_model->delete($id);
$this->session->set_flashdata ('success','Project Deleted Sucessfully');
redirect('admin/project/index');
}
Model Code Project_model.php :-
function getProjectDetails()
{
$delete_flag=0;
$data['project'] = $this->db->get_where('projects',array('delete_flag!='=>$delete_flag))->result_array();
}
function delete($id)
{
$delete_flag=0;
$this->db->where('id',$id)->update('projects',array('delete_flag'=>$delete_flag));
}
In Your View:-
Show Only records which have delete_flag!=0 by this query in your getProjectDetails()
I have make a html view page for generate to a pdf report from this view and i have also installed dompdf library, i have loaded data from database table on this view and create a hyperlink name as Download pdf. but when i click on this link it's show undefined variable dsale error.
here my controller method.
//pass data to view
public function dsreport($id,$fromdate,$todate)
{
$dsale=DB::table('directsales')
->join('clients','directsales.client_id','=','clients.id')
->join('products','directsales.product_id','=','products.id')
->select('clients.client_name','clients.addr','directsales.*','products.name')
->where('directsales.client_id','=',$id)
->whereBetween('directsales.issue_date',[$fromdate,$todate])
->distinct()
->paginate(3);
//->get();
$sumt=DB::table('directsales')
->where('directsales.client_id','=',$id)
->sum('directsales.total');
return view('reports.directsalereport',compact(['dsale','sumt']));
}
//generate pdf
public function dsalepdf()
{
$pdf=PDF::loadView('reports.directsalereport');
return $pdf->download('dsread.pdf');
}
Here is my view part.
<div class="panel-body">
<center><h1><strong>M/s. Priti Enterprise</strong></h1></center>
</center>
<center><h2><i>Daily Sales Report</i></h2></center>
<div class="row">
<div class="col-md-4"><strong>Client Name: {{$dsale[0]->client_name}}</strong></div>
<div class="col-md-4"></div>
<div class="col-md-4"><strong>Delivered by: {{$dsale[0]->deliverd_by}}</strong></div>
</div><br>
<div class="row">
<div class="col-md-4"><strong>Client address: {{$dsale[0]->addr}}</strong></div>
<div class="col-md-4"></div>
<div class="col-md-4"><strong>Date: {{$dsale[0]->issue_date}}</strong></div>
</div><br><br>
<table class="table table-bordered">
<thead>
<th class="col-md-1">SL no.</th>
<th>Product Name</th>
<th>Invoice no.</th>
<th>Unit per ctn.</th>
<th>Unit price</th>
<th class="col-md-2">Sales</th>
<th>Value</th>
</thead>
<tbody>
#foreach($dsale as $d)
<tr>
<td>#</td>
<td>{{$d->name}}</td>
<td>{{$d->transaction_code}}</td>
<td>{{$d->unitperctn}}</td>
<td>{{$d->unitprice}}</td>
<td>{{$d->ctn}} ctn {{$d->pcs}} pcs</td>
<td>{{$d->total}}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="col-md-11"></div>
<div class="row"><strong>Total:{{$sumt}}</strong></div>
<div>{{$dsale->links()}}</div>
<div class="col-md-8"></div>
<div class="row">
<i class=" glyphicon glyphicon-ok"></i> Download PDF
</div>
</div>
You have an array in compact.
You should use <div>{{$dsale[0]->links()}}</div>.
I think you should dont compact an array.
return view('reports.directsalereport',compact('dsale','sumt'));
Edited:
You should pass data to your Pdf view, too, so your method for downloading pdf should look like this.
public function dsalepdf()
{
$dsale=DB::table('directsales')
->join('clients','directsales.client_id','=','clients.id')
->join('products','directsales.product_id','=','products.id')
->select('clients.client_name','clients.addr','directsales.*','products.name')
->where('directsales.client_id','=',$id)
->whereBetween('directsales.issue_date',[$fromdate,$todate])
->distinct()
->paginate(3);
$sumt=DB::table('directsales')
->where('directsales.client_id','=',$id)
->sum('directsales.total');
$pdf=PDF::loadView('reports.directsalereport',['dsale' => $dsale,'sumt'=> $sumt]);
return $pdf->download('dsread.pdf');
}
You may need to delete the vendor folder, then run composer install
I'm using Yii2-advanced-template. I've 2 divisions on my form as follows - When I'll click on any of the radio button in 2nd div, the details of respective entry should be loaded in 1st div. That is, the form in 1st div will remain as is, but loads some values for update operation. But I'm getting it as - which is not what I expect. It is loading the whole page layout in that div.
I already used 'renderPartial()' in my controller. But it solved only the half of the problem, that is, the navigation bar & other layout is hidden after using renderPartial().
My form is as follows-
<div class="col-sm-12">
<div class="col-sm-6 fillitemform">
...
Enter Item Details form
...
</div>
<div id="item_details" class="col-sm-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Item details</h3>
</div>
<div class="panel-body">
<div class="table-responsive" >
<table class="table">
<thead>
<tr><th></th>
<th>PO No.</th>
<th>SKU</th>
<th>Order Type</th>
<th>Expected Qty</th>
<th>Received Qty</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
<?php
for($itr = 0, $ro = $modelRO, $rod = $modelROD; $itr < count($modelROD); ++$itr){
echo '<tr onclick="placeitemdtls('.$rod[$itr]['id'].')">';
echo '<td><input type="radio" name="item"></td>';
echo '<td>'.$ro[0]['ro_po_no'].'</td>';
echo '<td>'.$rod[$itr]['rod_sku'].'</td>';
echo '<td>'.$ro[0]['ro_order_type_id'].'</td>';
echo '<td>'.$rod[$itr]['rod_expected_qty'].'</td>';
echo '<td>'.$rod[$itr]['rod_received_qty'].'</td>';
echo '<td>'.$rod[$itr]['rod_weight'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
My JS/jQuery function-
function placeitemdtls(id){
$.post("index.php?r=receiving-order-details/update&id="+id, function(response) {
jQuery(".fillitemform").html(response);
});
}
My Controller function-
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelRO = ReceivingOrders::find()->where(['id' => $id])->all();
$modelROD = ReceivingOrderDetails::find()->where(['receiving_order_id' => $id])->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['create', 'id' => $id]);
} else {
return $this->renderPartial('update', [
'model' => $model, 'modelRO' => $modelRO, 'modelROD' => $modelROD,
]);
}
}
Please note that, I'm getting the correct form with proper values, but having rendering problem as I showed in 2nd image. Please help me to solve it. So that my output should look like -