Jump to content

Shagged multiple updates could use some help


dragon_sa

Recommended Posts

I have a form which shows products ordered from a catalog, it used to work fine when I had individual change buttons for each item, now I have to have multiple check boxes for removing items and the ability to change the quantities of multiple items with a single button. I know you have to use foreach and arrays for this but I am confusing myself trying to make the changes for it to update the correct items.

 

Attached is a pic of what the form looks like and is how it is supposed to function

 

here is the form part

<?php 
for ($basket_counter=0;$basket_counter<$_SESSION['ses_basket_items'];$basket_counter++) {
  		$price=sprintf("%01.2f",$ses_basket_price[$basket_counter]);
   		$quantity=$ses_basket_amount[$basket_counter];
	$code=$ses_basket_stockcode[$basket_counter];
	$itemID=$ses_basket_id[$basket_counter];
   		$name=$ses_basket_name[$basket_counter];
	$image=$ses_basket_image[$basket_counter];
	if ($country='AU') { $price=sprintf("%01.2f",($price*1.1));
	$unit=sprintf("%01.2f",($price/$quantity));
	 }
	else
	{ $unit=sprintf("%01.2f",($price/$quantity)); }
?><form method='post' action='' target="_self">
		<tr>
		  <td align='center' class='rescon' style="border-bottom:solid #330000 1px;"><input type="checkbox" name="remove[]" value="<?php echo $itemID; ?>" /></td>
		  <td align='left' class='rescon' style="border-bottom:solid #330000 1px;"><img src="product_images/<?php echo $image; ?>" width="60" alt="<?php echo $name; ?>" title="<?php echo $name; ?>" /></td>
		  <td align='left' class='rescon' style="border-bottom:solid #330000 1px;"><font size="+1"><?php echo $name; ?></font><br/><?php echo $code; ?></td>
            	<td align='left' class='rescon' style="border-bottom:solid #330000 1px;"> </td>
            	<td align='center' class='rescon' style="border-bottom:solid #330000 1px;"><input name="price" type="hidden" value="<?php echo $unit; ?>"><input type="hidden" name="pageLink" value="<?php echo $pageLink; ?>" /><input name="basket[]" type="hidden" value="<?php echo $itemID; ?>"><input name="quantity[]" style="vertical-align:middle;" type="text" value="<?php echo $quantity; ?>" size="2" maxlength="5"> </td>
            	<td align='center' class='rescon' style="border-bottom:solid #330000 1px;">$<?php echo $unit; ?></td>
            	<td class='rescon' align='right' bgcolor="#FFFF00" style="border-bottom:solid #330000 1px;">$<?php echo $price; ?> </td>
            </tr>
<?php }  
if ($country='AU') { $totalprice=sprintf("%01.2f",array_sum($ses_basket_price)); $totalprice=sprintf("%01.2f",($totalprice*1.1)); }
	else
	{ $totalprice=sprintf("%01.2f",array_sum($ses_basket_price)); }
   	$totalitems=array_sum($ses_basket_amount); ?>
   			<tr><td align='left' colspan='4' valign="top" class='cartbot'> </td>
            	<td align='left' valign="top" class='cartbot'><?php echo $totalitems; ?> Items</td>
                <td align='right' colspan='2' class='cartbot'><?php echo "<b>Subtotal:  $".$totalprice." </b>"; ?></b></td>
         	</tr>
   			<tr>
   			  <td align='left' colspan='5' valign="top"><input type="submit" id="change" name="change" style="vertical-align:middle;" value="Change"></td>
   			  <td align='right' colspan='2'> </td>
	  </tr></form>

 

and here is the processing part at the top of the page which I have sorta shagged, could use some help getting it to update the correct items for remove and quantity changes

// cart application
if (isset($_POST['change'])) {
$basket = $_POST['basket'];
// check faor AU to include GST
if ($country='AU') { $itemprice = sprintf("%01.2f",(($_POST['price']/11)*10)); }
else
{ $itemprice = $_POST['price']; }
$itemqty = $_POST['quantity'];
$newprice = ($itemprice*$itemqty);
if (($basket!="") && (isset($_POST['change']))){
   	if ($_SESSION['ses_basket_items']){
	// basket position
	$basket_position_counter=0;
	// double entry flag set to NO
	$double=0;
	// Check for existing basket id
	if ($_SESSION['ses_basket_items']>0){
	   foreach ($ses_basket_id as $basket_item){
	      if ($basket_item==$basket){
	         // If exist flag for update
			 $double=1;
	         $basket_position=$basket_position_counter;
	      }
		  // Add new basket position
	      $basket_position_counter++;
	   }
	}
	// Update basket with new quantity and price
	if ($double==1){
	   $ses_basket_amount[$basket_position]=$itemqty;
	   $ses_basket_price[$basket_position]=$newprice;
	}
}
// Delete Item when set to 0
if ($itemqty == "0") {
	array_splice ($ses_basket_name, $basket_position, 1);
	array_splice ($ses_basket_amount, $basket_position, 1);
	array_splice ($ses_basket_price, $basket_position, 1);
	array_splice ($ses_basket_stockcode, $basket_position, 1);
	array_splice ($ses_basket_image, $basket_position, 1);
	array_splice ($ses_basket_id, $basket_position, 1);
	$_SESSION['ses_basket_items']--;
}
if (isset($remove) && ($remove!='')) {
	$remove = $_POST['remove'];
    	if(count($remove) > 0){
       		foreach($remove AS $removed){
				array_splice ($ses_basket_name, $basket_position, 1);
			array_splice ($ses_basket_amount, $basket_position, 1);
			array_splice ($ses_basket_price, $basket_position, 1);
			array_splice ($ses_basket_stockcode, $basket_position, 1);
			array_splice ($ses_basket_image, $basket_position, 1);
			array_splice ($ses_basket_id, $basket_position, 1);
			$_SESSION['ses_basket_items']--;
        	}
    	}
}
}
if ($_SESSION['ses_basket_items']==0){
unset($_SESSION['ses_basket_items']);
unset($_SESSION['ses_basket_name']);
unset($_SESSION['ses_basket_amount']);
unset($_SESSION['ses_basket_price']);
unset($_SESSION['ses_basket_stockcode']);
unset($_SESSION['ses_basket_image']);
unset($_SESSION['ses_basket_id']);
header("Location: $pageLink");
}
}

 

the array variables I have here are $basket, $remove and $quantity.

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.