php获取用户下拉列表选择

发布于 2024-10-30 12:54:24 字数 655 浏览 1 评论 0原文

我如何为每个用户选择下拉菜单。

user table
------------
id    job
1     1
2     2

job table
----------
id   name
1    Doctor
2    Sales

$q = $db->query("SELECT * FROM affiliate LEFT JOIN user ON user.job = affiliate.id_affiliate");
        while($r = $q->fetch_array()) :
            if($r['id_user'] == $_SESSION['id_user'] && $r['job'] == $r['id_affiliate']) {
                echo '<option selected value="'.$r['id_affiliate'].'">'.$r['org'].'</option>'; 
            } else {
                echo '<option value="'.$r['id_affiliate'].'">'.$r['org'].'</option>'; 
            }
        endwhile;

how do i get dropdown selected for each user.

user table
------------
id    job
1     1
2     2

job table
----------
id   name
1    Doctor
2    Sales

$q = $db->query("SELECT * FROM affiliate LEFT JOIN user ON user.job = affiliate.id_affiliate");
        while($r = $q->fetch_array()) :
            if($r['id_user'] == $_SESSION['id_user'] && $r['job'] == $r['id_affiliate']) {
                echo '<option selected value="'.$r['id_affiliate'].'">'.$r['org'].'</option>'; 
            } else {
                echo '<option value="'.$r['id_affiliate'].'">'.$r['org'].'</option>'; 
            }
        endwhile;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-11-06 12:54:24

selected="selected" 或只是 selected 应该正常工作。如果不是,则说明您的 if 语句有问题。一种简单的方法是回显 if 语句的内容,如下所示:

注意!回显通常应该在选择开放标签之外完成,只需将以下内容粘贴到选择开放标签之外,但就在查询之后。

 while($r = $q->fetch_array()) :
        echo $r['id_user'] .'=='. $_SESSION['id_user'] .'&&'. $r['job'] .'== '.$r['id_affiliate'].'<br />';
 endwhile;

您现在可以检查这些值是否确实匹配。如果没有,那就是你的问题。

selected="selected" or just selected should normally work. If not there is a problem with your if statement. One simple way is to echo out the content of the if statement like this:

note!! the echo should normally be done outside the select open tag, just paste the following outside the select open tag but just after your query.

 while($r = $q->fetch_array()) :
        echo $r['id_user'] .'=='. $_SESSION['id_user'] .'&&'. $r['job'] .'== '.$r['id_affiliate'].'<br />';
 endwhile;

you can now check if the values actually match. if not then there is your problem.

守望孤独 2024-11-06 12:54:24

修改以下内容怎么样...

if( ($r['id_user'] == $_SESSION['id_user']) && ($r['job'] == $r['id_affiliate']) )

不确定这是否重要,但我在选项末尾选择了选择

<option value='cat' selected>

How about modifying the following ...

if( ($r['id_user'] == $_SESSION['id_user']) && ($r['job'] == $r['id_affiliate']) )

Not sure if it matters, but I have selected at the end of my option.

<option value='cat' selected>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文