下拉框中的选项在

时间:2018-04-01 14:07:00

标签: javascript php html css

英语不是我的第一语言,所以我试图尽可能具体。 我有一个表存储,所有已注册的帐户到我的网站。在它里面我有一个由php自动生成的下拉框。所以问题是,当我点击更改下拉菜单中的选项时,我的所有选项都出现在框外,并且是重复的。就像这个error dropdown box一样。这是代码

$sql1="Select ID, user_login,user_email, display_name from wp_users ";
    $result1=mysqli_query($conn,$sql1);
    echo '<div id="div_package" style="display: none" >';
    echo '<div style="text-align:center"><h1>Đổi gói thành viên</h1><input type="text" id="myInput_pack" onkeyup="FilterUsers()" placeholder="Nhập thông tin người dùng cần tìm" title="Type in a user" style="width: 20%;height:35px; font-size: 16px;border: 1px solid black; margin-bottom: 12px;text-align:center"></div>';
    echo "<table id='tb_user' class='tbadmin' border='1' cellpadding='10' align='center' style='border-collapse:collapse'>";
    echo "<tr><th>ID người dùng</th><th>Tên đăng nhập</th><th>Tên hiển thị</th><th>Email</th><th>Gói hiện tại</th><th></th></tr>";
    while($row= mysqli_fetch_array($result1))
    {
        $userid=$row['ID'];
        echo '<tr>';
        echo '<td>'.$userid.'</td>';
        echo '<td>'.$row['user_login'].'</td>';
        echo '<td>'.$row['display_name'].'</td>';
        echo '<td>'.$row['user_email'].'</td>';
        echo "<td><select name='package_$userid' id='package'>";
        $sqlcheck="SELECT 1 FROM wp_usermeta WHERE user_id=".$userid." AND meta_key='package_id' LIMIT 1";
        $result_check= mysqli_query($conn,$sqlcheck);
        if( mysqli_num_rows($result_check)!=0)
        {
            $sql_metaval="Select meta_value from wp_usermeta where user_id=".$userid." and meta_key='package_id'";
            $result_metaval= mysqli_query($conn,$sql_metaval);
            while($metaval= mysqli_fetch_array($result_metaval))
            {
                $val_package=$metaval['meta_value'];
                $test="Select post_title, ID from wp_posts where post_type='membership_package' and ID=".$val_package ;
                $sql2="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>".$val_package ;
                $result_test=mysqli_query($conn,$test);
                if(mysqli_num_rows($result_test)==0)
                {
                    echo '<option value="">FREE</option>';
                    $sql3="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>''";
                    $packages= mysqli_query($conn,$sql3);
                    while($pack = mysqli_fetch_assoc($packages)){
                        echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
                    }
                    echo '</select>';
                }
                else 
                {
                    while($row= mysqli_fetch_array($result_test))
                    {
                        echo '<option value="'.$val_package.'">'.$row['post_title'].'</option>';
                    }
                    $packages= mysqli_query($conn,$sql2);
                    while($pack = mysqli_fetch_assoc($packages)){
                        echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
                    }
                    echo '<option value="">FREE</option>';
                    echo '</select>';
                }
            }
        }

        else
        {
            echo '<option value="">FREE</option>';
            $sql3="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>''";
            $packages= mysqli_query($conn,$sql3);
            while($pack = mysqli_fetch_assoc($packages)){
                echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
            }
            echo '</select>';
        }
        echo "<td><a onclick='return confirmEdit();' href='#' id='update_$userid' class='update_user'>Cập nhật</a></td>";
        echo '</tr>';
    }

    echo "</table></br></div>";

似乎在第一个其他部分发生了某种循环。我尝试删除该部分旁边的选择标记,所有选项都出现在内部,但仍然像this one

一样重复

有人可以告诉我如何解决重复选项吗?这让我困扰了好几天。

1 个答案:

答案 0 :(得分:0)

我认为这个问题是因为您在同一select中循环选定的选项而未选择选项,而不会从查询获取未选择的选项($sql2)中排除选项。

 $test="Select post_title, ID from wp_posts where post_type='membership_package' and ID=".$val_package ;
 $sql2="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>".$val_package ;
 -------------------------------------------------------


   while($row= mysqli_fetch_array($result_test))
            {
                echo '<option value="'.$val_package.'">'.$row['post_title'].'</option>';
            }
            $packages= mysqli_query($conn,$sql2);
   while($pack = mysqli_fetch_assoc($packages)){
                echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
            }