Jump to content

Array Help


Canman2005

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.