I have this non-dynamic script works like this:
<p>Environment: <select name="formEnvironment" value="" >
<option value="TEST" <?= $_POST['formEnvironment'] == TEST ? 'selected' : '' ?>>Test</option>
<option value="PROD" <?= $_POST['formEnvironment'] == PROD ? 'selected' : '' ?>>Production</option>
</select></p>
As I have tried to make it more dynamic as possible within variables. I have problems with code within while nest and I do think it has to do with strings and variables mixing together which makes it more complicated. Any assistance with what is right way to code in that regard. I would appreciate assistance as I am only few months into PHP.
function DropDownItems($itemName, $query, $connection)
{
$result = mysqli_query($connection, $query);
while ($row = $result->fetch_assoc()){
$MAS = "$_POST['form"{$itemName}"']";
echo "<option value=\"{$row['DDVALUE']}\" ";
echo "<?= "{$MAS}" == "{$row['DDVALUE']}" ? 'selected' : '' ?>>"{$row['DDTEXT']}"</option>";
}
}
$itemName="Environment";
$query = "SELECT DDVALUE, DDTEXT FROM DDLISTS WHERE FIELDNAME='".$itemName."' ORDER BY DDTEXT ASC";
echo "<p>$itemName.": <select name='form".$itemName."'>";
echo "<option value=''>Select below:</option>";
DropDownItems($itemName, $query, $connection);
echo "</select>";