我试图根据从数据库中获取的数组,将带有数据库变量的li标签动态地加载到页面中。 li标签的数量将是可变的。我正在努力实现这一目标。请您提供任何帮助
$packages_menu = array();
$sql = ("SELECT provider, technology_name FROM technology ORDER BY technology_id ASC");
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$packages_menu[] = $row;
}
我有什么?
<li><?php echo $packages_menu; ?></li>
这是数组
Array
(
[0] => Array
(
[provider] => openserve
[technology_name] => fibre
)
[1] => Array
(
[provider] => openserve
[technology_name] => adsl
)
[2] => Array
(
[provider] => trusc
[technology_name] => fibre
)
[3] => Array
(
[provider] => trusc
[technology_name] => wireless
)
[4] => Array
(
[provider] => sonic
[technology_name] => wireless
)
)
答案 0 :(得分:0)
您有两种解决方案:收集完所有行之后,可以使用另一个循环输出数据。看起来可能是这样的:
foreach($packages_menu as $row) {
echo '<li>' . $row['provider'] . '</li>';
}
如果收集所有数据并输出不是直接接连发生,这可能是一个很好的解决方案。
如果两者之间没有代码,则无需使用中间的行数组。因此,即使这不是一个很好的解决方案(如第一个解决方案那样,它应保持PHP逻辑并输出更分离的效果),也可以这样做:
$sql = ("SELECT provider, technology_name FROM technology ORDER BY technology_id ASC");
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
echo '<li>' . $row['provider'] . '</li>';
}
答案 1 :(得分:-1)
Now, I'm not sure exactly how you're trying to display it, but you can see from the code below how to put your data in side of a list of tags. Then just return that list in the middle of your opening and closing `<ul>` tags.
$list_items = ""; //declare outside of the loop
while($row = mysqli_fetch_assoc($result)) {
$list_items += "<li>" . $row['provider'] . "</li>";
$list_items += "<li>" . $row['technology_name'] . "</li>";
}