...

View Full Version : PHP $_POST arrays with while loop



Joemoemofo
03-18-2010, 02:55 AM
Okay, here's what is going on...

I have a php form that grabs information such as users from my mysql database and I am able to edit multiple users' information in one page. Lets say I have 5 users with three textboxes each and only one submit button at the bottom. How would I go about $_POST'ing and updating my mysql tables for the information I have inputed. I will have a hidden input value of the user's id. I understand this:


<input name="username[]" value="<?=$r['user_name'] ?>" />

I have the array setting in the input, just do not know where to go from there. I'm sort of a PHP noob but of course I am a fast learner and understand the lingo. "THX" for those who help!

JoeMoe

MattF
03-18-2010, 03:22 AM
1) Don't use short tags. This:



<input name="username[]" value="<?=$r['user_name'] ?>" />


ought be:



<input name="username[]" value="<?php echo $r['user_name'] ?>" />


You'd be better off setting each name with the userid or username as the key, i.e:



<input name="username[<?php echo $r['userid'].']" value="'.$r['user_name'] ?>" />


And then you can loop through, using a foreach and assign correctly. If you use [], each key will just be an incremented number. Using the above, for example, you would use:



foreach ($_POST['username'] as $key => $val)
{
[$key is your userid and $val is the value]
}

Len Whistler
03-18-2010, 03:32 AM
You could combine all data into one array using the same input name


Untested, might be syntax errors

$textbox = $_POST[username];
$user_id = $_POST[user_id];

foreach ($textbox as $value) {
$username = $value[0]
$textbox_one = $value[1]
$textbox_two = $value[2]
$textbox_three = $value[3]
}

mysql_query("INSERT INTO table (username, one, two, three)
VALUES ('$username', '$textbox_one', '$textbox_two', '$textbox_three')
WHERE user_id = $user_id ");


<input name="username[]" value="<?=$r['user_name'] ?>" />
<input name="username[]" value="" />
<input name="username[]" value="" />
<input name="username[]" value="" />



----------

Joemoemofo
03-18-2010, 04:06 AM
@MattF, your code posted is perfect! Len, I didn't get a chance to try yours as Matt's is what I wanted. One thing, how would I go about a single checkbox with this array of material? Let's say if I wanted to make a user have the privilege to make comments on certain material I post on my website. The single checkbox is as follows:


<input type="checkbox" class="boxed" value="1" name="user_comments[<?php echo $r['user_id'] ?>]" <? if($r['user_comments'] == "0") { echo ' '; } else { echo 'checked'; } ?> />

The for each right now is:

foreach ($_POST['user_comments'] as $key => $val)
{
if(empty($val)) {
$val2 = "0";
mysql_query("UPDATE users SET user_comments='$val2' WHERE user_id='$key'");
} else {
mysql_query("UPDATE users SET user_comments='$val' WHERE user_id='$key'");
} }

MattF
03-18-2010, 09:32 AM
One thing, how would I go about a single checkbox with this array of material? Let's say if I wanted to make a user have the privilege to make comments on certain material I post on my website.

One checkbox regardless of users? It shouldn't require an array if so, but the code below should suffice for the array method. Untested, btw.


<input type="checkbox" class="boxed" value="1" name="user_comments[<?php echo $r['user_id'].']"'.(($r['user_comments'] == "0") ? ' checked' : ''); ?> />




if (isset($_POST['user_comments']) && !empty($_POST['user_comments']))
{
foreach ($_POST['user_comments'] as $key => $val)
{
if (!empty($val) && intval($key))
{
mysql_query('UPDATE users SET user_comments=\''.mysql_real_escape_string($val).'\' WHERE user_id='.intval($key));
}
}
}

Joemoemofo
03-18-2010, 07:33 PM
The code didn't work. It's alright, I just chose to drop the "FancyForm" style of my checkbox and went with a drop-down box. Thanks for the fix above, though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum