Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 28th, 2004, 06:46 PM
Registered User
Join Date: Jul 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unhappy student lost in arrary and cart with sessi

I created a simple cart that uses arrays to store products and prices

$products=array('IBM Laptop', 'Dell Laptop', 'Gateway Desktop', 'Service Contract');
$prices=array(200.00, 300.00, 400.00, 199.99);

I listed the items in a table and then I tried to add a link that deletes that specific tiem in the cart array called


the link that deletes the item syntax is

echo "<td><a href=" . $_SERVER['PHP_SELF'] . "?delete=" .$_SESSION['cart'][$n]. ">Delete</a></td>";

and this is the part of the script that is suppose to delete the item

f ($_GET['delete']) {

it does no delete the item and the price. somtimes it deletes the item and the prices and sometime it just deletes the item not the price.

I need to understand why this is happening and how can I fix the issue. This is the first part of my final and I have to present it on Friday. I am a newbie and My head is going crazy with this bug . it's been 24hours on four hours sleep trying to solve this issue.

here is a link to the cart.


this is the entire code:

if ($_GET['delete']) {

if ($_GET['delete_id']) {

$products=array('IBM Laptop', 'Dell Laptop', 'Gateway Desktop', 'Service Contract');
$prices=array(200.00, 300.00, 400.00, 199.99);

echo "<table cellspacing=20 >";
echo "<tr><th>Item Title</th><TH>Price</th></tr>";

echo "<tr>";
echo "<td>".$products[$_SESSION['cart'][$n]]."</td>";

echo "<td>".number_format($prices[$_SESSION['cart'][$n]],2)."</td>";

echo "<td><a href=" . $_SERVER['PHP_SELF'] . "?delete=" .$_SESSION['cart'][$n]. ">Delete</a></td>";

echo "</tr>";
$total = $total + $prices[$_SESSION['cart'][$n]];

echo "<td><b>TOTAL</td></b>";
echo "<td>".number_format($total,2)."</td>";
echo "<td><a href=store.php>Continue Shopping</a></td>";
echo "</table>";


Reply With Quote
  #2 (permalink)  
Old July 29th, 2004, 09:56 PM
Registered User
Join Date: Jul 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to eleckyt


Not sure if you're still struggling with the below, but perhaps I can help. There are a couple of problems:

1) You're using 'unset' on your array which means you're effectively making NULL the element of your cart array which the user wants to delete. What if the user have 10 items, and they delete the 5th? The 5th slot in your array is going to be NULL. This will cause you problems - you don't want empty slots! To get around this, once you've 'unset' the key in question, copy your array (with holes) into a new array using a 'for' loop, ignoring any 'null'-value items in the process. This will give you a new, clean array with no holes in it.

Something like:

$arMyNewArray = Array();
for ($i=0; $i<=sizeof($_SESSION['cart'])-1; $i++) {
 if ($_SESSION['cart'][$i]) {
  $arMyNewArray[] = $_SESSION['cart'][$i];
$_SESSION['cart'] = $arMyNewArray;

2) If you fix the first problem, this won't really matter, but using 'count' is going to cause you problems too. Consider using 'sizeof', which will give you the true size of an array. The problem before was that 'count' was telling you there were 10 items in the array, even if array positions 1 thru 8 were all null, and only 0 and 9 contained 'real items'. This was compounding the problem above. Use 'sizeof', and remember that arrays start counting at 0, not one. :)

Hope this helps. Good luck with your presentation.


Reply With Quote
  #3 (permalink)  
Old July 30th, 2004, 05:30 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

I don't know that I would copy the array to a new one because it has a numbered indice missing. Without really commenting on the original code. I think that approach would apply unnecessary overhead. I would avoid problems with non precision array offset numbering by always using a foreach loop to access the data instead of counters whereas both the indice and the value are always in sync.

Also, the use of unset destroys the variable, so it isn't technically NULL, it no longer exists which is a step beyond null ;).

A problem that I can see with the example that you've provided is that you haven't built in any error handling or authentication of variables. The best way to approach an application is to always anticipate something going wrong with its design. I don't have time at the moment to go through your code with a fine tooth comb, but right away I think you need more error handling. For example...

if (isset($_GET['delete']) && !empty($_GET['delete'])) {
    if (isset($_SESSION['cart'][$_GET['delete']])) {
    } else {
         echo 'Error: '.$_GET['delete'].' does not exist! Delete failed!';

Test the values of your variables wherever possible for what you expect them to contain. Set error_reporting to E_ALL. Output the values of variables to determine why they are behaving the way that they are.



The Spicy Peanut Project
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help a student re: Re-using variables random_eh C++ Programming 3 February 27th, 2006 08:47 AM
Trigger Unhappy reyboy SQL Server 2000 4 June 18th, 2005 04:20 AM
*student* in need of help satinder Access 2 April 4th, 2005 06:32 AM
* student * in need of help satinder Classic ASP Databases 1 April 2nd, 2005 08:34 AM

All times are GMT -4. The time now is 08:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.