harkly Posted February 1, 2012 Share Posted February 1, 2012 I have a list, can be any length, and I need to be able to pull out a specific variable. I had it working in a form but it was nested in another form, just learned that I cannot do that so I need help with how to get what I need done. So say my list consist of 3 names John Jane Betty I click on a link to 'block' John from the list, what is the best way to do that? This is what I had: <form action='' method='POST' name='blockuser' id='blockuser'> <input type='hidden' name='block2' value='$sender' /> <input type='image' value='Block' src='delete.png' title='Block'> </form> if($_POST['block2']){ echo "block $sender "; } I was thinking I need to put it in an array but this isn't working either <input type='hidden' name='block2[]' value='$id' /> <input type='image' value='Block' src='delete.png' title='Block'> $my_array2 = $_POST['block2']; if($_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)) { $sender=$r['sender']; echo "block $sender "; } // END while } // END for } // END if($_POST['block2']) what I get back is block John block Jane block Betty what I need is block John Maybe going with an array is not the way to do it, can someone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/256198-help-with-array/ Share on other sites More sharing options...
Proletarian Posted February 2, 2012 Share Posted February 2, 2012 I'm guessing your FOR loop is just iterating all values in the list, which is why you're seeing all the values. But since I don't know how you're populating your block2[] array with values since it's a hidden type, I can't really tell how the values are getting into the array in the first place. I just know that if you pass an array throw POST, it should contain all the values you put into it from the form and you should be able to iterate through that array with a FOREACH loop to get at those values. Try this and tell me if it works or not (and what happens if it doesn't work). <input type='hidden' name='block2[]' value='$id' /> <input type='image' value='Block' src='delete.png' title='Block'> $data = $_POST['block2']; if($data) { foreach ($data as $id) // changed FOR to FOREACH { $sql2 = mysql_query("SELECT sender FROM nudges WHERE id = $id;"); while($r = mysql_fetch_assoc($sql2)) // changed mysql_fetch_array() to mysql_fetch_assoc { $sender=$r['sender']; echo "block $sender "; } } } Quote Link to comment https://forums.phpfreaks.com/topic/256198-help-with-array/#findComment-1313545 Share on other sites More sharing options...
jcbones Posted February 2, 2012 Share Posted February 2, 2012 You may want to consider using radio buttons, or a link to select users to delete. Quote Link to comment https://forums.phpfreaks.com/topic/256198-help-with-array/#findComment-1313553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.