views:

691

answers:

4

I know this is a similar question to one already asked, but I was hoping for a different answers to a problem. I have a form where you can upload new articles to a database all fine works ace, wonderful, brill. The form uses a drop down menu for the type of article, I have news, gossip, travel, performances and others in my drop down box. When you upload to the db it's only going to add the selected item let's say, in this case, News.

I need the user to be able edit their articles, so I need to populate the drop down box with the selected item. I think it's a bit of a pain in the BUM to set up a database table with each of the drop items and an ID for them. Is there away of setting the selected item as value in the db, and then just using a if statement or something to populate the rest of the drop down box?

Hope that makes sense. I also thought use of a radio buttons could be a solution however I cant get that to set the selected button with the value in the db.

+1  A: 

Sounds like you want to put the article types in a table or something along those lines.

Then you can load the drop-down from the table, and just store a reference to the article type ID in the article table.

On the plus it should make retrieving all articles of a given type quite a bit faster as you'd be doing the lookup on ID.

Andrew Barrett
Yes, that sounds logical.
Cerebrus
+2  A: 

You could do something like this:

// array of available options
$available = array('news'=>'News', 'gosip'=>'Gosip', 'travel'=>'Travel', 'performances'=>'Performance');
// selected option value
$selected = 'news';
echo '<select name="article-type">';
foreach ($available as $val => $label) {
    echo '<option value="'.htmlspecialchars($val).'"';
    if ($val == $selected) {
        echo ' selected="selected"';
    }
    echo '>'.htmlspecialchars($label).'</option>';
}
echo '</select>';
Gumbo
A: 
$options = array(,);
$selection = filter_input(INPUT_POST, 'what');
$blah = true; //i don't know what to name this.
foreach($options as $option) {
    echo '....';
    if(isset($blah) && $option === $selection) {
        echo 'selected="selected"';
        unset($blah);
    }
}

This is probably the fastest way. Also if you're going for speed make sure to take advantage of echo's multiple parameters.

orlandu63
+1  A: 

It sounds to me like this is more a database design issue than anything else. Your basic design seems flawed. I'm trying to deduce the issue from your question so bear with me if I'm way off track.

Here is a relevant portion of the design, as it should be, IMO:

Table: Articles


ArticleID
ArticleContent
ArticleSubject (FK from Subjects)
..
..
.. Other columns.

Table: Subjects


SubjectID
SubjectDescription
..
..
.. Other columns.

Note that I called the second table Subjects to better illustrate my implication. This way, each Article has a reference to an article type (subject). Now when you retrieve data for the "Article types" dropdownlist, you would simply retrieve all the rows in the second table, and then would set the selected item based on the current selection (from the first table.)

Cerebrus