0

pretty sure my title needs to be fixed but anyhow, i have this loop. background info, i have 36 input "types" that need to be inserted 1 by 1. if the value has something.. set to a variable, if not set it to NULL.

MY question is, is there a loop i can perform to do this without listing 36 of these things.

using php version 5.2.17

    if (!empty($_POST['type1'])){
        $type1 = $_POST['type1'];
    }
    else 
        $type1 = NULL;

    if (!empty($_POST['type2'])){
        $type2 = $_POST['type2'];
    }
    else 
        $type2 = NULL;

    if (!empty($_POST['type3'])){
        $type3 = $_POST['type3'];
    }
    else 
        $type3 = NULL; // to 36...

php/html

<?php 
    for ($i = 1; $i < 37; $i++){
    echo "Type$i:<input name='type$i' type='text' size='20' maxlength='35' /><br />";
    }
 ?>

edit: I don't want to use an array.

5
  • 2
    This would be a whole lot easier if you defined the input names in your markup as an array: type[] Commented Jan 15, 2014 at 23:50
  • Or you could do the same thing: loop trough your post variables. Commented Jan 15, 2014 at 23:50
  • Starting question: why do you need them as seperate variables? Seems like an array would really do... Of course is can be done, but it's nasty to debug later on (you can't find easily where a variable was defined). Commented Jan 15, 2014 at 23:50
  • thats fine, i'm just looking for an answer, and i don't want to use an array. Commented Jan 15, 2014 at 23:52
  • with a for loop ? but still @wrikken 's comment is true, You dont WANT to use an array now, but debugging will become hell LATER Commented Jan 15, 2014 at 23:56

5 Answers 5

1

To actually define all those variables $type1, $type2 etc, use "variable variables":

for ($i = 1; $i < 37; $i++) {
    $varname = "type$i";
    if (! empty($_POST[$varname])) {
        $$varname = $_POST[$varname];
    }
    else {
        $$varname = NULL;
    }
}

Others suggest using an array instead (and in principle I agree), but this is an actual answer to the question.

Sign up to request clarification or add additional context in comments.

3 Comments

YES! indeed it does work, thanks for this. I know an array would work, there is 5000 ways of doing anything really, i wanted a simple solution to my problem without having to redo my entire script. thanks.
if you could just explain to me why you use two $$ in $$varname ?
It's all explained in the link to PHP-documentation on variable variables. Think of it this way: Let's say $i = 6. Then $varname = "type6", and $$varname is the same as saying $type6.
1

Question was updated to not use an array. Will keep it here for someone looking for an answer that can use an array.


You could add the $_POST array to a predefined array of values storing the result in another array. For example:

<?php
$defaults = array(
    "type1" => NULL,
    "type2" => NULL,
    "type3" => NULL,
    "type4" => NULL
    // etc
);
$_POST = array(
    "type1" => 1,
    "type2" => "foo"
);
$types = $_POST + $defaults;
print_r($types);

Which results in the array:

Array
(
    [type1] => 1
    [type2] => foo
    [type3] =>
    [type4] =>
    ...
)

Then for your html loop:

for ($i = 1; $i < 37; $i++){
    echo "Type$i:<input name='" . $types["type" . $i] . "' type='text' size='20' maxlength='35' /><br />\n";
}

It is important to note that this is slightly different than using a check with empty.

Comments

0

Use an array on both the php and form inputs.

$type = array();

for ($i = 1; $i <= 36; $i++) {
    if (isset($_POST['type'][$i])){
        $type[$i] = $_POST['type'][$i];
    }
}

HTML/PHP

<?php 
    for ($i = 1; $i <= 36; $i++){
        echo "Type$i:<input name='type[$i]' type='text' size='20' maxlength='35' /><br />";
    }
?>

Comments

0

Just an example with an array named $post.

$post = array(
'type1' => 'one',
'name' => 'test',
'type3' => 'three'

);
$matches  = preg_grep ('^type[0-9]^', array_keys($post));

foreach ($matches as $val) {
$$val = $post[$val];
}

var_export($type1);

Comments

0

Use array for this

$i = 0;
$myinputs = 36;

while($i<$myinputs){
 $i++;
 if(empty($_POST['type'.$i.''])){
    $type[$i] = $_POST['type'.$i.''];
 }else{
    $type[$i] = null;
 }



}

print_r($type);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.