Page 1 of 1

Removing Single Element From Array

Posted: Wed Jun 12, 2002 3:23 pm
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

Posted: Wed Jun 12, 2002 3:51 pm
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.

Posted: Thu Jun 13, 2002 7:14 am
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.

Posted: Thu Jun 13, 2002 7:23 am
by Wayne
If you are using PHP 4.2+ then you will also have to replace

$action with $_POST['action']

Posted: Thu Jun 13, 2002 7:34 am
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.

Posted: Thu Jun 13, 2002 10:13 am
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.

Posted: Thu Jun 13, 2002 12:36 pm
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