Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 28th, 2004, 06:46 PM
Registered User
 
Join Date: Jul 2004
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

$_SESSION['cart']

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']) {
unset($_SESSION['cart'][$_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.

http://nucitytech.com/store.php

this is the entire code:


<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if ($_GET['delete']) {
unset($_SESSION['cart'][$_GET['delete']]);
}

if ($_GET['delete_id']) {
unset($_SESSION['cart'][$_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>";

for($n=0;$n<count($_SESSION['cart']);$n++){
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>";

?>:(

 
Old July 29th, 2004, 09:56 PM
Registered User
 
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to eleckyt
Default

Hey,

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];
 };
};
unset($_SESSION['cart']);
$_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.

Ed



 
Old July 30th, 2004, 05:30 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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']])) {
        unset($_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.

HTH!



Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::





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





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