Jump to content

Dynamically choosing menu field


smc

Recommended Posts

Hey,

I have a drop down menu avaiable for editing and submitting articles in my CMS. My drop down menu on the HTML page has 3 entries, how can I get it to automatically choose the entry in the database for editing purpoes.

For example: I have Apples in my database under fruit. In editing the food favorites I have a list with Apples, Oranges, and Pears. I want to choose Apples automatically.

Using the above example, I tried value="<?php echo fruit; ?>" but no dice.

Any ideas?

Thanks!
Link to comment
Share on other sites

[code]
<?php
        echo "<form method=\"post\" action=\"". $_SERVER['php_self'] ."\">\n";
        echo "<select name=\"dropdown\">\n";
        $sql = "SELECT * FROM your_table";
        $query = mysql_query($sql);
        while($row = mysql_fetch_array($query)){
                echo "<option name=\"name\" value=\"". $row['value'] ."\"". (($row['value'] == $_POST['dropdown']) ? " SELECTED" : "") ."\">\n";
        }     
        echo "</select>\n";
        echo "<input type=\"submit\" value=\"Submit\">\n";
        echo "</form>\n";
?>
[/code]

untested.
Link to comment
Share on other sites

I don't know how you have your database set up but you could just use PHP to echo out your options, something like a
[code]<?php
while($row[variable] = mysql_fetch_array($var_result)) {
if ($something == $somethingelse) {
$selected = "selected=\"selected\" " ;
}
echo("<option name=\"name\" value=\"value\" $selected/>");
reset($selected);
}
?>[/code]

Like I said I don't know how your whole script is set up so that's a very simplified version but sometimes the simplified ways help figure out the complicated ones ^_^ (at least for me that's true). This is a similar way I did dynamic drop down for a project.

Also if you are not using XHTML then the $selected = 'selected'

Hope this helps, sorry if it doesn't ^_^
Link to comment
Share on other sites

How are you aproaching this is it all on one page ie you select apple from the drop down box and it populates the feilds so you can edit and update or do you have a list some place with the items where you click on the item name ie lick on apples and it brings you to a page to edit apples. Im just a bit lost here is your problem populating the drop down box or poulating the fields after you have chosen what you want to edit
Link to comment
Share on other sites

If this what you are looking for?

[code]
function select_menu($name, $options, $selected) {
    $list = '';
    foreach ($options as $value => $text) {
        $is_selected = $value == $selected ? ' selected="selected"' : '';
        $list .= "  <option value=\"$value\"$is_selected>$text</option>\n";
    }
    return "<select name=\"$name\" id=\"$name\">\n$list</select>\n";
}



echo "Size:";

$options = array(
    'value for this option' => 'text displayed for this option',
    '1' => 'Small',
    '2' => 'Medium',
    '3' => 'Large',
);

$menu_name = 'ud_Size';
// grab selected_val from db
$selected_value = $Size;

echo select_menu($menu_name, $options, $selected_value);
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.