Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Array help

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Thanked 2 Times in 2 Posts

    Array help

    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
    PHP Code:
    <input type='hidden' name='block2[]' value='$id' /> 
    input type='image' value='Block' src='delete.png'  title='Block'
    goes here to be processed

    PHP Code:
    $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]'"); 
    $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!

  2. #2
    Regular Coder
    Join Date
    May 2007
    Thanked 14 Times in 14 Posts
    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" />


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts