Jump to content
Larry Ullman's Book Forums

Drop Down Menu Issue


Recommended Posts

I am wondering if anyone can help me out with this.

On the CMS script I now have the capability to edit any page or category. I have a drop-down menu where the administrator can change the category that the page will be placed under. My problem is when I edit a page I would like the category drop-down menu to have the category of the page that it is under (from the database) already selected. I copied the code from the "add_page.php" file to my new "edit_page.php" file but when I go to edit the page the category is not selected only the text "Select One" is. Though I am able to click on a category from the drop-down menu to choose a category I would like the one that page is classified under to show automatically. If I forget to enter a category when I edit a page a "0" is automatically placed under the "category_id" column in the database and the page is unable to be viewed.

Here is the drop-down menu code that retrieves the categories from the database.

  <p><label for="category"><strong>Category</strong></label><br />
<select name="category_id"<?php if (array_key_exists('category', $add_page_errors)) echo ' class="error"'; ?>>
<option>Select One</option>
<?php // Retrieve all the categories and add to the pull-down menu:
$q = "SELECT id, category FROM categories ORDER BY category ASC";			
$r = mysqli_query ($dbc, $q);
	while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
		echo "<option   value=\"$row[0]\"";
		// Check for stickyness:
		if (isset($_POST['category_id'])  ) echo ' selected="selected"';
		echo ">$row[1]</option>\n";
	}
?>
</select><?php if (array_key_exists('category', $add_page_errors)) echo ' <span class="error">' . $add_page_errors['category'] . '</span>'; ?></p>

 

Many thanks for any help in advance.

Link to comment
Share on other sites

During the loop that creates the select options you need to compare $row[0] against that ID and add the selected="selected" snippet if that is the case. You'll need to use an if else to cater for form stickiness too.

  • Upvote 1
Link to comment
Share on other sites

During the loop that creates the select options you need to compare $row[0] against that ID and add the selected="selected" snippet if that is the case. You'll need to use an if else to cater for form stickiness too.

 

Hi Stuart, thank you for your help.

I have tried different combinations with no luck. I noticed something though, I only had one table selected (categories) I think I would need also to select the other table (pages) so a comparison could be made. But when I did add the other table I got multiple choices of the same item in the drop down menu. That is I have three of everything in the drop-down menu. And even that did not get the category selected. Here is the updated code I used thinking it would work but didn't.

 <select name="category_id"<?php if (array_key_exists('category', $add_page_errors)) echo ' class="error"'; ?>>
       <option>Select One</option>
       <?php // Retrieve all the categories and add to the pull-down menu:
       $q = "SELECT categories.id, categories.category, pages.category_id FROM categories, pages ORDER BY category ASC";                       
       $r = mysqli_query ($dbc, $q);
               while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
                       echo "<option   value=\"$row[0]\"";
                       // Check for stickyness:
                       if (isset($_POST['categories.id']) && ($_POST['pages.category_id'] == $row[0]) ) echo ' selected="selected"';
                       echo ">$row[1]</option>\n";
               }
       ?>
       </select>

 

This is how the two table are set up :

 

TABLE 'categories'

('id', 'category')

 

TABLE 'pages'

(id, category_id, title, description, content, date_created)

 

Any thoughts?

 

thanks again.

Link to comment
Share on other sites

No, you don't want to select the other table there. That query just gets the list of categories. Your edit script should have a separate query that retrieves all the information for the page being edited. That query should also be selecting the page's current category ID. It's this value you would compare to within the loop to determine which row gets pre-selected. Once you get that working, you can then add that code that watches for "stickyness".

Link to comment
Share on other sites

 Share

×
×
  • Create New...