php CI $this->cart->insert($data) get null value - php

I get stack in script. I used Code Igniter ver. 3.1.10. I use cart library in my controller
here my controller
public function add_to_cart()
{
$idit=$this->input->post('id_item');
$product=$this->Salesmodel->get_item($idit);
$i=$product->row_array();
$data = array(
'id' => $i['id_item'],
'name' => $i['name_item'],
'main_price' => $i['main_price'],
'sell_price' => $i['sell_price'],
);
$this->cart->insert($data);
$rows = count($this->cart->contents()); // I want to find out rows count and result is null
echo $i['id_item']; //get value, not null
echo $rows; // get '0'
}
model.php
function get_item($idit)
{
$rslt=$this->db->query("SELECT * FROM tb_item where id_item='$idit'");
return $rslt;
}
but in that script i always get null row count of the cart.
I have to add this script in config.php :
$config['sess_use_database'] = TRUE;
I also created a new table with a name
ci_session
but that returns the same result, my cart always has null row count and null data. Please help me with an error in the script that I made.
Thanks in advance

In order to save into the cart properly, these 4 array index are required :
id - Item identifier.
qty - Item quantity.
price - Item price.
name - Item name.
And the 5th index are options, which you could store all the additional attribute you need (should be an array though).
So you could modify the $data array like this :
$data = array(
'id' => $i['id_item'],
'qty' => 1, // here I just manually set it to 1
'name' => $i['name_item'],
'price' => $i['main_price'], // here I changed 'main_price' index to 'price'
'options' => array('sell_price' => $i['sell_price']) // moved the 'sell_price' array here
);

Related

Check record if exist using updateOrCreate and do a math(SUM) if record exist in laravel 5.5

I have a store function that saves array items into my items table and together with that I am trying to check if the product_is is already in my Warehouse1StockSummaries. if still not, I will grab the product_id and its qty, If its there already then I want to ADD the value from the 'stock_in_qty' which is inside the array to the 'qty_in' in my Warehouse1StockSummaries. I hope my explanation make sense to you :)
here's my code.
public function store(Request $request)
{
$input = $request->all();
$items = [];
for($i=0; $i<= count($input['stock_in_qty']); $i++) {
if(empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;
$acceptItem = [
'order_id' => $input['order_id'][$i],
'product_id' => $input['product_id'][$i],
'order_item_id' => $input['order_item_id'][$i],
'delivery_date' => $input['delivery_date'][$i],
'company_id' => $input['company_id'][$i],
'stock_in_qty' => intval($input['stock_in_qty'][$i]),
'stock_out_qty' => $input['stock_out_qty'][$i],
'transfer_to' => $input['transfer_to'][$i],
'delivery_note' => $input['delivery_note'][$i],
'user_id' => $input['user_id'][$i]
];
$product_id = $input['product_id'][$i];
$qty_in = intval($input['stock_in_qty'][$i]);
// dd($qty_in);
// ADD stock_in_qty TO QTY_IN ????
$stockSummary = Warehouse1StockSummaries::updateOrCreate(
['product_id' => $product_id ],
['qty_in' => $qty_in,
'qty_out' => null
]);
// dd($stockSummary);
array_push($items, Warehouse1stocks::create($acceptItem));
}
return redirect()->route('orders.index');
}
I check and everything is ok the only missing is the part where I need to grab the value from 'stock_in_qty' and add to 'qty_in' if the product id is already found in Warehouse1StockSummaries. Thank you so much in advance!
You could use the wasRecentlyCreated property on the model to determine if the model has just been created or not. If it hasn't then it won't have used $qty_in value, this means you could then use the increment() to add to the existing value in the database:
$stockSummary = Warehouse1StockSummaries::firstOrCreate(
['product_id' => $product_id ],
['qty_in' => $qty_in, 'qty_out' => null]
);
if (!$stockSummary->wasRecentlyCreated) {
$stockSummary->increment('qty_in', $qty_in);
}

Programmatically update Menu Item in Component in joomla

I'm creating a component in Joomla, where among other things, can create and update menus, and I have successfully used this answer to create a new menu item.
https://joomla.stackexchange.com/questions/5104/programmatically-add-menu-item-in-component?newreg=1e7c576205354c0795a55607bc3e2508
$menuTable = JTableNested::getInstance('Menu');
// which menu you want to add to -
$menutype = 'thisismymenusname';
// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'menutype' => $menutype,
'title' => $table->alias,
'alias' => $table->alias,
'path' => $table->alias,
'type' => 'heading',
'component_id' => 0,
'language' => '*',
'published' => 1,
);
// this item is at the root so the parent id needs to be 1
$parent_id = 1;
$menuTable->setLocation($parent_id, 'last-child');
// save is the shortcut method for bind, check and store
if (!$menuTable->save($menuData))
{
$this->setError($menuTable->getError());
return false;
}
But I'm unable to find the correct way to update a menu item, like the title and alias. But it is unsuccessful. I have tried using this with no success.
$menu_title = $_POST['title'];
$menu_alias = JFilterOutput::stringURLSafe($menu_title);
$menuTable = JTableNested::getInstance('Menu');
// this is heading menu item but what data you have and require will vary per case - just look at an appropriate row in yr menu table
$menuData = array(
'title' => $menu_title,
'alias' => $menu_alias,
);
// this item is at the root so the parent id needs to be 1
$parent_id = ($_POST['parent'] != "" ? $_POST['parent'] : '1');
$menuTable->setLocation($parent_id, 'last-child');
// save is the shortcut method for bind, check and store
if (!$menuTable->set($_POST['menuid'],$menuData)) {
$this->setError($menuTable->getError());
exit;
}
Can anyone help?

How to update the quantity of all items in the cart with respect to database?

I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html

php logic on a system shopping cart and session value management

this is a question about the logic of an e-commerce cart built in php:
i have an item in the cart saved with a session in php, this session is named by a composed string where the unique key is given by the product_id:
$this->session->data['quote_total_'.$product_id];
the problem is when i add the same product in the cart but with diefferent price - the price is generated automatically by the system at the change of misures, options, and quantity -
indeed the first value of the cart session that is the value of the first product price is overwritten by the new one. which is the best method to avoid this overwrite for you?
i was thinking about a unique value to append at the name of the session but i don't understand which ones...
How about making this variable an array instead of a primitive datatype?
Instead of using the product ID as a key, just let the key be created automatically which will be numerical and incremented with each product
// add a product - new product will always be added
$this->session->data[] = array(
'product_id' => 123,
'qty' => 1,
'name' => 'Product Name',
'price' => 10
);
// you can iterate over the products like
foreach($this->session->data as $product)
{
print_r($product);
}
Append price with product id in session variable to uniquely identify each product with different prices.And also use array to store multiple products.
Create multidimensional array such as
$data = array([0]=>array(
'product_id' => 123,
'qty' => 1,
'name' => 'Product Name',
'price' => 10),
[1]=>array(
'product_id' => 123,
'qty' => 2,
'name' => 'Product Name',
'price' => 20)
);

How do I count the number of each unique array inside a multidimentional array? PHP

I'd like to build a simple shopping cart using arrays. I need to display each unique item in the shopping cart aswell as the quantity of each item along side.
My initial cart array might look like this:
$cart=
array(
array(2003,100,"Table")
,array(2003,100,"Table")
,array(2003,100,"Table")
,array(2004,200,"Chair")
,array(2004,200,"Chair")
);
The first value in each array is the product_id, then the price & then the product name.
How do I print each unique item once aswell as the quantity of each along side?
Thanks in advance.
$new_cart = array();
foreach($cart as $product) {
if(!isset($new_cart[$product[0]])) {
$new_cart[$product[0]] = array('quantity' => 1, 'label' => $product[2], 'price' => $product[1]);
}
else {
$new_cart[$product[0]]['quantity']++;
}
}
I strongly suggest using associative arrays for this though. Your problem is the way you are storing the values. Try using something like this:
$cart_items = array(
2004 => array('quantity' => 3, 'label' => 'Leather Chair', 'price' => 200),
2901 => array('quantity' => 1, 'label' => 'Office Desk', 'price' => 1200),
);
When a user updates a quantity or adds an existing product simply increment the quantity.
You could simply iterate the array and use the product ID as key to count the amounts:
$amounts = array();
foreach ($cart as $item) {
if (!isset($amounts[$item[0]])) $amounts[$item[0]] = 0;
$amounts[$item[0]]++;
}
But it would be easier if your cart just stores the product IDs and amounts, so:
array(
2003 => 3,
2004 => 2
)
This is actually what the algorithm above is doing. But with this, you have all the information you need (product ID and amount).

Categories