Jump to content

Autoselecting a value in a dropdown menu


HaLo2FrEeEk

Recommended Posts

I'm revamping my sites administration panel and I put in a dropdown menu to select the section to administrate (it was a LOT better than the list of links I had before, I'll probably update it some more though) and I want to know how I can autoselect whatever page I'm on in the dropdown menu, for example, here's the menu:

 

Administrate:

News

Roster

 Update Roster

 Update Emblems

Click Counter

Pending Affiliates

 

If I select Update Roster, I want it to automatically have Update Roster selected when I load that page.  Basically I've got the indx.php which has a large switch statement that handles all the different actions that can be taken, the switch statement uses include to call up the corresponding file, which also has a switch statement to handle all the different functions of that particular section.  Roster has Update and Update Emblems, and Add a new member; Click Counter has Add a new link, delete link, reset to zero, etc.  I want the page I'm on to be the selection in the dropdown menu.  How can I use php to do this?

Link to comment
https://forums.phpfreaks.com/topic/66468-autoselecting-a-value-in-a-dropdown-menu/
Share on other sites

Well, if its all going through index.php and you pass an action to it through the URL, then it would be something like:

 

<?php
$action = $_GET['action'];
$pages = array('news','roster');//fill the array with all of your pages
echo '<select name="page">';
foreach($pages as $v){
echo "<option value='$v'";
echo $action==$v? "selected='selected'": "";
echo ">$v</option>";
}
echo '</select>';
?>

 

Makes it much easier by putting all the possible pages in an array.

 

So that means that if I update and add functionality to the admin index page, then I have to update the array too.  This is way complicated.  Is there any way to make it so I don't have to have all the values in 3 different places?  For example:

 

index.php:

switch ($action) {
  case "news":
    include('./news.php');
    break;
  case "news_insert":
    include('./news.php');
    break;
  etc...

 

news.php:

switch ($_REQUEST['action']) {
  case "news_insert":
    // Code to insert from the form below
    break;
  default:
    // Form to submit news
    break;
  etc...

 

Array in index.php:

$c = array('news', 'news_insert', etc...);

 

There's a lot to go wrong there, any way I can make it a better system?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.