Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-20-2011, 12:33 PM
Iain M's Avatar
Iain M Iain M is offline
 
Join Date: Jul 2005
Location: Glasgow, Scotland
Posts: 256
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
Reply With Quote
  #2  
Old 08-20-2011, 12:50 PM
Marco64Th Marco64Th is offline
 
Join Date: Aug 2011
Posts: 34
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

<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']
Reply With Quote
  #3  
Old 08-20-2011, 01:34 PM
Iain M's Avatar
Iain M Iain M is offline
 
Join Date: Jul 2005
Location: Glasgow, Scotland
Posts: 256
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #4  
Old 08-20-2011, 03:39 PM
Marco64Th Marco64Th is offline
 
Join Date: Aug 2011
Posts: 34
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 08-20-2011, 04:31 PM
Iain M's Avatar
Iain M Iain M is offline
 
Join Date: Jul 2005
Location: Glasgow, Scotland
Posts: 256
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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));        
    } 
Reply With Quote
Reply


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. The time now is 06:19 PM.


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.05324 seconds
  • Memory Usage 2,231KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete