Shadowing Posted January 19, 2012 Share Posted January 19, 2012 having a problem figuring out how to change the default on a drop down box to equal the users current timezone. So when they view it the drop box default is showing what their setting is atm. The problem is the value is differant from whats displayed. Is it possible to some how say if users time offset is equal to a option value set selected=""> to yes. if not what is the idea behind this? Cant find a guide showing an example In other drop down boxes where the value is the same as whats displayed I just simply created another value field at the top of the list and echo it in. <select name="gmt"> <option value="-28800">(GMT -0800) Pacific Time </option> <option value="-25200">(GMT -0700) Mountain Time </option> <option value="-21600">(GMT -0600) Central Time</option> <option value="-18000">(GMT -0500) Eastern Time </option> </select> Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 19, 2012 Share Posted January 19, 2012 <option value="-28800" <?php echo $user_timezone == '-28800' ? 'selected="selected"' : ''; ?>>(GMT -0800) Pacific Time </option> <option value="-25200" <?php echo $user_timezone == '-25200' ? 'selected="selected"' : ''; ?>>(GMT -0700) Mountain Time </option> <option value="-21600" <?php echo $user_timezone == '-21600' ? 'selected="selected"' : ''; ?>>(GMT -0600) Central Time</option> <option value="-18000" <?php echo $user_timezone == '-18000' ? 'selected="selected"' : ''; ?>>(GMT -0500) Eastern Time </option> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2012 Share Posted January 19, 2012 I'll add to scootsah's example that you will make your life much easier if you set the values in an array and then create the options in a loop. Makes your code much more flexible. You can hard code the array in the current script, another file or even get it from the database. Plus, you don't have to create ALL those lines of code. When you do the same thing over and over again it is very easy to make a mistake on one or more instances and then spend a lot of time trying to find the cause. Example: $timezones = array( '-28800' => '(GMT -0800) Pacific Time', '-25200' => '(GMT -0700) Mountain Time', '-21600' => '(GMT -0600) Central Time', '-18000' => '(GMT -0500) Eastern Time', ); foreach($timezone as $value => $label) { $selected = ($user_timezone == $value) ? ' selected="selected"' : ''; echo "<option value="{$value}"{$selected}>{$label}</option>\n"; } Quote Link to comment Share on other sites More sharing options...
Shadowing Posted January 19, 2012 Author Share Posted January 19, 2012 Thanks alot scootstah that was exactly what I was looking for. now i need to figure out how to do it on my populating drop down boxes. thanks Psycho. Shouldnt I try to use html when ever possible though for performance? "i mean by not echoing in the html" Im using an array for my populating drop down boxes like this foreach($name as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; } I tried to use your example scootstah on this and it didnt work. $current1['name'] is the default I want the box to be set too I dont understand it looks to me to be the same concept. foreach($name as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($current1['name'] == $current1['name'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2012 Share Posted January 19, 2012 thanks Psycho. Shouldnt I try to use html when ever possible though for performance? "i mean by not echoing in the html" I've never heard of any such issues and I don't see how that is different from what you were trying to do above. But the problem with what you were trying to do is the format of the code is wrong and your condition. The condition is set as $current1['name'] == $current1['name'] You are comparing the same value to itself and that would always return true. But the code you just posted does not make sense based upon your first post. In the first post you have an offset number as the option value and a description as the option label. But, the last code you provided shows you using the exact same variable for the option value and the option label. I can't really provide any updated code without knowing the format of the data you are using (i.e. the array format and how the offset and description are stored). Quote Link to comment Share on other sites More sharing options...
Shadowing Posted January 19, 2012 Author Share Posted January 19, 2012 Sorry Psycho The 2nd code is another drop down box not related to the first one. Its just a drop down box displaying names from the database. On screen refresh im trying to make the drop down box remember the name selected while having a active session. So I have the name stored in a session when a name is selected This is the full code for the drop down box being populated with names. Wanting to get $_SESSION['planet'] to be the default in the drop down box. $result = mysql_query("SELECT name,address FROM planets WHERE id ='".($_SESSION['user_id']."' ORDER BY name ASC")) or die(mysql_error()); $name = array(); while($raw = mysql_fetch_array( $result )) { $name[] = $raw; } echo '<form method="post" action="">'; echo '<select name="name_list" id="name_list" >'; foreach($name as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; i guess i figured $_SESSION['planet'] == $_SESSION['planet'] was the same as what scootstah was doing here on the first dropdown box example <option value="-28800" <?php echo $user_timezone == '-28800' ? 'selected="selected"' : ''; ?>>(GMT -0800) Pacific Time </option> Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 19, 2012 Share Posted January 19, 2012 You should be able to just replace $_POST['name_list'] with $_SESSION['planet'] Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2012 Share Posted January 19, 2012 Do you need to do the same thing for other select lists? If so, it would be easy to create a function for creating your list items. Just pass the funtion the list of values/labels and the selected value. function createSelectOptions($valuesAry, $selectedValue=false, $useIdAsValue=false) { $output = "": foreach($valuesAry as $id => $label) { $value = ($useIdAsValue) ? $id : $label; $selected = ($value === $selectedValue) ? ' selected="selected"' : ''; $output .= "<option value='{$value}'{$selected}>{$label}</option>\n"; } return $output; } Quote Link to comment Share on other sites More sharing options...
Shadowing Posted January 19, 2012 Author Share Posted January 19, 2012 Thanks scootstah its working now. I actually tried the oppsite of that at first. I havnt even started using "call to a functions" yet thanks Psycho for that example. since im using this name list on several pages this should probably be the day I finally make my first call to function page. Maybe im just scared to move to php level 3. Then again this is how I felt about arrays before i learn how to use them and now I use arrays all the time. one thing that always kinda concern me about using a external function page is that its hard to see whats going on in the page since the code isnt on the page. Since im still new to this. but then again its not to much more differant then using external CSS pages Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 19, 2012 Share Posted January 19, 2012 Use a good IDE and it will help a lot. You will be able to see the parameters and such for external functions. And if you use PHPDoc comments you can see all that too. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2012 Share Posted January 19, 2012 Use good function names and make your functions do specific things - not a whole bunch of things. Then if you load a page and see an empty select list you can open the script for that page and you would see that there is a function call to a function such as createSelectOptions. You would then know that the problem is either with the data passed to the function or with the function itself. 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.