View Full Version : Array help

02-01-2012, 08:47 PM
I have an array the inputs the user name in a table, however it loops thru all the names and inputs each one into the table. I need help figuring out how to do just the one selected.

I have a list of names which is displayed after it loops thru an array, this list can be endless


With each name is a link you click to "block" that person from appearing on the list again

Code for that

<input type='hidden' name='block2[]' value='$id' />
<input type='image' value='Block' src='delete.png' title='Block'>

goes here to be processed

$my_array2 = $_POST['block2'];

$totalIDs = count($my_array2);
for ( $i=0; $i < $totalIDs; $i++ ) {
$sql2 = mysql_query("SELECT sender FROM nudges WHERE id='$my_array2[$i]'");
while($r = mysql_fetch_array($sql2)) {

$query2 = ("INSERT INTO blockUser VALUES(NULL,'$clientID','$sender')");
$result2 = mysql_query($query2) or die(mysql_error());

} // END while
} // END for
} // END if($_POST['block2'])

However it loops thru every name on the page and inputs that into the db, I know that is does this due to the loop.

My problem is I cannot figure out how to code it to only do the 1 name, you will only be able to click on 1 name at a time.

Please help!

02-01-2012, 09:39 PM
I think the problem is that by having this <input type='hidden' name='block2[]' value='$id' /> in the form for ever user when the form is submitted every single id is submitted, not just the one which is clicked.

My suggestion is to use checkboxs to select a name and then a normal submit button. This way when the form is submitted only the users which were checked will have their user ids sent to the script which handels the database action.

<form action="?" method="post">


$users = array(56 => 'Ken', 84 => 'Jody', 125 => 'Stan');

foreach ($users as $id => $name) { ?><input type="checkbox" name="block2[]" value="<?=$id?>" /> <?=htmlSpecialChars($name)?><br /><? }


<input type="submit" value="Submit" />