Page 1 of 1

Array getting mixed up, test for me

Posted: Sat Apr 16, 2005 7:20 pm
by totally_forgot
Basically I have a problem with this code, it works perfectly

However as soon as I click to delete or email just number 10, without number 5 ticked, on the post page it shows that for some reason number 5 is now the option!

Also I cant get the last row of the array (the update button itself) to stop interering.

Heres the code, test and you'll see what I mean.

Code: Select all

<?php 

if(!$_POST['update'])
{ ?> <form action="test" name="caf" method="post">
<table width="40%" cellspacing="1" cellpadding="4" class="table">
    <tr> 
      <td class="headbars">-</td>
      <td width="5%" class="headbar2">Email</td>
      <td width="4%" class="headbar2" align="center">Delete</td>
    </tr><tr><td class="cell">5</td>

<td width="5%" class="cell"><input name="email[]" type="checkbox" value="1" checked="checked" />1</td>
      <td width="4%" class="cell" align="center">
	    <input name="delete[]" type="checkbox" id="delete[]" value="1" />
	    <input type="hidden" name="tid[]" value="5" /></td>
    </tr><tr><td class="cell">10</td>
     
	  <td width="5%" class="cell">
	      <input name="email[]" type="checkbox" value="1" checked="checked" />1 </div></td>
      <td width="4%" class="cell" align="center">
	    <input name="delete[]" type="checkbox" id="delete[]" value="1" />
	    <input type="hidden" name="tid[]" value="10" />

</td></tr></table>
<input name="update" class="button" type="submit" value="Update Subscriptions" /></form><? }
	
	else
	
	{
	$tid = $_POST['tid']; 
	$email = $_POST['email'];
	$delete = $_POST['delete'];
	$while = (count($_POST['tid']));
	
	
	print_r($_POST); echo '<br /><br />';
while($while >= 0) {
$while--; $a++;
$tidu = $tid["$while"];
	#$email[$while] = ($email[$while] == 1) ? 1 : 0;
	#$delete[$while] = ($delete[$while] == 1) ? 1 : 0;

	if($delete[$while] == 1)
#{query("DELETE FROM $forumzDB.forumz_subscribe WHERE tid = '$tid[$while]' AND uid = '$uid'");} 
	{echo "delete thread $tidu, $delete[$while]<br />";}
	
	elseif($tidu != 1 || $tidu == '')
	{echo "updated $tidu, set email to $email[$while]<br />";}
#query("UPDATE $forumzDB.forumz_subscribe SET email = '$email[$while]' WHERE tid = '$tidu' AND uid = '$uid'");}
}#end while
}#end if

#success_box('Subscriptions updated.', "$HTTP_REFERER");
#header("Location: ".$forum_properties['base_url']."/edit_subscriptions");

	
?>

Posted: Sat Apr 16, 2005 7:30 pm
by totally_forgot
oh and you can test here; http://forumz.wuggawoo.co.uk/index/test

if you cant be bothered ;p

Posted: Sun Apr 17, 2005 7:04 am
by feyd
the problem comes down to your usage of hidden fields and checkboxes. When a checkbox is checked, it's sent with the form submission, otherwise, it's not. While hidden fields are always sent.

My suggestion: place an actual index value in each field to give them specific locations in the array. In reality, you could also lose the hidden fields and simply place the value that they would send as the value of the checkbox.

Posted: Sun Apr 17, 2005 9:12 am
by totally_forgot
hmm...

So theres no way to just tighten up the array somehow?

Posted: Sun Apr 17, 2005 9:27 am
by feyd
try my suggestion.. it does that.. in less space.

Posted: Sun Apr 17, 2005 11:56 am
by totally_forgot
You are suggesting that for a checked value, its the id? (10 or 5 for example)

hmm