Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2012, 08:47 PM   PM User | #1
harkly
Regular Coder

 
Join Date: Jun 2010
Location: Earth
Posts: 293
Thanks: 26
Thanked 2 Times in 2 Posts
harkly is an unknown quantity at this point
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

John
Jen
Betty

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']; 

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']; 

            
$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!
harkly is offline   Reply With Quote
Old 02-01-2012, 09:39 PM   PM User | #2
>ssp-cdr<
Regular Coder

 
Join Date: May 2007
Posts: 100
Thanks: 16
Thanked 11 Times in 11 Posts
>ssp-cdr< is an unknown quantity at this point
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.

Code:
<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" />

</form>
>ssp-cdr< is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 PM.


Advertisement
Log in to turn off these ads.