PDA

View Full Version : Using a for loop with an array?


Iain M
08-20-2011, 12:33 PM
Hi,

I'm having problems validating radio options from a form, before entering into the database.
Because the names of the radio options change depending on the row id.
I put my validation into a for loop, but it didn't work - nothing was posted from the form.


for($i=$oldestID['id'];$i<=$newestID['id'];$i++)
{
// Clean parameters
$vbulletin->input->clean_array_gpc('p', array(
'gender_$i' => TYPE_BOOL,
'free_$i' => TYPE_BOOL,
'active_$i' => TYPE_BOOL,
));
// Easy names
$gender = $vbulletin->GPC['gender_$i'];
$free = $vbulletin->GPC['free_$i'];
$active = $vbulletin->GPC['active_$i'];

// Do stuff
}

The names of the fields are:
gender_{vb:raw id}
free_{vb:raw id}
active_{vb:raw id}

id changes for each row pulled from the database, so we'll have
HEADER
Row 1: gender_1 | free_1 | active_1
Row 2: gender_2 | free_2 | active_2
Row 10: gender_10 | free_10 | active_10

Could someone point me in the right direction?

If I use

$gender = $_POST["gender_$i"];
$free = $_POST["free_$i"];
$active = $_POST["active_$i"];

in my for loop, it works fine, but I'd rather validate the entries.



Thanks in advance for your help.


Iain

Marco64Th
08-20-2011, 12:50 PM
<a href="http://th2.php.net/manual/en/control-structures.foreach.php" target="_blank">http://th2.php.net/manual/en/control...es.foreach.php</a>

And it would be a lot easier ifyou used arrays like: GPC['gender'][] or better: GPC[]['gender']

Iain M
08-20-2011, 01:34 PM
Thanks Marco, could you give me an example?

So, I would name my fields gender[{vb:raw id}], free[{vb:raw id}], active[{vb:raw id}]?

How would I use foreach to use vbulletin's input cleaner?

Marco64Th
08-20-2011, 03:39 PM
If i have time i will try to put up an example tomorrow. But if you would use the id as array index, then you would only put the array itself through the input cleaner (GPC['gender']) and then loop through the resulting arrays.

Iain M
08-20-2011, 04:31 PM
That would be great thanks =] I'll try and figure it out tho.

--------------- Added 1313870349 at 1313870349 ---------------

Ok.. got somewhere.

I have my fields named as: gender[{vb:raw id}], free[{vb:raw id}], active[{vb:raw id}]

and my inputs being validated:

$vbulletin->input->clean_array_gpc('p', array(
'gender' => TYPE_ARRAY_UINT,
'free' => TYPE_ARRAY_UINT,
'active' => TYPE_ARRAY_UINT,
));


Now it's figuring out how to post the data to the database. I've had a look at the foreach, but I think that'd require foreach loops for each array?
$i = 1; /* for illustrative purposes only */

foreach ($vbulletin->GPC['gender'] as $gender) {
echo "\$vbulletin->GPC['gender'][$i] => $gender.\n";
$i++;
}
Which outputs:
$vbulletin->GPC['gender'][1] => 3. $vbulletin->GPC['gender'][2] => 3. $vbulletin->GPC['gender'][3] => 3.

Is there a way to merge them together or another way of doing it?

--------------- Added 1313874927 at 1313874927 ---------------

Got it working! Thanks for your help Marco.

So, you don't need multiple foreach loops, you just put your array in the first one. Here's the full working code if anyone's interested:
if($_REQUEST['action'] == "updatenames")
{
// Clean parameters
$vbulletin->input->clean_array_gpc('p', array(
'gender' => TYPE_ARRAY_UINT,
'free' => TYPE_ARRAY_UINT,
'active' => TYPE_ARRAY_UINT,
));

// Get the oldest ID
$oldestID = $db->query_first("SELECT id FROM " . TABLE_PREFIX . "names WHERE userid = '" . $vbulletin->userinfo['userid'] . "' ORDER BY id ASC");
$i = $oldestID['id'];

// Loop through and update each record
foreach ($vbulletin->GPC['gender'] as $gender)
{
// Easy names
$free = $vbulletin->GPC['free'][$i];
$active = $vbulletin->GPC['active'][$i];

$db->query_write("
UPDATE " . TABLE_PREFIX . "names
SET gender = '$gender',
free = '$free',
active = '$active'
WHERE id = '$i'
");
$i++;
}

// Redirect to view changes
$vbulletin->url="names.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewnames";
eval(print_standard_redirect('names_updated', true, true));
}