Canman2005 Posted March 24, 2011 Share Posted March 24, 2011 Hi I have a HTML form which posts to a PHP script. On the PHP script I have $_SESSION['mystuff'][] = array('idnumber'=>''.$_SESSION['myID'].'','titlename'=>''.$_POST['productname'].''); and basically this array builds up each time the form is submitted I then output this data using $num=0; foreach($_SESSION['mystuff'] as $mystuffvals) { print $_SESSION['mystuff'][$num]['titlename']; print "<br>" $num = $num+1; } this all works fine, but I want to be able to add a "delete" link next to each one so that it can be removed. how possible is that? I did try adding <a href="?remove=<?php print $num; ?> under the bit of code print $_SESSION['mystuff'][$num]['titlename']; and then running something at the top of the page which looks like if(isset($_GET['remove'])) { unset($_SESSION['mystuff'][$_GET['remove']]); } ?> but that seems to unset random arrays and I can't think what could be wrong. Please help Thanks J Quote Link to comment https://forums.phpfreaks.com/topic/231599-array-help/ Share on other sites More sharing options...
.josh Posted March 24, 2011 Share Posted March 24, 2011 are you putting the delete link in the foreach loop? before or after the $num = $num + 1? (it should be in the loop, before the increment). Quote Link to comment https://forums.phpfreaks.com/topic/231599-array-help/#findComment-1191750 Share on other sites More sharing options...
Canman2005 Posted March 24, 2011 Author Share Posted March 24, 2011 yep, it's adding before Quote Link to comment https://forums.phpfreaks.com/topic/231599-array-help/#findComment-1191751 Share on other sites More sharing options...
linus72982 Posted March 24, 2011 Share Posted March 24, 2011 First things first, I'm not sure about the rest of your script, but it looks like you don't need that many dimensions to your array, just make an associative array with idnumber as the key and then titlename as the value, like this: $_SESSION['mystuff][$_SESSION['myID']] = $_POST['productname'] That way you can get two pieces of information per array element and not go one dimension deeper. So, then, you can also change up your foreach with the alternate method and actually use the data you reference to variables in the foreach to cut down your code, like this: foreach($_SESSION['mystuff'] as $mystuffkeys => $mystuffvals) print $mystuffvals.'<a href="?remove='.$mystuffkeys.'">Remove This Product</a>'; (no, you don't need curly brackets if it is just one line, the program will know that without brackets just to read the next line only and loop.) Okay, so then, you can use this at the top of your page: if (isset($_GET['remove'])) unset($_SESSION['mystuff'][$_GET['remove']); I think that should work. If it doesn't, let me know what is going on or any errors you have and we'll work through it. I haven't tested the code above so there may be a syntax error or something. Quote Link to comment https://forums.phpfreaks.com/topic/231599-array-help/#findComment-1191760 Share on other sites More sharing options...
Canman2005 Posted March 24, 2011 Author Share Posted March 24, 2011 thanks all, managed to crack it, stupid mistake I made Quote Link to comment https://forums.phpfreaks.com/topic/231599-array-help/#findComment-1191782 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.