Jump to content

Setting a default on a drop down box


Shadowing

Recommended Posts

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>

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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>'; 

 

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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>

 

 

 

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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