scotchegg78 Posted September 4, 2009 Share Posted September 4, 2009 HI guys I have a form collecting just .. 15 input boxes of numeric numbers 1-10 a colour enum- red, white yellow or blue. hidden userid field. is it better to have 15 instances of.. $score_1 = filter_input(INPUT_POST, 'score_1', FILTER_VALIDATE_INT); $score_2 = filter_input(INPUT_POST, 'score_2', FILTER_VALIDATE_INT); $score_3 = filter_input(INPUT_POST, 'score_3', FILTER_VALIDATE_INT); or do something with the php input array function? Does it really make a performance difference? thanks for advice. Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/ Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 Performance-wise it's irrelevant, but I would recommend using an array. It's much easier to work with, and it's a data structure specifically designed to hold groups of data. Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/#findComment-912606 Share on other sites More sharing options...
scotchegg78 Posted September 4, 2009 Author Share Posted September 4, 2009 ok thanks this maynot make sense, but then would i not have to put my form 15 inputs into an array? like <input type="text" class="text" name="score[]" id="score_1" /> <input type="text" class="text" name="score[]" id="score_2" /> <input type="text" class="text" name="score[]" id="score_3" /> etc sorry just not used php filter array before. and to keep the entered input on page reload how do i access these from the post array? is it.. <input type="text" class="text" name="score[]" id="score_1" [b]value="<?php echo $_POST['score[1]']?>"[/b]/> ?? that looks wrong! Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/#findComment-912626 Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 First of all, remember that enumerated arrays are zero indexed! Secondly, $_POST is an array, as you know, so if you need to access an array inside $_POST you do like $_POST['some_array']['some_index']. As for the form, you can do like you did, but you can also explicitly specify the index like this: <input type="text" class="text" name="score[5]" id="score_5" /> You'll need to set a value for the checkboxes though. To make it checked when the page loads you will have to add checked="checked". Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/#findComment-912628 Share on other sites More sharing options...
scotchegg78 Posted September 4, 2009 Author Share Posted September 4, 2009 hi dan i am not using checkboxes, its 15 input boxes for int values only. so my load values from the post on page load into form would be.. <input type="text" class="text" name="score[5]" id="score_5" value="<?php echo $_POST['score'][5]"?>" /> ? Is that right? thanks Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/#findComment-912639 Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 i am not using checkboxes, its 15 input boxes for int values only. Ah, sorry. I could have sworn I saw the word "checkbox" somewhere. so my load values from the post on page load into form would be.. <input type="text" class="text" name="score[5]" id="score_5" value="<?php echo $_POST['score'][5]"?>" /> ? Is that right? Looks so. You could try it out and see if it works You'll need to use isset to make sure you aren't accessing an invalid index though. Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/#findComment-912645 Share on other sites More sharing options...
scotchegg78 Posted September 4, 2009 Author Share Posted September 4, 2009 thanks i will, just trying to get my head around the use of array filter on my 15 input boxes! is this it for just the score post array then.. $filter = array('score' => FILTER_SANITIZE_INT); $cleaninputs = filter_input_array( INPUT_POST, $filter ); or if i had another post in their like colour for example its.. $filter = array( 'score' => FILTER_SANITIZE_INT, 'colour' => FILTER_SANITIZE_STRING); $cleaninputs = filter_input_array( INPUT_POST, $filter ); thanks Quote Link to comment https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/#findComment-912653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.