I am getting no error while batch update of array data. The submit and show me no error. Could you please help me to figure this problem out.
$invoice_id = $this->input->post('id');
$desc = $this->input->post('desc[]');
$size = $this->input->post('size[]');
$stock = $this->input->post('stock[]');
$color = $this->input->post('color[]');
$qty = $this->input->post('qty[]');
$price = $this->input->post('price[]');
$linetotal = $this->input->post('linetotal[]');
$invoice_id = $this->input->post('id');
$input_data = array();
for ($i = 0; $i < count($desc); $i++ )
{
$input_data[] = array(
'desc' => $desc[$i],
'size' => $size[$i],
'stock' => $stock[$i],
'color' => $color[$i],
'qty' => $qty[$i],
'price' => $price[$i],
'linetotal' => $linetotal[$i],
);
}
$this->db->where('invoice_id', $invoice_id);
$this->db->update_batch('invoice_data',$input_data, 'invoice_id');
$this->session->set_flashdata('success', 'Data added successfully.');
redirect('invoices');
I am using id field in my view too. But can't get the data updated.
Related
I am tring Order update.I need to update multi product in different row. when I update the product then it only updated created fields. i want to update current row and then new product insert in different row.
$product_id = $request->product_id;
$seller_id = Auth::user()->id;
$price = $request->product_price;
$quantity = $request->product_qty;
$payment_status = $request->payment_status;
$delivery_status = $request->delivery_status;
$shipping_type = "home_delivery";
$shipping_charge = $request->shipping_cost;
$user = Address::all()->where('user_id', $request->user_id)->first();
$cname= DB::table('users')->where('id', $user->user_id)->value('name');
$shipping_address = [
'user_id'=>$request->user_id,
'name'=> $request->name,
'phone'=>$request->phone,
'email'=>$user->email,
'city'=>$user->city,
'postal_code'=>$user->postal_code,
'country'=>$user->country,
'address'=>$request->address,
];
$order_id=$request->id;
$order = Order::findOrFail($order_id)->update([
'seller_id' => Auth::user()->id,
'shipping_address' => json_encode($shipping_address),
'payment_status' => $request->payment_status,
'payment_type' => $request->payment_option,
'payment_details' => $request->payment_option,
'coupon_discount' => $request->coupon_discount,
'round_amount' => $request->round_discount,
'grand_total' => $request->grand_total,
]);
for($i = 0; $i <= count($request->product_id)-1; $i++){
$data = [
'order_id' => $order_id,
'product_id' => $product_id[$i],
'seller_id' => $seller_id,
'price' => $price[$i],
'quantity' => $quantity[$i],
'payment_status' => $payment_status,
'delivery_status' => $delivery_status,
'shipping_type' => $shipping_type,
'shipping_cost' => $shipping_charge,
];
if(!empty(OrderDetail::where('order_id', $order_id))){
OrderDetail::where('order_id', $order_id)->update($data);
}
else{
Orderdetail::insert($data);
}
$product_stock_info = ProductStock::where('product_id', $product_id[$i])->first();
// dd($product_id[$i]);
$stock = $product_stock_info->qty;
$updateStock = $stock - $quantity[$i];
$product_stock_info->qty = $updateStock;
$product_stock_info->save();
}
my problem is here
if(!empty(OrderDetail::where('order_id', $order_id))){
OrderDetail::where('order_id', $order_id)->update($data);
} else {
Orderdetail::insert($data);
}
You can use updateOrCreate method .
the updateOrCreate method persists the model, so there's no need to manually call the save method
$flight = Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);
as like
$whereData = [
'order_id' => $order_id
];
OrderDetail::updateOrCreate($whereData ,$data);
I have an ordering app which have order items. I was able to update the field which only have single data but I can't make the field that handle an array data work. The order items belongs to another model called OrderItems. Can you help on to deal with this properly?
here's my update function.
public function update(Request $request, Orders $order )
{
$this->validate($request, [
'user_id' => 'required',
'status_id' => 'required',
'currency_id' => 'required',
'company_id' => 'required',
'purchase_no' => 'required|unique:orders,purchase_no,'.$order->id,
'notes' => '',
'admin_notes' => '',
'delivery_date' => '',
'publish' => '',
'product_id' => 'required',
'product_code' => 'required',
'product_name' => 'required',
'quantity' => 'required'
]);
$order->user_id = $request->input('user_id');
$order->status_id= $request->input('status_id');
$order->currency_id = $request->input('currency_id');
$order->company_id = $request->input('company_id');
$order->purchase_no = $request->input('purchase_no');
$order->notes = $request->input('notes');
$order->admin_notes = $request->input('admin_notes');
$order->delivery_date = $request->input('delivery_date');
$order->publish = $request->input('publish');
$order->grandtotal = (float) str_replace(',', '', $request->input('grandtotal'));
$order->save();
$input = Orderitems::findOrFail($order->id);
for($i=0; $i<= count($input['quantity']); $i++) {
if(empty($input['quantity'][$i]) || !is_numeric($input['quantity'][$i])) continue;
$items->product_id = $input->input('product_id')[$i];
$items->product_code = $input->input('product_code')[$i];
$items->product_name = $input->input('product_name')[$i];
$items->cost = $input->input('cost')[$i];
$items->quantity = $input->input('quantity')[$i];
$items->total_cost = (float) str_replace(',', '', $input->input('total_cost')[$i]);
$orderItems->save();
}
return redirect()->route('orders.index');
}
Here's the screenshot from my update form
Thank you so much in advance!
You have written wrong code for the update.it should be like this.
You are saving $orderItemsobject which hasn't updated in your code.
$items= $order->orderItems;
// still code not updated here i want to print product _id ?
foreach($items as $key => $item) {
$item->product_id = $request->input('product_id')[$key];
$item->product_code = $request->input('product_code')[$key];
$item->product_name = $request->input('product_name')[$key];
$item->cost = $request->input('cost')[$key];
$item->quantity = $request->input('quantity')[$key];
$item->total_cost = (float) str_replace(',', '', $request->input('total_cost')[$key]);
$item->save();
}
I am currently working on adding multiple requests in single click using CodeIgniters insert_batch.
This is my model
function add_request($data) {
$this->db->insert_batch('requests',$data);
}
This is my controller
if($_POST) {
$code = $this->input->post('code');
$about = $this->input->post('about');
$qnty = $this->input->post('quantity');
$budget = $this->input->post('budget');
$sched = $this->input->post('sched');
for($i = 0; $i < count($code); $i++) {
$data[$i] = array(
'code' => $code,
'description' => $_POST['desc'],
'qnty' => $qnty,
'budget' => $budget,
'sched' => $sched,
'from' => $this->session->userdata('user_id'),
'status' => 'Pending',
'about' => $about
);
$this->request->add_request($data[$i]);
}
This code doesnt work It only adds blank record.
Consider this code for the controller:
$data = array();
for($i = 0; $i < count($code); $i++) {//build the array
$data[$i] = array(
'code' => $code,
'description' => $_POST['desc'],
'qnty' => $qnty,
'budget' => $budget,
'sched' => $sched,
'from' => $this->session->userdata('user_id'),
'status' => 'Pending',
'about' => $about
);
}
//$data will be a bidimentional array
//pass $data to the model after the looping is done, thus the array is complete
$this->request->add_request($data);
now i am having a hard time in placing all my values in an associative array.
Because I can only get the last value from query. Not all the values. I can't spot the error.
Here's my code:
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE cscart_order_data.type = 'I'
AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
echo "<pre>";
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = array(
'id' => $row1['order_id'],
'total' => $row1['total'],
'status' => $row1['status'],
'data' => unserialize($row1['data_serialize']),
'date' => $row1['date']
);
}
$range = array();
foreach($var_data as $data){
$id = $data['id'];
$total = $data['total'];
$cost = $data['data']['cost'];
$var_date = $data['date'];
$status => $data['status'];
$date_var = date('Y-m-d H:i:s',$var_date);
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$range[] = $cost_ratio;
$test = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
}
echo "</table>";
print_r($test); //this will return the last index from my array
That;s my problem guys i hope you can help me. Thanks.
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE cscart_order_data.type = 'I'
AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
echo "<pre>";
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = array(
'id' => $row1['order_id'],
'total' => $row1['total'],
'status' => $row1['status'],
'data' => unserialize($row1['data_serialize']),
'date' => $row1['date']
);
}
$range = array();
$test = array();
foreach($var_data as $data){
$id = $data['id'];
$total = $data['total'];
$cost = $data['data']['cost'];
$var_date = $data['date'];
$status => $data['status'];
$date_var = date('Y-m-d H:i:s',$var_date);
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$range[] = $cost_ratio;
$test[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
}
echo "</table>";
print_r($test); //this will return the last index from my array
This piece of code is loading a scalar value
$test = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
And should be
$test[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
I'm working on this e-commerce site and I'm trying to create a jSON array containing the cart items in PHP.
So far I have:
for ($i=0; $i < count($_SESSION['cart']); $i++) {
$prodid = $_SESSION['cart'][$i][0];
$sizeId = $_SESSION['cart'][$i][1];
$colorId = $_SESSION['cart'][$i][2];
$qty = $_SESSION['cart'][$i][3];
$inslagning = $_SESSION['cart'][$i][4];
$wrapCost += ($inslagning == 'YES' ? 20 : 0);
$row = get_product_buy($prodid, $sizeId, $colorId);
$prodname = $row['prodname'];
$color = $row['color'];
$size = $row['size'];
$prodCatid = $row['catid'];
$image = $row['biggerimage'];
$box = $row['box_number'];
for ($j=0;$j<$qty;$j++) {
$cart = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
}
I know I have the $cart var inside the loop which is probably wrong. The end result should be like this:
$cart = array(
array(
'reference' => '123456789',
'name' => 'Klarna t-shirt',
'quantity' => 1,
'unit_price' => $att_betala * 100,
'discount_rate' => 0,
'tax_rate' => 2500
),
array(
'reference' => '123456789',
'name' => 'Klarna t-shirt',
'quantity' => 1,
'unit_price' => $att_betala * 100,
'discount_rate' => 0,
'tax_rate' => 2500
)
);
All help is appreciated!
You have to append a new child to $cart instead of overwriting it. To append values to an array (the easy way), use $array[] = …. PHP increments the child's ID automatically.
Not required, but please initialize $cart first and use descriptive variables.
To inspect an array (or other data), use var_dump.
// Initialize an empty array. Not needed, but correct to do.
$cart = array();
for ($i=0; $i < count($_SESSION['cart']); $i++) {
$prodid = $_SESSION['cart'][$i][0];
$sizeId = $_SESSION['cart'][$i][1];
$colorId = $_SESSION['cart'][$i][2];
$qty = $_SESSION['cart'][$i][3];
$inslagning = $_SESSION['cart'][$i][4];
$wrapCost += ($inslagning == 'YES' ? 20 : 0);
$row = get_product_buy($prodid, $sizeId, $colorId);
$prodname = $row['prodname'];
$color = $row['color'];
$size = $row['size'];
$prodCatid = $row['catid'];
$image = $row['biggerimage'];
$box = $row['box_number'];
// Append products $qty times.
for ($productCount=0; $productCount<$qty; $productCount++) {
// Append a new product to $cart.
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
}
Use like this
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
Try Using
for ($j=0;$j<$qty;$j++) {
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
$json_enc = json_encode($cart);
You are not appending to the $cart variable, you are overwriting it on every pass of the loop.
Use the [] syntax to append to an array:
$cart[]=...
Also, its good to declare an empty array at the top of the code:
$cart=array();