Struggling To Output Php Array As Unordered Html List
Apologies as this is probably very basic. I have created a SELECT query and have (I think) stored the data retrieved as an array. By myself I have been able to use printf to output
Solution 1:
There is not much to change in your code. Add <ul> and </ul> around the while loop. Change the pattern to <li><a href="%s">%s</a></li>. And swap $row[0], $row[1] to $row[1], $row[0]:
$result = mysqli_query($conn, "SELECT anchor, link FROM footerLinks");
echo'<ul>';
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
printf('<li><a href="%s">%s</a></li>', $row[1], $row[0]);
}
echo'</ul>';
I would though use MYSQLI_ASSOC instead of MYSQLI_NUM (which is considered bad practice), and also use the object oriented style for mysqli functions:
$result = $conn->query("SELECT anchor, link FROM footerLinks");
echo'<ul>';
while ($row = $result->fetch_assoc()) {
printf('<li><a href="%s">%s</a></li>', $row['link'], $row['anchor']);
}
echo'</ul>';
Post a Comment for "Struggling To Output Php Array As Unordered Html List"