简体   繁体   English

遍历选定的用户并动态填充下拉列表

[英]Looping throughout selected users and populating the drop-down list dynamically

So this is a part from the administrator page. 因此,这是管理员页面的一部分。 After several modifications I came to this, which is the best I could get unfortunately: 经过几次修改后,我得出了这一点,不幸的是,这是我能得到的最好的结果:

<?php

$sql = "SELECT name
        FROM users";
$result = mysqli_query($connection, $sql);

$users = array();

while ($row = mysqli_fetch_assoc($result))
{
     $users[] = $row["name"];
}

echo ('</br><form action="" method="POST" style="display:inline!important"></br>
        <select name="deleteUser">
        <option value="' . $users[1] . '">' . $users[1] . '</option>' . 
       '<option value="' . $users[2] . '">' . $users[2] . '</option>' .
       '<option value="' . $users[3] . '">' . $users[3] . '</option>' .
       '<option value="' . $users[4] . '">' . $users[4] . '</option>' .
       '<input class="w3-bar-item w3-button w3-hide-small w3-hover-green"  type="submit" value="Delete" name="delete"></select></form>');
if (isset($_POST["delete"]))
{
     $sql = "DELETE
             FROM users
             WHERE name = '" . $_POST["deleteUser"] . "'";
     mysqli_query($connection, $sql);

}
?>

I tried to write a loop to generate the select options like this: 我试图编写一个循环来生成如下所示的选择选项:

<?php

$sql = "SELECT name
        FROM users";
$result = mysqli_query($connection, $sql);

$users = array();

while ($row = mysqli_fetch_assoc($result))
{
     $users[] = $row["name"];
}

echo ('</br><form action="" method="POST" style="display:inline!important"></br>
        <select name="deleteUser">');

for ($i = 0; $i < count($users); $i++)
{
    echo('<option value="' . $users[$i] . '">' . $users[$i] . '</option>');
}   

echo('<input class="w3-bar-item w3-button w3-hide-small w3-hover-green"  type="submit" value="Delete"></select></form>');
if (isset($_POST["delete"]))
{
     $sql = "DELETE
             FROM users
             WHERE name = '" . $_POST["deleteUser"] . "'";
     mysqli_query($connection, $sql);

}
?>

But the browser doesn't render it(ie Chrome), maybe because it just can't on portions? 但是浏览器没有渲染它(例如Chrome),也许是因为它不能部分显示? Like to wait for the loop to complete and then to end with the last part of the form. 喜欢等待循环完成,然后以表单的最后部分结束。 Anyway, how could I make it work? 无论如何,我该如何运作? Thanks mates... 谢谢队友...

Instead of using while loop you can use foreach this is where you can use foreach properly 您可以使用foreach while不是使用while循环, while在这里可以正确使用foreach

Here is an example that might help you 这是一个可能对您有帮助的例子

 while ($row = mysql_fetch_assoc($result)) { 
   $rows[] = $row; 
 }
foreach($rows as $val){

   //Here populate your options like this
   echo '<option> '.$val["name"].' </option'
}

Hope this will help you out :) 希望这会帮到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM