9 Replies - 380 Views - Last Post: 03 October 2013 - 03:09 PM Rate Topic: -----

#1 patk570  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-February 13

Getting selected timezone from database

Posted 03 October 2013 - 01:39 PM

Hi Everyone, I have a timezone variable that is stored in my database. In my code, I have it generate every timezone there is but what I need is to have it return the results of the selected="selected" when you go to pull the edit page up so you don't have to search 100's of different timezones to get yours again..

Here is the timezone code

    <?php
    function get_timezones()
    {
    $o = array();
    $t_zones = timezone_identifiers_list();
    foreach($t_zones as $a)
    {
    $t = '';
    try
    {
    //this throws exception for 'US/Pacific-New'
    $zone = new DateTimeZone($a);
    $seconds = $zone->getOffset( new DateTime("now" , $zone) );
    $hours = sprintf( "%+02d" , intval($seconds/3600));
    $minutes = sprintf( "%02d" , ($seconds%3600)/60 );
    $t = $a ." [ $hours:$minutes ]" ;
    $o[$a] = $t;
    }
    //exceptions must be catched, else a blank page
    catch(Exception $e)
    {
    //die("Exception : " . $e->getMessage() . '<br />');
    //what to do in catch ? , nothing just relax
    }
    }
    ksort($o);
    return $o;
    }
    $o = get_timezones();
    ?>


and to display the results:
    <label>Timezone</label>
    <select class="input" name="timezone">
    <?php
    foreach($o as $tz => $label)
    {
    echo "<option value=".$tz.">$label</option>";
    }
    ?>
    </select><br><br>


I need for the selected to go here somehow:
    <label>Timezone</label>
    <select class="input" name="timezone">
    <?php
    foreach($o as $tz => $label)
    {
    echo "<option selected='selected' value=".$tz.">$label</option>";
    }
    ?>
    </select><br><br>


Is This A Good Question/Topic? 0
  • +

Replies To: Getting selected timezone from database

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,675
  • Joined: 30-January 09

Re: Getting selected timezone from database

Posted 03 October 2013 - 01:52 PM

The way I generally do this is test for the selected value per loop, like so:
<label>Timezone</label>
<select class="input" name="timezone">
<?php
$dbValue = // Get the value from the database
foreach($o as $tz => $label)
{
$selected = ($tz == $dbValue ? "selected='selected'" : "");
echo "<option $selected value='$tz'>$label</option>";
}
?>
</select><br><br>


This post has been edited by e_i_pi: 03 October 2013 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 patk570  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-February 13

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:00 PM

Hello and thanks for the quick reply,

I've tried that before, and when it comes back all it says is zulu +0:00

Here is how i have it structured

<label>Timezone</label>
<select class="input" name="timezone">
<?php
	$dbValue= $row['timezone'];
    foreach($o as $tz => $label)
	$selected = ($tz == $dbValue ? "selected='selected'" : "");
    {
        echo "<option $selected  value=".$tz.">$label</option>";
    }
?>
</select><br><br>



and here is the source code of that selection:


<label>Timezone</label>
<select class="input" name="timezone">
<option   value=Zulu>Zulu  [ +0:00 ]</option></select><br><br>



Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,675
  • Joined: 30-January 09

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:08 PM

What format is your database value, and what format are the values from the $o array? Also, you need single quotes around the value value in your HTML.
Was This Post Helpful? 0
  • +
  • -

#5 patk570  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-February 13

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:11 PM

$query = mysqli_query($con,"SELECT * FROM settings WHERE id = '1' ");
    $row = mysqli_fetch_array($query);


I am using mysqli for this.
Was This Post Helpful? 0
  • +
  • -

#6 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,675
  • Joined: 30-January 09

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:14 PM

Not your query string, but the value you're testing with (i.e. - $dbValue) and against (i.e. = $tz / $label)
Was This Post Helpful? 0
  • +
  • -

#7 patk570  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-February 13

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:23 PM

In the database it says America/Denver

$row['timezn'] = America/Denver

$dbValue = $row['timezn'];
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8903
  • View blog
  • Posts: 33,383
  • Joined: 12-June 08

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:34 PM

Moving to PHP.
Was This Post Helpful? 0
  • +
  • -

#9 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,675
  • Joined: 30-January 09

Re: Getting selected timezone from database

Posted 03 October 2013 - 02:50 PM

Ok, so you've got to get the 'America/Denver' value matching against the right part of the array, whether that be $tz or $label.
Was This Post Helpful? 0
  • +
  • -

#10 patk570  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 13-February 13

Re: Getting selected timezone from database

Posted 03 October 2013 - 03:09 PM

I made it easier and better, and not it works.

<label>Time Zone:</label><select name="timezone">
<?
$timezns = timezone_identifiers_list();
foreach ($timezns as $timezn) {
echo '<option';
if ( $timezn == $row['timezn'] ) echo ' selected';
echo ' value="' . $timezn . '">' . $timezn . '</option>' . "\n";
}
?>
</select>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1