anilm Posted January 14, 2015 Share Posted January 14, 2015 Hi. I have 2 problems which I'm hopeing someone can help me with. 1. For the following script to change the pages language, I would like it so that whichever option selected is set on the page (i.e. if you select Euskal, the select box stays set as Euskal, and not Espanyol as it does at the moment) <form name="Language" action="" method="post" id="Language"> <div class="btn-group"> <?php $uri = strtok($_SERVER["REQUEST_URI"],'?');; $url="http://".$_SERVER['HTTP_HOST'].$uri; ?> <span class="btn btn-primary"> <select name="lang" onchange="javascript:change_langs(this.form)"> <option value="Esp">Espanyol</option> <option value="Eng">English</option> <option value="Cat">Catalan</option> <option value="Bas">Euskal</option> <option value="Gal">Galego</option> </select> </span> </div></form> 2. I have 2 dropdown lists in the following format: <select class="form-control" name='category'> <option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="A"))?"selected='A'":""); ?> value='A'><?=getlg($lang,"A")?></option> <option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="b"))?"selected='b'":""); ?> value='b'><?=getlg($lang,"b")?></option> <option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="c"))?"selected='c'":""); ?> value='c'><?=getlg($lang,"c")?></option> </select> <select class="form-control" name='category'> <option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="D"))?"selected='D'":""); ?> value='D'><?=getlg($lang,"D")?></option> <option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="e"))?"selected='e'":""); ?> value='e'><?=getlg($lang,"e")?></option> <option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="f"))?"selected='f'":""); ?> value='f'><?=getlg($lang,"f")?></option> </select> However, I require just one dropdown list with catagories A and D as the main item. Catagories b & c should be a sub category of A, and e & f should be a sub category of D Any help would be appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 14, 2015 Solution Share Posted January 14, 2015 1. Store selected language in a session variable and use the "selected" attribute to select the stored language 2. Use <optgroup> to group the subcats into categories session_start(); if (isset($_GET['lang'])) { $_SESSION['lang'] = $_GET['lang']; // store newly selected lang } if (!isset($_SESSION['lang'])) { $_SESSION['lang'] = 'Esp'; // set default if not yet set } // LANGUAGE OPTIONS $langs = array( "Esp" => "Espanyol", "Eng" => "English", "Cat" => "Catalan", "Bas" => "Euskal", "Gal" => "Galego", ); $langOpts = ''; foreach ($langs as $k => $l) { $sel = ($k == $_SESSION['lang']) ? 'selected="selected"' : ''; $langOpts .= "<option value='$k' $sel> $l</option>\n"; } // CATEGORY OPTIONS $cats = array ( "A" => array ("b", "c"), "D" => array ("e", "f") ); $catOpts = ''; foreach ($cats as $c => $subs) { $catOpts .= "<optgroup label='$c'>\n"; foreach ($subs as $sc) { $catOpts .= "<option value='$sc'> $sc</option>\n"; #$catOpts .= "<option value='$sc'> $sc</option>\n"; } $catOpts .= "</optgroup>\n"; } ?> <html> <head> <style type="text/css"> option.subcat { padding-left: 25px; } </style> </head> </html> <form> Language <select name="lang" onchange="javascript:change_langs(this.form)"> <?= $langOpts ?> </select> <br> Category <select name="category"> <option value=''>Select ...</option> <?= $catOpts ?> </select> <input type="submit" name="btnSub" value="Submit"> </form> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 14, 2015 Share Posted January 14, 2015 (edited) I would make one slight tweak to what Barand provided. I would still use a session value to save the user's language selection, but I wouldn't use that variable when determining which language to use for the output. Instead I would determine separately 1) What language to use and 2) What is the user's selected language. Here's why: The fact that a user has not selected a language may be an important piece of data. Yes, you should have a default language to fall back on, but sometimes you may need confirmation that the user has selected a language. For example, if you just showed pages in a specific language that the user is not familiar with it may be difficult for them to understand what items to select to change it. Instead, you may provide them with a prompt when they visit your site if they have no solved selection that includes text in all the supported languages asking them to make a selection. So, I would do something such as this: session_start(); //Set the default presentation language $presLang = 'Esp'; if (isset($_GET['lang'])) { $_SESSION['lang'] = $_GET['lang']; // store newly selected lang } if (isset($_SESSION['lang'])) { $presLang = $_SESSION['lang']; // set presentation language if user selected } Then, use $presLang when determining which language to create output in. When showing a select list for the user to choose a language I would use the session value and add a "Select Language" option if they have not previously selected one. $langOpts = "<option value=''>- Select a Language -</option>\n"; foreach ($langs as $k => $l) { $sel = ($k == $_SESSION['lang']) ? 'selected="selected"' : ''; $langOpts .= "<option value='$k' $sel> $l</option>\n"; } Edited January 14, 2015 by Psycho Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.