Array getting mixed up, test for me

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
totally_forgot
Forum Newbie
Posts: 5
Joined: Sat Mar 26, 2005 3:56 pm

Array getting mixed up, test for me

Post 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");

	
?>
totally_forgot
Forum Newbie
Posts: 5
Joined: Sat Mar 26, 2005 3:56 pm

Post by totally_forgot »

oh and you can test here; http://forumz.wuggawoo.co.uk/index/test

if you cant be bothered ;p
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
totally_forgot
Forum Newbie
Posts: 5
Joined: Sat Mar 26, 2005 3:56 pm

Post by totally_forgot »

hmm...

So theres no way to just tighten up the array somehow?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try my suggestion.. it does that.. in less space.
totally_forgot
Forum Newbie
Posts: 5
Joined: Sat Mar 26, 2005 3:56 pm

Post by totally_forgot »

You are suggesting that for a checked value, its the id? (10 or 5 for example)

hmm
Post Reply