vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Using a for loop with an array? (https://vborg.vbsupport.ru/showthread.php?t=268813)

Iain M 08-20-2011 12:33 PM

Using a for loop with an array?
 
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.

PHP Code:

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

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
PHP Code:

            $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 [DATE]1313870349[/DATE] at [TIME]1313870349[/TIME] ---------------

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:
PHP Code:

            $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?
PHP Code:

$i 1/* for illustrative purposes only */

foreach ($vbulletin->GPC['gender'] as $gender) {
    echo 
"\$vbulletin->GPC['gender'][$i] => $gender.\n";
    
$i++;


Which outputs:
Code:

$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 [DATE]1313874927[/DATE] at [TIME]1313874927[/TIME] ---------------

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:
PHP Code:

    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'truetrue));        
    } 



All times are GMT. The time now is 12:01 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01029 seconds
  • Memory Usage 1,757KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete