Albert D
Albert D

Reputation: 71

php loop with variable changing simplified

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.

Upvotes: 0

Views: 66

Answers (5)

grebneke
grebneke

Reputation: 4494

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.

Upvotes: 1

Mando Madalin
Mando Madalin

Reputation: 193

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);

Upvotes: 0

MitMaro
MitMaro

Reputation: 5927

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.

Upvotes: 1

accurate respondetur
accurate respondetur

Reputation: 702

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);

Upvotes: 0

3dgoo
3dgoo

Reputation: 15794

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 />";
    }
?>

Upvotes: 0

Related Questions