473,395 Members | 1,456 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,395 software developers and data experts.

unset() unsets more than it should from $_SESSION subarray

hey, i'm basically trying to use php sessions and a bit of javascript to make tabbed browsing within a web page using sessions to store the data of the different opened pages.
Problem: when i unset the first 0 index in the array (ie close the default page) it seems to wipe the other one (can't get it to add more that one anymore) from the array also

the information is held in sub array
[PHP]$_SESSION['pages'][/PHP]
each page has its own array of infomation
[PHP]$_SESSION['pages'][<pageindex>]['title' => value, 'link' => value][/PHP]

the menu down the side uses a foreach loop to echo out each of the pages and links in the ['pages'] array using
[PHP]foreach($_SESSION['pages'] as $tid => $page){...}[/PHP]
so that i can get the tab id for the page to unset it from the array
there is a link echoed for each page like so:
[HTML]<a href="menu.php?action=deletetab&tid='.$tid.'" target="menu">[/HTML]

so that when its clicked, we go to the menu page where we have the following code at the top (the menu page is self contained to do all of this)

[PHP]
switch($_REQUEST['tid']){
case "deletetab":
unset($_SESSION['pages'][$_REQUEST['tid']]);
break;
<other code for other actions>
Default:
break;
}
[/PHP]

Note: it did use to work correctly when i use a for loop to itterate through the different tabs but then this would have failed because it would after one deletion, have the wrong array id (tid) number for the pages after the one we deleted and also others wouldn't have been displayed that had a higher id number because the for loop used a count($_SESSION['pages']) function to determine how many there were and how high $i should count in the loop, but if we delete from the middle, there are less even though the id's haven't changed....they wern't all displayed! but this new way seems to delete all the data when the default one is clicked, though if you click to delete the second array object, it still leaves the first intact.

Thanks in advance!
Oct 23 '07 #1
2 2013
Anyone? please?! .
Oct 24 '07 #2
This answer is about 5 years late. However I came across the same problem and found no help online. So here is the solution in case anyone else ends up here.
Your problem is that when you call unset($_SESSION['pages'][$_REQUEST['tid']]);
PHP will unset everything in $_SESSION['pages'], not just the sub-array.
You need to address it like this:
foreach ($_SESSION['pages'] as $key => $value) {
if ($key == $_REQUEST['tid']) unset($_SESSION['pages'][$key]); }
}
You are unsetting only the $key from within the parent array.
Feb 24 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ante Perkovic | last post by:
Hi, everybody First, the code: --------------------- $maxNo = 500 $numbers = range (1,$maxNo); srand ((double)microtime()*1000000); shuffle ($numbers); $subarray = array_slice ($numbers, 0,...
2
by: Xerxes | last post by:
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...
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...
2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
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)...
1
by: Nico | last post by:
Hi all, I've created the following code: <?php session_start(); ?> <FORM METHOD="POST" ACTION="prova.php"> Add <b>Combination</b><br><br>
5
by: avlee | last post by:
Hello I have application in php where users login and logout. I wanted to create function for administrator which will delete all session's data for other users. For example, each user session...
2
by: O.B. | last post by:
I've written a small operation that sets/unsets bits within an unsigned integer. Is there a better way to do this? static uint setBits(uint original, uint newValue, uint offset, unint mask) {...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.