Getting data from mysql in result array and batch updating - codeigniter

I need a quick solutions to this problem as I'm getting uninitialized offset 2 error codes
$this->db->select('bprice');
$this->db->from('items');
$this->db->limit(20);
$query = $this->db->get()->result_array();
$data = array();
foreach($query as $key => $value){
$data = array(
'branch_price' => (float)$value['bprice'][$key] + ((float)$value['bprice'][$key] * 0.30)
);
}
return $this->db->update_batch('items',$data);

$this->db->select('bprice');
$this->db->from('items');
$this->db->limit(20);
$query = $this->db->get()->result_array();
$data = array();
foreach($query as $query){
$data = array(
'branch_price' => (float)$query['bprice'] + ((float)$query['bprice'] * 0.30)
);
}
return $this->db->update_batch('items',$data);

Related

Image Update Problem Using Laravel Query Builder

My All Data has been updated successfully but Image not update and old image not remove. I am using Query Builder.
Can anyone tell me what to do?
My Controller are given below
I want to know something new.
public function update(Request $request,$id)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'title' => 'required',
'details' => 'required',
]);
$data = array();
$data['name'] = $request->name;
$data['title'] = $request->title;
$data['details'] = $request->details;
$data['status'] = $request->status;
$image=$request->photo;
if ($image) {
$image_name = str_random(20);
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/upload/event';
$image_url = $upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
if ($success) {
$data['photo'] = $image_url;
$img = DB::table('events')->where('id',$id)->first();
$image_path = $img->photo;
$done = unlink($image_path);
$user = DB::table('events')->where('id',$id)->update($data);
if ($user) {
$notification = array(
'messege' => 'Data Updated Successfully',
'alert-type' => 'success'
);
return redirect('admin/event')->with($notification);
}else{
return redirect()->back();
}
}
}else{
return redirect()->back();
}
//return redirect('admin/event')->with($notification);
}

How to remove commas and spaces when submitting a excel file in laravel

I'm trying to save data that is in an excel file to my database and I would like to check if any of those has a comma or spaces in it and if it does I would like to remove it.
I'm using phpoffice/phpspreadsheet and laravel and if possible I would like to use phpoffice/phpspreadsheet if it can do it.
Here is my code
$xl = IOFactory::load($path);
$sheet = $xl->getSheetByName('Products');
$c = $sheet->getHighestDataColumn();
$r = $sheet->getHighestDataRow();
$data = $sheet->rangeToArray("A1:{$c}{$r}", null, false);
$headers = array_shift($data);
$projects = [];
foreach ($data as $row) {
$entry = array_combine($headers, $row);
$projects[] = [
'name' => $entry['Name'],
'description' => $entry['description'],
'price' => $entry['price'],
];
}
return $projects;
try to create a function :
function cleanData($data) {
$data= str_replace( ',', '', $data);
$data= preg_replace('/\s+/', '', $data);
return $data;
}
and use it:
foreach ($data as $row) {
$entry = array_combine($headers, $row);
$projects[] = [
'name' => cleanData($entry['Name']),
'description' => cleanData($entry['description'],
'price' => cleanData($entry['price']),
];
}

how to increment the quantity of a product in laravel session

my cart session is
Array
(
[0] => Array
(
[id] => 123-XXL
[prod_name] => Grey Shirt
[prod_rate] => 2100
[size] => XXL
[qty] => 1
[ind_tot] => 2100
)
[1] => Array
(
[id] => 134-XL
[prod_name] => red_shirt
[prod_rate] => 1800
[size] => XL
[qty] => 1
[ind_tot] => 1800
)
)
Now i tried to increment the qty value of the session by 1,
my increment function is
public function increment(Request $request){
$id = $request->id;
$rate = $request->rate;
$qty = $request->qty;
$ind_tot = $request->ind_tot;
$input = $request->all();
$qty_up = $qty+1;
$ind_tot = $ind_tot+$rate;
if(Session::has('cart_sess')){
$cart = Session::get('cart_sess');
foreach ($cart as $key => $value){
if($cart[$key]['id'] == $id){
$cart = $cart->replace([$cart[$key]['qty'] => $qty_up, $cart[$key]['ind_tot'] => $ind_tot]);
Session::set('cart_sess', $cart);
}
}
}
return response()->json($qty_up, 200);
}
When I console it shows the error as
500 (Internal Server Error)
How to update the values in laravel session array.
You can do it like this
public function increment(Request $request){
$id = $request->id;
$rate = $request->rate;
$qty = $request->qty;
$ind_tot = $request->ind_tot;
$input = $request->all();
$qty_up = $qty+1;
$ind_tot = $ind_tot+$rate;
if($request->session()->has('cart_sess')){
$cart = $request->session()->get('cart_sess');
foreach ($cart as $key => $value){
if($cart[$key]['id'] == $id){
$value['qty'] = $qty_up;
$value['ind_tot'] = $ind_tot;
}
}
}
$request->session()->put('cart_sess', $cart);
return response()->json($qty_up, 200);
}

display the users order history (laravel)

I want to create a page that shows users what they bought but I really have no idea how can I do this!
and this is cart view and I don't have cart model:
public function addtocart(Request $request){
$data = $request->all();
if (empty($data['user_email'])){
$data['user_email'] = ' ';
}
$user_id = Auth::id();
$session_id = Session::get('session_id');
if (empty($session_id)){
$session_id = Str::random(40);
Session::put('session_id' , $session_id);
}
DB::table('cart')->insert(['product_id' => $data['product_id'] , 'product_name' => $data['product_name'], 'user_id'=>$user_id,
'product_price' => $data['product_price'], 'qty' => $data['qty'], 'user_email' => $data['user_email'] , 'session_id' => $session_id ]);
return redirect('cart');
}
public function cart(){
$user_id = Auth::user()->id;
$session_id = Session::get('session_id');
$userCart = DB::table('cart')->where(['session_id'=>$session_id])->get();
foreach ( $userCart as $key=>$product){
$productDetail = Singleproduct::where('id' , $product->product_id)->first();
$userCart[$key]->image = $productDetail->image;
}
return view('UI.store.cart' , compact('userCart'));
}
can anyone help me in it plz?
To get a list of orders for a user you can do:
$history = DB::table('cart')
->where('user_id', auth()->id())
->join('products', 'products.id', '=', 'cart.product_id')
->select('products.name', 'cart.quantity', 'cart.product_price')
->get();
Then it's up to you to output it how you want

API response slow

My script works overall, but the API response is really slow.
This is what my code looks like:
<?php
error_reporting(E_ALL);
$config = include('config.php');
$predictions = include('predicton.php');
function fetchData($currency) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.coinmarketcap.com/v1/ticker/'.$currency.'/?convert=EUR'
));
$response = curl_exec($curl);
$response = json_decode($response, true);
curl_close($curl);
return $response;
}
function convertData($currency) {
global $config;
$response = fetchData($currency);
$id = $response[0]['id'];
$name = $response[0]['name'];
$value = $response[0]['price_eur'];
$perc_change_h = $response[0]['percent_change_1h'];
$perc_change_d = $response[0]['percent_change_24h'];
$perc_change_w = $response[0]['percent_change_7d'];
$amount = $config[$currency]['amount'];
$starting_price = $config[$currency]['starting_price'];
$spend = $amount * $starting_price;
$total = $amount * $value;
$profit = $total - $spend;
return array(
'id' => $id,
'name' => $name,
'value' => round($value, 4),
'change_hour' => $perc_change_h,
'change_day' => $perc_change_d,
'change_week' => $perc_change_w,
'amount' => $amount,
'starting' => number_format($starting_price, 4),
'spend' => number_format($spend, 2),
'total' => number_format($total, 2),
'profit' => number_format($profit, 2)
);
}
function convertPrediction ($currency, $percentage) {
global $config, $predictions;
$amount = $config[$currency]['amount'];
$prediction = $predictions[$currency]['value'];
$value = $amount * $prediction / 100 * $percentage;
$value = round($value, 2);
return $value;
}
This is the site: http://bitcap.kazuto.de/
A sample api request: https://api.coinmarketcap.com/v1/ticker/stellar/?convert=EUR
The response itself is quick, but not when running in my code. It takes around 11-13 seconds to refresh, even though not much data is requested at all.
Does anyone have an idea why?

Resources