0

http://localhostr.com/files/V1ruKKj/capture.png

I want to rewrite all these ifs to something more manageable for lots of form fields.

I know it's not right but I want to rewrite it to something like this:

$fields = array(); 

function infcheck($var) 
{ 
if ( !empty( $var ) ) 
{ 
$fields[$var] = $var; 
} 

} 

infcheck( $_POST['streetname'] ); 
infcheck( $_POST['city'] ); 
infcheck( $_POST['state'] );

Basically when I run infcheck() I want the output to look like this:

Assuming $_POST['streetname'] is "Circle Street"

streetname => "circle street"

for a full example:

$fields = array(); 

infcheck( $_POST['streetname'] ); 
infcheck( $_POST['city'] ); 

//would be:

$fields = array(streetname => 'Circle Street', city => 'New York City');

I guess what I'm having trouble with is keeping the name of the form when $_POST['formname'] is turned into a variable.

hakre
  • 178,314
  • 47
  • 389
  • 754
Tek
  • 2,592
  • 5
  • 41
  • 69
  • you may need to `global $fields;` in your function. – drudge Mar 10 '11 at 01:11
  • @jnpcl He most certainly **doesn't need `global`**. Nobody does. – deceze Mar 10 '11 at 01:13
  • 1
    @deceze: I meant in his current implementation. – drudge Mar 10 '11 at 01:20
  • @Tek What exactly is the goal? Currently you're just transferring all set variables from `$_POST` to another variable called `$fields`. Is that all you want to do? – deceze Mar 10 '11 at 01:20
  • @jnpcl Please don't advise the use of `global`, whatever the circumstances. :) – deceze Mar 10 '11 at 01:22
  • @deceze: Please don't pretend that it is not the way to fix the above code with the given implementation, when it is. This knee-jerk reaction against `global` is all well and good if you're going to provide a balanced argument, but please don't spread one-sided fear-mongering rhetoric to those who don't know any better. – Lightness Races in Orbit Mar 10 '11 at 01:24
  • @Tomalak I'm not saying it's not a way to fix the above implementation. The OP is on the wrong track already with his solution, the use of `global` would just make it worse. There's virtually no situation where `global` is a good solution, so it shouldn't be advertised, *especially* to those who don't know better. – deceze Mar 10 '11 at 01:26
  • @deceze: You did not say all that. You just put misleading fear-mongering in bold. Also note that your hatred of global is still just your opinion. – Lightness Races in Orbit Mar 10 '11 at 01:32
  • @Tomalak Point taken. Next time I'll link to [other people's opinions](http://stackoverflow.com/questions/5166087/php-global-in-functions/5166527#5166527) then. – deceze Mar 10 '11 at 01:40
  • @deceze: Yea, link to an answer that starts "Globals Are Evil" in big bold letters. How about a little balance? This is a programming community, not a propaganda campaign. We're not here to force people into specific viewpoints. – Lightness Races in Orbit Mar 10 '11 at 01:47
  • @Tomalak Then I'd be interested to hear your counter argument for why `global` would be a good solution here, as opposed to, say, my solution below. – deceze Mar 10 '11 at 01:50
  • @deceze: I don't think that it would be a better solution here. That does not mean I will go on a rampage pretending that it does not exist, and screaming at everybody not to use it under any circumstances, ever. Further, as an immediate fix to the current implementation, it's _completely_ valid. – Lightness Races in Orbit Mar 10 '11 at 01:51

2 Answers2

2

What you're basically doing is this:

$fields = array_filter($_POST);

If you want to limit the $fields array to certain keys in a predetermined list and skip anything in the $_POST array that's not on that list, you can do something like this:

$whitelist = array('streetname', 'city', ...);
$fields = array_filter(array_intersect_key($_POST, array_flip($whitelist)));
deceze
  • 471,072
  • 76
  • 664
  • 811
0

You could just put the array of values you want to pass through a foreach loop..

foreach($_POST as $key => $value){

$fields[$key] = $value;

}
Christian
  • 96
  • 3