mardi 24 janvier 2017

What is the best practice for a multi-conditional php if statements with variables?

I have a form which fetched inner variables. Original looks like this:

if ($something != 'Some-thing'{
    $code = ' <form id="form" method="post" action="' . $the_url . '">
              </form>';
    return $code;
} else { 
$code = '<form action="http://ift.tt/1aEY8Qf">
         </form>';
return $code;
}

It first checked if variable $something equals a value, if so then first, if not then second.

But then I wanted to add in new fields, with if/else statement to toggle the whole field.

Now, I already have a toggle on the back end which I click a checkbox and it changes $variable1 to on or off. See below:

screenshot of the back end

Then I add this input field which fetches another field to fill it in if it is toggled:

<input type="hidden" name="a_variable_1" value="' . $params['a_variable_A'] . '" />

Now, I was having trouble putting the if statement inside, so I duplicated the whole field. Which was fine for just one extra variable.

But then I wanted to add a second field with an on/off toggle. And this is where things began to get messy.

Here is my code, but it doesn't seem very practical. And if I wanted to add another variable it would just get absurd.

if ($something != 'Some-thing' && $variable1 != "on" && $variable2 != "on") {
    $code = ' <form id="payfrm" method="post" action="' . $the_url . '">
              </form>';
    return $code;
} elseif ($something != 'Some-thing' && $variable1 != "on" && $variable2 === "on") {
    $code = ' <form id="payfrm" method="post" action="' . $the_url . '">
                    <input type="hidden" name="variable_2" value="' . $params['variable_B'] . '" />
              </form>';
    return $code;
} elseif ($something != 'Some-thing' && $variable1 === "on" && $variable2 != "on") {
    $code = ' <form id="payfrm" method="post" action="' . $the_url . '">
                    <input type="hidden" name="variable_1" value="' . $params['variable_A'] . '" />
              </form>';
    return $code;
} elseif ($something != 'Some-thing' && $variable1 === "on" && $variable2 === "on") {
    $code = ' <form id="payfrm" method="post" action="' . $the_url . '">
                    <input type="hidden" name="variable_1" value="' . $params['variable_A'] . '" />
                    <input type="hidden" name="variable_2" value="' . $params['variable_B'] . '" />
              </form>';
    return $code;
} else { 
$code = '<form action="http://ift.tt/1aEY8Qf">
         </form>';
return $code;
}

Above, variable_A is the value put into the back end for $variable1 as shown in the screenshot given above. Likewise, variable_B is the text value for $variable2 from the back end.

What is the best way to add the if statement inside? Or how can I revise my code for optimal practice? Ideally I would like to make it easy for me to add in more variables with fields inside the input, without having to have a messy complicated code.

Aucun commentaire:

Enregistrer un commentaire