Hi
I'm hoping you can point me in the direction I should be taking as I'm confusing myself at the moment!
I have two tables in a database. One is a list of articles, one is a list of categories for those articles.
I have a form for the user to enter a new article which pulls the list of categories and displays it in a select box for the user to choose as they add the article. This then submits the chosen category name to the table for the list of articles along with the text and title etc.
That all seems to work fine.
I'm getting myself confused as to how best to organise the edit form for these articles.
The Edit form pre-populates a form with the details taken from the articles table including currently a simple input box with the previously chosen category included all from the articles table.
But I'm not sure how to combine that with the previously used select list of categories from the existing category list AND have the chosen category showing in the select box.
How do I show the select box options of all the categories from the category list with the category in the article list showing?
Any advice welcomed!
Different table data confusion
Moderator: General Moderators
if you build the options from the category list in code and just use an if statment to compare the category name from the edit details to the query you can put a selected keyword in the html option tag.
something like this maybe:
hope this helps a bit.
something like this maybe:
Code: Select all
//connect to database
//get categories
$categories = mysql_query(...);
$list_options = '';
while ($row = mysql_fetch_assoc($categories)){
$category_name = $row['category_name'];
//compare this name and the one from the edit results
if ($category_name == $selected_category){
$list_options .= '<option value="{$row['category_id']}" selected>$category_name</option>';
} else {
$list_options .= '<option value="{$row['category_id']}">$category_name</option>';
}
}
echo '<select name="list">'.$list_options.'</select>';