closerwalk Posted October 8, 2007 Share Posted October 8, 2007 Ok here we go... What I have here is three select boxes one for all the tables in the db, one listing the table columns in that table and a third to collect the table columns selected stored in a session called $_SESSION['collections']. Up to this point all that works beautiful the session stores the selected collections. But heres what I want to be able to do: I want to remove single values from the $_SESSION['collections']. How do I do that? Currently it posts the value I want to remove just as it collects the values in the collections select box. But how do I remove that single element from the array? //----------------------DATABASE ABOVE THIS LINE----------------// // Sesssion Start and Session Counter session_start(); $counter = $_SESSION['counter']++; // ----------------------------// //-Get Database Table--// if($_GET['table']) { $tables = $_GET['table']; } else { $tables = 'project'; } $query = 'select * from '. $tables; $result = mysql_query($query, $link); ///---------------// //Store Session Collection Values if($_GET['collections']){ $_SESSION['collections'][$counter] = $_GET['collections']; } $sql1 = "SHOW TABLES FROM $dbname"; $result1 = mysql_query($sql1); //------------------tables------------------------// echo '<table>'; echo '<tr>'; echo '<td valign="top">'; echo '<form method="GET" action="">'; print '<select multiple name="table" onBlur="window.location.href=\'testing.php?table=\' + this.value">'; while ($row = mysql_fetch_array($result1)) { for($i=0;$i<mysql_num_fields($result1);$i++) { } print '<OPTION VALUE='.$row[mysql_field_name($result1, $i)].'>'.$row[mysql_field_name($result1, $i)].''; } echo '</select>'; //-----------------------------------------------// //--------------------tableheadings---------------------------// echo '</td>'; echo '<td valign="top">'; print '<select multiple name ="collections" onBlur="window.location.href=\'testing.php?collections=\' + this.value"" >'; for ($i = 0; $i < mysql_num_fields($result); $i++) { print '<OPTION VALUE='.mysql_field_name($result, $i).'>'.mysql_field_name($result, $i).''; } echo '</select>'; echo '</td>'; //------------------collections------------------// echo '<td valign="top">'; print '<select multiple name="collections" onBlur="window.location.href=\'testing.php?unset=\' + this.value"" >'; foreach($_SESSION['collections'] as $key => $value) { print '<OPTION VALUE='. $value .'>'. $value .''; } echo '</select>'; echo '</form>'; echo '</td>'; echo '</tr>'; echo '</table>'; //-----------unsetting----------------- if($_GET['unset']){ unset($_SESSION['COLLECTIONS'][$unset]); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/ Share on other sites More sharing options...
MmmVomit Posted October 8, 2007 Share Posted October 8, 2007 What you're doing is just fine, but you have to define $unset before you use it. Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/#findComment-364965 Share on other sites More sharing options...
closerwalk Posted October 8, 2007 Author Share Posted October 8, 2007 Can you elaborate: I have tried: $unset = $_GET['unset']; $unset = $_SESSIONS['collections'][$_GET['unset']]; Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/#findComment-364976 Share on other sites More sharing options...
Rithiur Posted October 8, 2007 Share Posted October 8, 2007 Have you tried replacing the unset($_SESSION['COLLECTIONS'][$unset]); with unset($_SESSION['collections'][$_GET['unset']]); The $unset you are trying to use there currently is not defined anywhere, as MmmVomit said. In addition, do note that those array keys are case sensitive. You were trying to use upper case in this particular piece, while rest of the code uses lower case. Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/#findComment-364983 Share on other sites More sharing options...
closerwalk Posted October 8, 2007 Author Share Posted October 8, 2007 Tried it no change unset($_SESSION['collections'][$_GET['unset']]); Theoretically it should work.. or at least I think so unset($_SESSION['collections'][$_GET['unset']]); realy = unset($_SESSION['collections']['value_in_collection']]; seems logical.... but nada! Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/#findComment-364986 Share on other sites More sharing options...
Rithiur Posted October 8, 2007 Share Posted October 8, 2007 Hmm.. perhaps you have error in the following line print '<OPTION VALUE='. $value .'>'. $value .''; Perhaps this should rather be: print '<OPTION VALUE='. $key .'>'. $value .''; If you are using the $value as the value, instead of the $key, then only the value of the session variable is sent to the unset (assuming the script works as I expect it to). However, then you are trying to unset the session variable by the value, rather than the key, which obviously wont work (unless they are the same). If this is not the case, however, I would recommend putting var_dump($_GET); before the unsetting if clause to make sure that the correct variable exists and has value you expect it to have. Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/#findComment-364996 Share on other sites More sharing options...
closerwalk Posted October 8, 2007 Author Share Posted October 8, 2007 Spoke too soon Almost solution. if($_GET['unset']){ unset($_SESSION['collections'][$key]); header('location: http://cesserver5/testing.php'); } It unsets the correct values unless you select the top one first. Quote Link to comment https://forums.phpfreaks.com/topic/72363-solved-removing-a-single-value-from-_session-array/#findComment-364997 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.