I am new to PHP and MySQL and hope you can help me with some tipps or comments on this.
I have a MySQL db with a table "Languages" from which I want to fetch all data and then echo it on my page with some HTML tags around it.
In the example below "ISO" is a language code and "$trans" the selected translation of the language names, e.g. English.
The output here is a list of (customized) checkboxes with a checkbox for each language.
So far I have the following which works as intended but since I am new to this I was wondering if I actually need the While loop here or if there is a shorter / better way to get the same - in the end I only need to echo the results of my Select.
My PHP:
// select data from db
$tbl = "Languages";
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM " . $tbl . " ORDER BY ISO ASC";
$result = $conn->query($sql);
// create array
while($arrLanguages = $result->fetch_assoc()){
$languages[] = array("ISO" => $arrLanguages["ISO"], "trans" => $arrLanguages[$lang]);
}
$conn->close();
// echo results with some HTML tags
$i = 1;
foreach($languages as $language){
echo "<input type='checkbox' class='checkSingle' id='language" . $i . "' name='language' value='" . $language['ISO'] . " - " . $language["trans"] . "' />";
echo "<label for='language" . $i . "'>" . $language['ISO'] . "</label>";
$i++;
}
select *, then. never suck in fields you're not going to use. just haveselect ISO, trans, then$languages[] = $arrLanguages.