Removing Single Element From Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
honkyinc
Forum Newbie
Posts: 19
Joined: Tue Jun 04, 2002 10:30 am
Location: Maryland, USA

Removing Single Element From Array

Post by honkyinc »

Howdy all,

I've been trying to figure this out for the past two hours, and still nothing. I have an array with seven items (0-6), and want the user to be able to remove one of the items with a link. Here is the code (without the input form):

Code: Select all

<?php

if($action == "delete") &#123;
	unset($item&#1111;$id]);
&#125;

$color = "#336699";

var_dump($item);

for($counter = 0; $counter < 7; $counter++) &#123;
	$offset = $counter + 1;
	echo "<tr><td width="80%" bgcolor="$color">\n";
	echo "Item " . $offset . ": " . $item&#1111;$counter];
	echo "</td>\n";
	echo "<td width="20%" bgcolor="$color">\n";
	echo "<a href="makelist.php?action=delete&id=$counter" class="link">Delete</a>\n";
	echo "</td></tr>\n\n";

	if($color == "#336699") &#123;
		$color = "#6699CC";
	&#125; else &#123;
		$color = "#336699";
	&#125;
&#125;

?>
Albeit not the cleanest code, it *should* function, according to my understanding. It will print the array into an alternating color table, with a delete link next to each one, that transmits the action (delete) and id of each specific element.

If someone can tell me why the code doesn't work, or modify it so that it does work, I'd be GREATLY appreciative.

Thanks in advance!

Martin
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post by Zmodem »

a couple things:

1) Check your register globals. If you are using PHP 4.2+ you will need to use $_POST['id'] and not just $id in your unset statement

2) Try putting double quotes around $id in your unset() statement.
honkyinc
Forum Newbie
Posts: 19
Joined: Tue Jun 04, 2002 10:30 am
Location: Maryland, USA

Post by honkyinc »

:grumble:

Neither of those worked. I've done examples to prove that unset() works, but whenever I try it this way, it makes the $item array NULL.

I'll keep plugging, but if anyone else wouldn't mind chiming in, it'd be greatly appreciated!

And thanks Z!

EDIT: I've also tried using a constant, i.e.

Code: Select all

unset($item&#1111;1]);
and that doesn't work either.
Last edited by honkyinc on Thu Jun 13, 2002 7:32 am, edited 1 time in total.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

If you are using PHP 4.2+ then you will also have to replace

$action with $_POST['action']
honkyinc
Forum Newbie
Posts: 19
Joined: Tue Jun 04, 2002 10:30 am
Location: Maryland, USA

Post by honkyinc »

PHP is recognizing that I'm performing an action, otherwise it wouldn't alter the array at all. All I know is this is confusing the hell outta me, which as a progammer I'm used to.

I could make a workaround for it, but I shouldn't have to for something you all tell me SHOULD work. Back to the drawing board.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Is the array set in the script or are you passing it to the page? if so how are you passing it?

Check that the array is set and has values in it before trying to delete values.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

You are not passing the array when the page calls itself.

When the page is created it creates an array, you click the delete link which is supposed to delete that item, but when you call your page again there is no array to delete from hence the NULL output, $item isn't an object.

HTML is stateless, i.e. it doesn't know from one page to the next that it is supposed to be linked, in the same transaction, or whatever. The only way to do this is to pass/store data, either with forms (POST), in the URL (GET), stored in a database referenced by cookies, sessions etc.

Mike
Post Reply