简体   繁体   English

使用html / php多重选择器从数据库中获取多个值

[英]Getting multiple values out of a database using html/php multiple selector

I am trying to extract multiple values from a row in a table in a mysql database. 我试图从mysql数据库的表中的一行中提取多个值。 I want the selector to show the description only, and after the form is submitted, I want to be able to access additional information from that row. 我希望选择器仅显示描述,提交表单后,我希望能够从该行访问其他信息。 I am able to get all of the item_types out into an array, but I am not sure how to add the item_id. 我能够将所有item_types放入一个数组中,但是我不确定如何添加item_id。 I don't want item_id to show up in the html selector. 我不希望item_id显示在html选择器中。

I tried a few things like array_push. 我尝试了一些类似array_push的东西。 The only way I can think of getting this done is by making one big string in "value" and extracting the parts after the form is submitted. 我能想到的唯一方法是在“值”中创建一个大字符串,并在提交表单后提取各部分。

Here is the function so far: 这是到目前为止的功能:

function createDropdown() {
    echo '<select multiple name="items[]">';
        try {
            $items = mysql_query("SELECT item_id,item_type FROM items");
            while ($row = mysql_fetch_assoc($items)) {
                echo '<option value="'.$row['item_type'].'"';
                echo '>'. $row['item_type'] . '</option>'."\n";
            }
        }
        catch(PDOException $e) {
            echo 'No results';
        }
echo '</select>';
}

Hmm you can try to generate a lookup table whenever you create a drop-down list: 嗯,无论何时创建下拉列表,您都可以尝试生成查找表:

function createDropdown(&$ddlLookup) {
    echo '<select multiple name="items[]">';
        try {
            $items = mysql_query("SELECT item_id,item_type FROM items");
            while ($row = mysql_fetch_assoc($items)) {
                echo '<option value="'.$row['item_type'].'"';
                echo '>'. $row['item_type'] . '</option>'."\n";
                $ddlLookup[$item_type] = $item_id;
            }
        }
        catch(PDOException $e) {
            echo 'No results';
        }
echo '</select>';
}

Then whenever you need the id for a given description you use that table(array) to get it: 然后,只要您需要给定描述的ID,就可以使用该表(数组)获取它:

$mainDropdownLUT = array();
createDropdown($mainDropdownLUT);

var_dump($mainDropdownLUT['testCow']);
 -> 734

Also, if you need to pass it to another page it can be serialized and added to a hidden field. 另外,如果您需要将其传递到另一个页面,则可以对其进行序列化并将其添加到隐藏字段中。

 $mainDropdownLUT = serialize($mainDropdownLUT);
   "<input type="hidden" value =\"$mainDropdownLUT\">"
   -------------------------**OTHER PAGE **--------------
    $mainDropdownLUT = unserialize($mainDropdownLUT);

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

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