简体   繁体   中英

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. 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. I don't want item_id to show up in the html selector.

I tried a few things like 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:

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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