slaterino Posted January 5, 2010 Share Posted January 5, 2010 Okay, Currently my code always adds just one item to my shopping basket at a time, which is completely fine for most items. However there are a few items that are in multiples, and are therefore only available in quantities of 5, 10 and 20. I have altered my code slightly so that when looking in the basket customers can change the quantities solely to these amounts but have a problem as when the basket page is opened the quantity always defaults to 1 item, even though it may show in the basket as 5. It is then necessary to click the 'Update Cart' button to refresh the amount. My question then is thus: what can I add to the script so that the update cart function is run as soon as the page loads? As this to me seems like the easiest way to do it. This is my script below. I want the mutliple items to default to the value of $multi1a and not to $qty (as when the page loads $qty will always be the value of 1) $output[] = '<td><select name="qty'.$id.'" value="'.$qty.'"><option selected value="'.$multi1a.'">'.$multi1a.'</option><option value="'.$multi2a.'">'.$multi2a.'</option><option value="'.$multi3a.'">'.$multi3a.'</option></td>'; } if ($pricing == "Multi") { if ($qty == $multi1a) { $output[] = '<td>£'.($multi1b).'</td>'; $total += ($multi1b); } if ($qty == $multi2a) { $output[] = '<td>£'.($multi2b).'</td>'; $total += ($multi2b); } if ($qty == $multi3a) { $output[] = '<td>£'.($multi3b).'</td>'; $total += ($multi3b); } } else { $output[] = '<td>£'.(($Price) * $qty).'</td>'; $total += ($Price) * $qty; } $output[] = '</tr>'; } $sum = array_sum($contents); $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/187212-update-page-using-some-form-of-php-function/ Share on other sites More sharing options...
Catfish Posted January 5, 2010 Share Posted January 5, 2010 where is the update cart function? and how is it ran? is it a php function? or a javascript function? Quote Link to comment https://forums.phpfreaks.com/topic/187212-update-page-using-some-form-of-php-function/#findComment-988725 Share on other sites More sharing options...
slaterino Posted January 5, 2010 Author Share Posted January 5, 2010 Okay, that would probably help yes. This is both the 'add' and the 'update function. They both use php. I guess the thing will either to have the 'update' function load when the page loads or maybe to change the add function but I was hoping to find a quick solution to this, which for me would be just to have that 'update' function load when the page does. Would I be able to simply place the 'update' function at the top of the page so that it will always run when the page loads. Is this possible or bad practice? switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; } break; Quote Link to comment https://forums.phpfreaks.com/topic/187212-update-page-using-some-form-of-php-function/#findComment-988885 Share on other sites More sharing options...
Catfish Posted January 5, 2010 Share Posted January 5, 2010 if you want it to run on page load just add a line to call it. can't see how that might be a bad thing to do. Quote Link to comment https://forums.phpfreaks.com/topic/187212-update-page-using-some-form-of-php-function/#findComment-989021 Share on other sites More sharing options...
ignace Posted January 5, 2010 Share Posted January 5, 2010 I don't like your approach honestly for example if I need 5 I'll take a pack of 5. If I need 10 I'll take a pack of 10. When I need 15 I'll... go somewhere else?? Create separate products for each and allow me (the customer) to select how many I want. In the example a pack of 10 and 5 (or 3 packs of 5). If I need 100 I'll add the pack of 20 and update my cart to 5. A customer will not buy 20 just because you don't support 15 and certainly not if your competitor is more flexible than you. Quote Link to comment https://forums.phpfreaks.com/topic/187212-update-page-using-some-form-of-php-function/#findComment-989070 Share on other sites More sharing options...
slaterino Posted January 5, 2010 Author Share Posted January 5, 2010 Okay, I got it sorted in the end. Had to try a different approach though but got it sorted. Thanks for all your help! And ignace, I'm not really sure when this post became about business ethics and quite why you felt the need to bring that into it! But anyway, thanks to everyone else! Quote Link to comment https://forums.phpfreaks.com/topic/187212-update-page-using-some-form-of-php-function/#findComment-989203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.