I have a foreach loop to check all array of ID and if using in_array to see if any of the arrays of IDsis equaled my $_POST['id'] as shown below:
$cart = array (
'title' => $_POST['title'],
'price' => $_POST['price'],
'img_src' => $_POST['img_src'],
'id' => $_POST['id'],
);
foreach ($_SESSION['cart'] as $item) {
$id = $item['id'];
}
if(in_array($_POST['id'], $id)){
echo "ID exist";
}else{
$_SESSION['cart'][] = $cart;
$count = count($_SESSION["cart"]);
}
For some reason, it keeps adding even when the ID exist inside the list of arrays of IDs.
You are just changing the value of $id inside foreach loop. Try to store value in array. Refer the below code:
$cart = array (
'title' => $_POST['title'],
'price' => $_POST['price'],
'img_src' => $_POST['img_src'],
'id' => $_POST['id'],
);
$id = array();
foreach ($_SESSION['cart'] as $item) {
$id[] = $item['id'];
}
if(in_array($_POST['id'], $id)){
echo "ID exist";
}else{
$_SESSION['cart'][] = $cart;
$count = count($_SESSION["cart"]);
}
Related
The assignment is to have a table display some products with an add to cart link which sends the user to another page which uses the information in the link to get the data to display the total of the purchase. The table is adding in new items that are clicked but not updating the actual quantity when an item is clicked more than once.
I have tried placing an echo in the check for the item already being in the cart and it does state the quantity of 1 before the quantity update and 2 after the update but for some reason this is not actually updating. The same goes for the price.
<?php
session_start();
if(empty($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
$cart = $_SESSION['cart'];
$id = $_GET['productid'];
$name = $_GET['productname'];
$price = $_GET['productprice'];
$qty = 1;
$alreadyHas = 0;
foreach($cart as $cartKey => $cartItem) {
if(!empty($cartItem['pid'])) {
if($cartItem['pid'] == $id) {
$cartItem['qty'] = $cartItem['qty'] + 1;
$cartItem['total'] = $cartItem['price'] * $cartItem['qty'];
$alreadyHas = 1;
}
}
}
if($alreadyHas == 0) {
$cartItem = array(
'pid' => $id,
'name' => $name,
'price' => $price,
'qty' => $qty,
'total' => $price
);
array_push($cart, $cartItem);
}
$_SESSION['cart'] = $cart;
?>
Your problem is in your foreach loop. You are updating $cartitem however because you are not passing it by reference in the loop the $cart variable itself is not being updated. Try changing the foreach loop to this (note the addition of & before `$cartItem):
foreach($cart as $cartKey => &$cartItem) {
Note that you could simplify your code with the use of array_search and array_column:
if (($k = array_search($id, array_column($cart, 'pid'))) !== false) {
$cart[$k]['qty'] = $cart[$k]['qty'] + 1;
$cart[$k]['total'] = $cart[$k]['price'] * $cart[$k]['qty'];
}
else {
$cartItem = array(
'pid' => $id,
'name' => $name,
'price' => $price,
'qty' => $qty,
'total' => $price
);
array_push($cart, $cartItem);
}
I am trying to add 1 to session['cart']['qty'] if it's not a new entry and if it is a new item it will just add the item to session['cart'].
if (!isset($_SESSION['cart'])) {
$item = array('pid' => $p['productID'],
'qty' => 1
);
$_SESSION['cart'][0] = $item;
} else {
$item_id = array_column($_SESSION['cart'], "pid");
if (in_array($p['productID'], $item_id)) {
$to_update = 'qty';
$new_qty = 5;
$base = $_SESSION['cart']['pid']['qty'];
} else {
$count = count($_SESSION['cart']);
$item = array('pid' => $p['productID'],
'qty' => 1
);
$_SESSION['cart'][$count] = $item;
}
}
You could use the $pid as a unique index to track like
$pid=$p['productID'];
if(!isset($_SESSION['cart'][$pid])){
$item = array(
'pid' => $pid,
'qty' => 1
);
$_SESSION['cart'][$pid] = $item;
}else{
/*
* add 1 qty
*/
if(isset($_SESSION['cart'][$pid])) {
$_SESSION['cart'][$pid]['qty']= ($_SESSION['cart'][$pid]['qty'] +1);
}
}
you cal also use a similar reduce quantity.
You are not changing the session variable. Try this:
if (!isset($_SESSION['cart'])) {
$item = array('pid' => $p['productID'],
'qty' => 1
);
$_SESSION['cart'][0] = $item;
} else {
$item_id = array_column($_SESSION['cart'], "pid");
if (in_array($p['productID'], $item_id)) {
$new_qty = 5;
$_SESSION['cart'][$p['productID']]['qty'] += $new_qty;
} else {
$count = count($_SESSION['cart']);
$item = array('pid' => $p['productID'],
'qty' => 1
);
$_SESSION['cart'][$count] = $item;
}
}
This line here doesn't make sense.
$base = $_SESSION['cart']['pid']['qty'];
Multidimensional arrays don't work like that.
You are trying to access the key qty from the array $_SESSION['cart']['pid'], but that does not exist. Those two keys are siblings.
I just want to add my session values in array but it displays only the last element. Here is my code
if(isset($_SESSION["cart_products"])) {
foreach ($_SESSION["cart_products"] as $cart_item)
{
$p_name = $cart_item["info2"];
}
echo $p_name;
}
$product_files[] = array(
'name' => $p_name,
'filename' => $p_name,
'source'=> $c_path );
You can do the following
if(isset($_SESSION["cart_products"])) {
$args = array();
foreach ($_SESSION["cart_products"] as $cart_item){
$args[] = $cart_item["info2"];
}
print_r($args);
updated
if(isset($_SESSION["cart_products"])) {
$p_name = array();
foreach ($_SESSION["cart_products"] as $cart_item)
{
$p_name = $cart_item["info2"];
$product_files[] = array(
'name' => $p_name ,
'filename' => $p_name,
'source'=> $c_path);
}
}
updated $product_files inside loop
how to update plan with vendor_plan_task_status_mapp table.
the model for plan update is
public function updatePlanData(){
$planId = $this->input->post('plan_id');
$data = array(
'plan_title' => $this->input->post('plan_title'),
'plan_price' => $this->input->post('plan_price'),
'plan_desc' => $this->input->post('plan_desc')
);
$this->db->where('plan_id', $planId);
$this->db->update('tbl_plan', $data);
$this->db->where('plan_id',$planId);
$this->db->delete('plan_task_mapping');
foreach ($this->input->post('task_id') as $key => $value)
{
$data2 = array(
'plan_id' => $planId,
'task_id' => $value
);
// echo "Index {$key}'s value is {$value}.";
$this->db->insert('plan_task_mapping', $data2);
}
//-------- HEAR I NEED A CODE TO UPDATE The V_T_S_M table-----------
}
after 1st table update i want to update the data in vendr_task_status_mapping table?????
IN THIS ANSWER YOU GET AN ERROR IF YOU CHANGE THE PLAN BUT USING THIS CODE YOU HAVE TO SIMPLY EDIT AND UPDATE IT ANGIN AND THIS MODEL IS CREATE A NEW ID FOR YOUR TASK AND INSERT IT AGAIN.
public function vendorUpdateModel($data)
{
$vendor_id = $this->input->post('vendor_id');
$data = array(
'category_id' => $this->input->post('category_id'),
'plan_id' => $this->input->post('plan_id'),
'city_id' => $this->input->post('city_id'),
'business_name' => $this->input->post('business_name'),
'owner_name' => $this->input->post('owner_name'),
'contact_no1' => $this->input->post('contact_no1'),
'contact_no2' => $this->input->post('contact_no2'),
'vendor_email' => $this->input->post('vendor_email'),
'subscription_date' => $this->input->post('subscription_date'),
'vendor_description' => $this->input->post('vendor_description'),
'vendor_address' => $this->input->post('vendor_address')
);
$this->db->where('vendor_id', $vendor_id);
$this->db->update('vendor',$data);
$this->db->where('vendor_id',$vendor_id);
$this->db->delete('vendor_task_status_mapping');
$this->db->select('task_mapp_id');
$this->db->where('plan_id',$this->input->post('plan_id'));
$query = $this->db->get('plan_task_mapping');
foreach($query->result() as $row)
{
$data2 = array(
'task_mapp_id' => $row->task_mapp_id,
'vendor_id' => $vendor_id,
'task_status' => '0'
);
$this->db->insert('vendor_task_status_mapping', $data2);
}
return;
}
If you added one field in plan_task_mapping table for add unique number then... As you mention your code:
$unique=array();
foreach ($this->input->post('task_id') as $key => $value)
{
$no=rand(0, 15);
$data2 = array(
'unique_no'=>$no,
'plan_id' => $planId,
'task_id' => $value
);
$unique[] = $no; //store no in array to use it.
// echo "Index {$key}'s value is {$value}.";
$this->db->insert('plan_task_mapping', $data2);
}
Before deleting plan_task_mapping table data. fetch that data.
function select()
{
$this->db->where('plan_id',$planId);
$Q = $this->db->get('plan_task_mapping');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
Then delete the data as you mention in you code:
$this->db->where('plan_id',$planId);
$this->db->delete('plan_task_mapping');
Then delete that old data from VTSM table:
$this->db->where('vendor_plan_task_mapping_id',$data[0]['id']); //this data[0]['id'] come form select(). change field name if required.
$this->db->delete('VTSM');
Here fetch that new inserted data by that unique no: //which we stored it in array.
foreach($unique as $u_no)
{
$this->db->where('unique_no',$u_no);
$Q = $this->db->get('plan_task_mapping');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row1)
{
$plan[] = $row1;
}
}
$Q->free_result();
return $plan;
}
In above code we have fetched that new inserted data to get their id to insert status.
Now inserting status:
foreach($plan as $a)
{
$Sdata=array(
"plan_task_mapping_id"=>$a['id'], //this is new id change name if required
"status"="your new status");
$this->db->insert('VTSM',$Sdata);
}
Use $this->db->insert_batch(); instead of inserting in foreach loop.
Check at this link how to use it.
I populate the $_SESSION['products'] array from a file:
$myFile = '.\products.txt';
$handle = fopen($myFile, 'r');
while (!feof($handle))
{
$prod = explode('|', fgets($handle));
$_SESSION['products'] = array($prod[4] => array(
'name' => $prod[0],
'price' => $prod[1],
'description' => $prod[2],
'image' => $prod[3]));
}
Then I want to loop through it, printing all the names and prices:
foreach ($_SESSION['products'] as $prodID=>$value) {
echo $_SESSION['products'][$value]['name'];
echo $_SESSION['products'][$value]['price'];
}
But it doesn't seem to work!
You need to check again how the foreach works. In your case, you can simply do the following:
foreach($_SESSION['products'] as $value) {
echo $value['name'];
echo $value['price'];
}
Your problem is, here you are overwriting the products array with every product, which means there will only ever be the last product present:
$_SESSION['products'] = array($prod[4] => array(
Try appending to the array like:
$_SESSION['products'][$prod[4]] = array(
// ^^^^^^^^ set the key as the product ID here
'name' => $prod[0],
'price' => $prod[1],
'description' => $prod[2],
'image' => $prod[3]);
Also your foreach is wrong, try
foreach ($_SESSION['products'] as $prodID=>$value) {
echo $value['name'];
echo $value['price'];
}
foreach ($_SESSION['products'] as $prodID=>$value) {
echo $_SESSION['products'][$prodID]['name'];
echo $_SESSION['products'][$prodID]['price'];
}