[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Remove values from an array

Posted on 2011-10-10
19
Medium Priority
?
293 Views
Last Modified: 2012-05-12
Along the same lines of the code below:

 
<?php 
if(isset($_POST['submit'])) 
{
  $_SESSION['names'][] = $_POST['name'];
}
?>

<?php 
foreach ($_SESSION['names'] as $name) 
{
  echo $name .'<br>';
}
?>

Open in new window


Once listed, I'd like to add a repeated 'delete' link that would remove that chosen value from the array.  What I am trying to build is a user submission form that shows what they entered, then once the user is done, they can choose to submit all the values to a database.
0
Comment
Question by:axessJosh
  • 10
  • 7
  • 2
19 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36943279
How is the item selected for deletion?

Because of how arrays are made in PHP, a simple unset() on the variable might get it done.  I don't know if this approach is safe within a foreach loop.  I can discuss that issue if necessary.  (I suspect it's safe but am not sure.  I can propose a safe approach but it depends on my question above.)

I have 2 other ideas if the above doesn't work for you.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36943292
I hadn;t really thought the process through, I was hoping to be able to add a delete link in the loop that would show for each element.  I wanted to use it like a repeat region out of a database and add that element ID to the Session value?
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36943344
Anyway, if you can determine that the element you want to delete is something like $_Session [ 'Joseph' ] then I'd try   unset ( $_SESSION [ 'Joseph' ] );  I think that would work and I hope PHP would do adequate garbage collection.

I can't tell if that meets your needs or not.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:axessJosh
ID: 36943369
yea, i think it is setting each name as a $_SESSION variable.  what would the code look like to delete that?

 
<?php
function removeName() 
{
    unset($_SESSION['name']);
}

Open in new window


How would i set the delete link?

echo $name ."delete here" . "<br>";
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36943370
Current file:

<?php 
if(isset($_POST['submit'])) 
{
  $_SESSION['names'][] = $_POST['name'];
}
?>

<?php 
foreach ($_SESSION['names'] as $k => v ) 
{
  echo $v .' <a href="rem.php?id='. $k .'">Remove</a><br>';
}
?>

Open in new window


rem.php:
<?php 
if(isset($_GET['id'])) 
{
  echo 'Removing: '. $-SESSION['names'][$_GET['id']] .'<br>';
  unset($_SESSION['names'][$_GET['id']]);
}


foreach ($_SESSION['names'] as $k => v ) 
{
  echo $v .' <a href="rem.php?id='. $k .'">Remove</a><br>';
}
?>

Open in new window


Or you could build it into the same file as such:
<?php 
if(isset($_POST['submit'])) 
{
  $_SESSION['names'][] = $_POST['name'];
}
if(isset($_GET['id']))
{
  echo 'Removing: '. $_SESSION['names'][$_GET['id']] .'<br>';
  unset($_SESSION['names'][$_GET['id']]);
}


foreach ($_SESSION['names'] as $k => v ) 
{
  echo $v .' <a href="'.$_SERVER['PHP-SELF'].'?id='. $k .'">Remove</a><br>';
}
?>

Open in new window

0
 
LVL 2

Author Comment

by:axessJosh
ID: 36943453
the third set gave me the following error:

Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in C:\wamp\www\axessDB\newDB\contributions\index.php on line 154
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36943484
I fixed it, the v needed to be set as a $V.

however, everything works fine until i begin to delete.  Once ive deleted, i can no longer add new names.  The URL goes to ?ID=interger which I am sure i causing the issue.
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36943551
that's cuz i'm passing the id (or array index) as a get parameter within the url. I don't see anything in there that shows a form for adding names - so I don't know how to fix what I can't see.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36943559
sorry, here is the full deal that i have so far.  my bad.

 
<fieldset><legend>Enter Contributor</legend>

<form action="" method="post">
<input type="input" name="name"> Name <input name="submit" type="submit">
</form>
</fieldset>

<?php if(isset($_POST['submit'])) {
 $_SESSION['names'][] = $_POST['name'];
}
if(isset($_GET['id']))
{
  echo 'Removing: '. $_SESSION['names'][$_GET['id']] .'<br>';
  unset($_SESSION['names'][$_GET['id']]);
}

foreach ($_SESSION['names'] as $k => $v ) 
{
  echo $v .' <a href="'.$_SERVER['PHP-SELF'].'?id='. $k .'">Remove</a><br>';
}
?>

Open in new window

0
 
LVL 10

Accepted Solution

by:
Derokorian earned 1000 total points
ID: 36943581
Note I've change the action of the form to $_SERVER['PHP_SELF'] this will remove the query string (everything in the url after the question mark ?) when the form is submitted.

<fieldset><legend>Enter Contributor</legend>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="input" name="name"> Name <input name="submit" type="submit">
</form>
</fieldset>

<?php 
if(isset($_POST['submit'])) {
 $_SESSION['names'][] = $_POST['name'];
}
if(isset($_GET['id']))
{
  echo 'Removing: '. $_SESSION['names'][$_GET['id']] .'<br>';
  unset($_SESSION['names'][$_GET['id']]);
}

foreach ($_SESSION['names'] as $k => $v ) 
{
  echo $v .' <a href="'.$_SERVER['PHP-SELF'].'?id='. $k .'">Remove</a><br>';
}
?>

Open in new window

0
 
LVL 2

Author Closing Comment

by:axessJosh
ID: 36943616
Very Cool!
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36943694
What if I wanted to add more form input fields to show for the same ID?  Where would I add that?
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36949237
>> What if I wanted to add more form input fields to show for the same ID?  Where would I add that?

Not sure what you're asking, but if you want to put more names in at once before hitting submit you could try this:

<fieldset><legend>Enter Contributor</legend>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Names <input type="input" name="name[]"><br>
<input type="input" name="name[]"><br>
<input type="input" name="name[]"><br>
<input type="input" name="name[]"><br>
<input type="input" name="name[]"><br>
<input name="submit" type="submit">
</form>
</fieldset>

<?php 
if(isset($_POST['submit'])) {
  foreach( $_POST['name'] as $name )
  {
    $name = trim($name);
    if( empty($name) ) continue;
    $_SESSION['names'][] = $name;
  }
}
if(isset($_GET['id']))
{
  echo 'Removing: '. $_SESSION['names'][$_GET['id']] .'<br>';
  unset($_SESSION['names'][$_GET['id']]);
}

foreach ($_SESSION['names'] as $k => $v ) 
{
  echo $v .' <a href="'.$_SERVER['PHP-SELF'].'?id='. $k .'">Remove</a><br>';
}
?>

Open in new window


you could even get fancy and use javascript to add more fields with a button so that there is unlimited inputs. I'm not great with javascript so you'd probably want to post that as another question in the javascript section :P
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36949268
thanks.

What I'm wanting is to add more info to each name.  something like:

<input type="input" name="amount">Amount Given
etc.

other input fields of differnet information for each person.
make sense?
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36949447
Ah yeah ok. Big change to the code then. Try this out, see if its what you're looking for:

<fieldset><legend>Enter Contributor</legend>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type="input" name="name"><br>
Amount Given <input type="input" name="amt_given"><br>
Age <input type="input" name="age"><br>
<input name="submit" type="submit">
</form>
</fieldset>

<?php 
if(isset($_POST['submit'])) {
	$_SESSION['donor'][] = array('name' => $_POST['name']
										 ,'age' => $_POST['age']
										 ,'amount' => $_POST['amt_given']
										 );
}
if(isset($_GET['id']))
{
  echo 'Removing: '. $_SESSION['donor'][$_GET['id']]['name'] .'<br>';
  unset($_SESSION['donor'][$_GET['id']]);
}

foreach ($_SESSION['donor'] as $k => $v ) 
{
  echo $v['name'] .' ('. $v['age'] .') - $'. $v['amount'] .' <a href="'.$_SERVER['PHP-SELF'].'?id='. $k .'">Remove</a><br>';
}
?>

Open in new window


HTH - Out of office rest of the day, will get back to you tomorrow to answer more questions. Also note this doesn't have any input validation - which you should definitely have! Good Luck!
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36950800
Hi,
Got done with my meetings early, thought i'd throw this out there as something a little more flexible for you:

<?php
   session_start();
   // List the items you want to collect data on
   $fields = array('name','amount_given','age','date_given');
?>
<fieldset><legend>Enter Contributor</legend>
	
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
   foreach( $fields as $field )
   {
      echo ucwords(str_replace('_',' ',$field)) .' <input type="text" name="'. $field .'"><br>';
   }
?>
<input name="submit" type="submit">
</form>
</fieldset>

<?php 
if(isset($_POST['submit']))
{
   $new_donor = array();
   foreach( $fields as $field )
   {
      $new_donor[$field] = isset($_POST[$field]) && !empty($_POST[$field]) ? $_POST[$field] : 'N/A';
   }
   $_SESSION['donor'][] = $new_donor;
}
if(isset($_GET['id']))
{
   echo 'Removing: '. $_SESSION['donor'][$_GET['id']]['name'] .'<br>';
   unset($_SESSION['donor'][$_GET['id']]);
}

if( isset($_SESSION['donor']) )
{
   foreach ($_SESSION['donor'] as $k => $v ) 
   {
      echo $v['name'] .' ('. $v['age'] .') - $'. $v['amount_given'] .' on '. $v['date_given'] .' <a href="'.$_SERVER['PHP_SELF'].'?id='. $k .'">Remove</a><br>';
   }
}
?>

Open in new window

0
 
LVL 2

Author Comment

by:axessJosh
ID: 36965292
I used the first set of code because i understood it better and could see how to input to my DB.

i do get the following error:

Notice: Undefined index: PHP-SELF in C:\wamp\www\axessDB\newDB\contribution_manage.php on line 190

 is that just a localhost issue?
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36965451
Also, If i remove a person, the statement 'removed' shows up.  When i add another person, that statement still shows with no value.  How can I unset the $_GET['id'] to remove that?
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36969792
i do get the following error:

Notice: Undefined index: PHP-SELF in C:\wamp\www\axessDB\newDB\contribution_manage.php on line 190

That's a typo... should be PHP_SELF (note underscore not dash). This should also fix the second problem.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question