A php array as the value for a drop down menu?

Howdy all,

I have an incredibly complicated page that displays a drop down menu with data from a DB. The data is of various types, from different tables in my DB. For example sake, the drop down menu might list off a user’s favorite items. Some of them might be books, which have a certain structure, and some might be movies (diff structure).

I have several buttons that allow the user to move those items up and down in the list, remove them, rename them, etc.

I need to pass 2 values from the drop down menu with a form, an item ID and a type ID. The “remove” function would then remove the item matching that ID and type from the user’s favorite list.

here’s some code:

// Move Up Function (Form submits to self, passing in dropdownarray
if (isset($_GET['btnMoveUp'])) {
    if (!isset($_GET['dropdownarray'])) {
    	$message .= '<font color="red">You must first choose a item to move</font><br /><hr>';
    } else { // move the destination up

        $dropdownarray = $_GET['dropdownarray'];
        echo $dropdownarray['itemID'] .'<br />';
        echo $dropdownarray['typeID'] .'<br />';

    // move the item up //

// dropdown header
echo '<select style="width: 380px;" name="dropdownarray" size="' .$size .'">';

// inside my drop down menu
echo '<option value=' .$dropdownarray .'>';
echo '</option>';

The two echo statements above print out the letter “A” and nothing else. They should print out 2 numbers, the first between 1 and 7000 (itemID) and the second should be 1 2 or 3.

TIA for any suggestions,

Forgot to add that the correct values are in the $dropdownarray before it is passed[/edit]

Hi Gibb. The question is how the option values are in the dropdown menu. In other words, how does the $dropdownarray looks like.

Just to show you a possible way. The drop down menu:

<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <select name="dropdownarray" id="dropdownarray">
        <option value="1_1">Item 1 - Type 1</option>
        <option value="23_4">Item 23 - Type 4</option>
        <option value="756_4">Item 756 - Type 4</option>
        <option value="1145_5">Item 1145 - Type 5</option>
        <option value="5555_3">Item 5555 - Type 3</option>
        <option value="6130_1">Item 6130 - Type 1</option>
        <option value="6999_2">Item 6999 - Type 2</option>
    <button type="submit" name="action" id="action" value="btnMoveUp">Move up</button>

And the move up code:

$message = '';
if(isset($_GET['action']) && $_GET['action'] = 'btnMoveUp') {
    if($_GET['dropdownarray']) {
        $dropdownarray = explode("_", trim($_GET['dropdownarray']));
        echo 'Item: ' , $dropdownarray[0] , '<br />';
        echo 'Type: ' , $dropdownarray[1] , '<br />';
        // Move up
    } else {
        $message .= '<font color="red">You must first choose a item to move</font><br /><hr>';

If the 1145_5 option is selected, it will print:

Item: 1145
Type: 5

If want to get more spesific help you need to post more about how you create you drop down list.

-Helge :slight_smile:

I had thought of doing it the way you posted, but I figured you could pass an array more easily.

// I setup my arrays like this:

// query and results

// while loop

$myArray['itemid'] = $result['item_id'];
$myArray['itemtype'] = $result['item_type'];

// then in my dropdown menu

<option value="$myArray">My Item</option>

// end drop down
// end while loop

Does this method not work because $myArray will be the same for every line in the drop down menu because it is reset for every step through the while loop? I’ll give your method a shot since that looks like it’ll work perfectly.

Thanks :slight_smile:

Ok just tried your _ explode method. Worked like a charm. Thanks again :slight_smile:

Glad you got it working. :slight_smile:

I have a question. I have 100 records, and divide it into 10 pages. So how to navigate to ?mode=view&page=4, then on the dropdown menu it will show number 4, like this: Page: [4]

echo "<select  onChange=\\"jumpMenu('parent',this)"><option>Page</option>";

echo "<option value=\\"?mode=view&page=$i\\" >$i</option>";
echo "</select>";

In the above codes, whatever I go, the menu still go to [Page], but not the page of the view mode.

so how to fix?
thank you

// your code
echo "<select  onChange=\\"jumpMenu('parent',this)"><option>Page</option>";

echo "<option value="?mode=view&page=$i\\" >$i</option>";

// try this

echo '<select  onChange="jumpMenu(\\'parent\\',this)"><option>Page</option>';
echo '<option value="?mode=view&page=$i">' .$i .'</option>';

this way doesn’t work. It jumps to ?mode=view&page=$i, but not page=4 or 6

// try this then
echo '<option value="?mode=view&page=' .$i .' ">' .$i .'</option>';

change this $i <= $blah to $i < $blah
cheers :slight_smile:

tried all. none works. I think there maybe selectedIndex something?

This is the jumpMenu, I’m not sure if it has something to do with the loop?

<script language="JavaScript">
function jumpMenu(targ,selObj,restore){ //v3.0
  if (restore) selObj.selectedIndex=0;