Generate HTML select tag and options using a text file having list of countries. If the file doesn't exist or contains only empty / white space lines then generate an input box instead of select tag.
Can someone please do the code review. The code is below:
generate_select_tag_and option_from_list_of_countries.php
<?php
$file = "countries.txt";
$file_handle = @fopen($file, "r");
$line = "";
$valid_line_found = false;
$label_input_html = '<label style="position:absolute;right:70%;color:black;">** Select Country: </label>' . "\n";
$label_input_html = $label_input_html . '<input style="position:absolute;left:31%;" type="text" name="country" id="country" maxlength="40" size="40"><br><br>' . "\n";
$label_input_html = $label_input_html . "\n";
if ($file_handle == false) {
echo $label_input_html;
goto END;
}
while(!feof($file_handle)) {
$line = fgets($file_handle);
$line = trim($line);
if (empty($line)) {
continue;
} else {
$valid_line_found = true;
break;
}
} // end of while loop
if ($valid_line_found == false) {
echo $label_input_html;
fclose($file_handle);
goto END;
}
echo '<label for="country" style="position:absolute;right:70%;color:black;">** Select Country: </label>' . "\n";
echo '<select name="country" id="country" size="1" style="position:absolute;left:31%;" maxlength="40" size="40">' . "\n";
echo "\t". '<option value=""></option>' . "\n";
echo "\t". '<option value="' . $line . '">' . $line . '</option>' . "\n";
while(!feof($file_handle)) {
$line = fgets($file_handle);
$line = trim($line);
if (empty($line)) {
continue;
}
echo "\t". '<option value="' . $line . '">' . $line . '</option>' . "\n";
} // end of while loop
echo '</select><br><br>' . "\n";
echo "\n";
fclose($file_handle);
END:
?>