简体   繁体   English

在中选择特定行 <select> SQL查询返回后的列表

[英]Select specific row in <select> list after return from SQL query

I have this rather tricky problem to solve whereby I have used the following to display a list of named shops with ID number for selection from a drop down list when creating a new employee record. 我要解决这个棘手的问题,在创建新员工记录时,我使用以下内容显示了具有ID号的命名商店列表,以便从下拉列表中进行选择。 This works well at this point. 在这一点上,这很好。 A piece of javascript splits the displayed text from the the user selection and sends the shop ID number off with the new employee details to be inserted into the employee table in the database. 一段javascript会从用户选择中拆分显示的文本,并发送带有新员工详细信息的商店ID号,以将其插入数据库中的employee表中。 I am using a hidden shopID text box to store the number as can be seen in the javascript. 我正在使用一个隐藏的shopID文本框来存储数字,如javascript中所示。

Here is the code PHP first then javascript: 这是代码PHP首先是javascript:

$result = mysqli_query($link,"SELECT shopID, shopName FROM SHOP");
            echo "<br><select class='formInput' name='listbox' id='listbox' onchange='captureShopID()' tabindex=9>";
            #Use onchange instead of onclick where Keyboard is used. onclick does not register changes fro keyboard
            while($row = mysqli_fetch_array($result))
            {
                $shopID = $row['shopID'];
                $shopName = $row['shopName'];
                $allText = "$shopID, $shopName";
                echo "<option value='$allText'>S00$shopID  $shopName</option>";
            }
            echo "</select>";

AND (including this, because it may contain hints to a solution for my problem) AND(包括此内容,因为它可能包含解决我的问题的提示)

<script>
function captureShopID()
{
  var sel = document.getElementById("listbox");
  var result;
  result = sel.options[sel.selectedIndex].value;
  var shopNumber = result.split(',');
  document.getElementById("shopID").value = shopNumber[0];
}
</script>

OK. 好。 So all good so far. 到目前为止一切都很好。 What I am trying to do is use the same set up for amendments to the same record. 我想做的是使用相同的设置修改相同的记录。 So the update layout is similar to the one for creating the record. 因此,更新布局类似于用于创建记录的布局。 I have the list element again but what I would like is to have it showing the shop that the employee works at otherwise it is confusing as the list defaults to the first item in the list, in most cases not the actual shop that the employee works in. 我再次有了list元素,但我想让它显示员工在其工作的商店,否则会令人困惑,因为该列表默认为列表中的第一项,在大多数情况下不是该员工在工作的实际商店在。

So, instead of: 因此,代替:

S001 London S001伦敦

Maybe it should be: 也许应该是:

S003 Paris… Where the employee works. S003巴黎…员工在哪里工作。

I have tried various things but it is a tricky one. 我已经尝试过各种方法,但这是一个棘手的问题。 The fact that there is the option value concatenation of $shopID and $shopName may be complicating things a bit in my quest for a solution. $ shopID和$ shopName存在选项值串联的事实可能会使我寻求解决方案的事情变得有些复杂。

Pretty new to PHP and javascript (javascript pretty mysterious) and programming as a whole. PHP和javascript的新知识(JavaScript相当神秘)和整个编程。 Learning quickly but suffer many days of brain cell overload. 学习速度很快,但会遭受许多天的脑细胞超负荷。

Any pointers in the right direction appreciated. 任何朝着正确方向的指针表示赞赏。

My Solution… After some outside the box tangental thinking working on some other aspects of my project. 我的解决方案…经过开箱即用的思考,在项目的其他方面进行了研究。

<?php
            include "../db.php";

            $result = mysqli_query($link,"SELECT shopID, shopName FROM SHOP");
            echo "<br><select class='formInput' name='listbox' id='listbox' onchange='captureShopID()' tabindex=9>";
            #Use onchange instead of onclick where Keyboard is used. onclick does not register changes from keyboard
            echo '<option>Works at:'.$shopName.'</option>';
            while($row = mysqli_fetch_array($result))
            {
                $shopID = $row['shopID'];
                $shopName = $row['shopName'];
                $allText = "$shopID, $shopName";
                echo "<option value='$allText'>S00$shopID  $shopName</option>";
            }
            echo "</select>";
        ?>

I achieved what I wanted by adding the extra to the top of the list outside the loop to act as the default item listed: 通过在循环之外的列表顶部添加多余的内容作为列出的默认项,我实现了所需的功能:

echo 'Works at:'.$shopName.''; 回显'Works at:'。$ shopName。'';

This has the result of presenting the update employee record so that shop from their record is what you see in the menu like so: 结果是显示更新的员工记录,这样您从菜单中看到的商店就是您在菜单中看到的,如下所示:

Works at: London Southwark 在伦敦南华克工作

And the list pops up to show the other shop options 并弹出列表以显示其他商店选项

Works at: London Southwark 在伦敦南华克工作
S001 DISTRIBUTION CENTRAL Paris S001巴黎市中心分布
S002 Paris Montmartre S002巴黎蒙马特
S003 London Southwark S003伦敦南华克
S004 Roma Trastevere S004罗马Trastevere

and so on. 等等。

Not exactly what I initially intended but just as good 不完全是我最初的意图,但同样出色

I am aiming for a clean and spasre interface without too many labels and this solution makes the list element self explanatory. 我的目标是提供一个干净而简洁的界面,且没有太多标签,并且此解决方案使list元素易于说明。

If any one has a good suggestion for the thread title please post as this one is quite useful. 如果有人对主题名称有很好的建议,请发贴,因为这很有用。

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

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