473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

unset() is not unsetting!

Hi,
I have a cart, setup as an associative array (itemid=>qty). When the
cart is displayed, the quantity field is an input box and the value can
be changed to add/remove an item:
echo '<input type = "text" name = "'.$itemid.'" value = "'.$qty.'" size
= "3">';

when the "Save changes" button on the page that displays thecart is
pressed, a hidden field called "save" is set and the form is submitted.

session_start();
if(isset($_POST['save']))
{
foreach ($_SESSION['cart'] as $itemid => $qty)
{
$new_qty = $_POST[$optionid]; // I used (int)
$_POST[$optionid], didn't help
if($new_qty == 0) // Removing item?
{
// Search the cart for items dependent on the item being removed
if (!($result = check_required($optionid,
$_SESSION['builderid']))) { // this function returns FALSE
unset($_SESSION['cart'][$optionid]);
unset($_SESSION['optionid']);
}
.....
}
}
This things seems to go into an infinite loop. It unsets, say item 4000,
then goes back and tries to remove it again, and again. I used
error_log() function to see what is going on and it seems that the value
of the cart['itemid'] is unset but for some reason it is still there;
that particualr item keeps showing up.

Thanks for your help.

Jul 17 '05 #1
2 2021
"Xerxes" <as*******@hotmail.com> wrote in message
news:21******************************@news.teranew s.com...
Hi,
I have a cart, setup as an associative array (itemid=>qty). When the
cart is displayed, the quantity field is an input box and the value can be changed to add/remove an item:
echo '<input type = "text" name = "'.$itemid.'" value = "'.$qty.'" size = "3">';

when the "Save changes" button on the page that displays thecart is
pressed, a hidden field called "save" is set and the form is submitted.
session_start();
if(isset($_POST['save']))
{
foreach ($_SESSION['cart'] as $itemid => $qty)
{
$new_qty = $_POST[$optionid]; // I used (int)
$_POST[$optionid], didn't help
if($new_qty == 0) // Removing item?
{
// Search the cart for items dependent on the item being removed if (!($result = check_required($optionid,
$_SESSION['builderid']))) { // this function returns FALSE
unset($_SESSION['cart'][$optionid]);
unset($_SESSION['optionid']);
}
....
}
}
This things seems to go into an infinite loop. It unsets, say item 4000, then goes back and tries to remove it again, and again. I used
error_log() function to see what is going on and it seems that the value of the cart['itemid'] is unset but for some reason it is still there;
that particualr item keeps showing up.

Thanks for your help.


Never mind about this issue (or non-issue), it was caused by a stupid
logic problem!
Jul 17 '05 #2
Xerxes wrote:
Hi,
I have a cart, setup as an associative array (itemid=>qty). When the
cart is displayed, the quantity field is an input box and the value can
be changed to add/remove an item:
echo '<input type = "text" name = "'.$itemid.'" value = "'.$qty.'" size
= "3">';

when the "Save changes" button on the page that displays thecart is
pressed, a hidden field called "save" is set and the form is submitted.

turn on error reporting:

error_reporting(E_ALL);
ini_set(diplay_errors', '1');
session_start();
if(isset($_POST['save']))
{
foreach ($_SESSION['cart'] as $itemid => $qty)
{
$new_qty = $_POST[$optionid]; // I used (int) $_POST[$optionid], didn't help
What is $optionid ?
if($new_qty == 0) // Removing item?
{
// Search the cart for items dependent on the item being removed
if (!($result = check_required($optionid,
$_SESSION['builderid']))) { // this function returns FALSE
unset($_SESSION['cart'][$optionid]);
Changing the array that controls the loop inside the loop ?
I don't like it, but guess it's ok.
unset($_SESSION['optionid']);
}
....
}
}
This things seems to go into an infinite loop. It unsets, say item 4000,
then goes back and tries to remove it again, and again. I used
error_log() function to see what is going on and it seems that the value
of the cart['itemid'] is unset but for some reason it is still there;
that particualr item keeps showing up.


Perhaps you're mixing $optionid with 'optionid'
or $optionid with $itemid

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
Given a bunch of mixes variables, I'm in a situation where I can't know what type they are. Can I use unset to kill an object? unset($object); Can I use unset to kill a pointer to an open...
3
by: Phil Powell | last post by:
PHP: unset($_SESSION); In my original environment (PHP 4.3.2) this line will delete the session variable 'mainDisplay'. But in the testing environment (PHP 4.3.6) the variable persists even...
21
by: steve | last post by:
Dont’ make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the...
1
by: frizzle | last post by:
Hi group, Why won't $new_var be unset in the following function? Am i missing out something? Greetings Frizzle. *************************************
7
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
2
by: Iain Adams | last post by:
Hey i have a loop like so, foreach($users as $user) { foreach($syncWebUsers as $sync) { //user already synched if($sync == $user) {
5
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray)...
4
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the...
0
Airslash
by: Airslash | last post by:
Hello, I've written a small function to delete a variable from a class' internal array. The variables on their own are custom class objects, and I'm a bit confused about the whole pass by...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.