Im trying to output a dropdown select menu from an array, and it all works fine, but the default option isnt working. Heres what ive got:

<?php
		$usertz = $user->$main["timezone"];

	foreach($timezonearray as $tz => $off)
	{
		echo "<option value='$tz'"; 
		if($tz == $usertz)
		{
			echo 'selected="selected"';
		}
		echo ">$tz";
		if(array_key_exists($tz, $timezonelabelarray))
		{
			echo " / ".$timezonelabelarray[$tz];
		}
		echo "</option>";
	}
	?>

$timezonearray, $timezonelabelarray, and $user are global vars on the server. $timezonearray is a list of timezones from GMT-11 to GMT+13. $timezonelabelarray is a name given to some of them (for example, GMT-5 is also EST). The value of $user->$main["timezone"] is the timezone that the user has selected that all their times be displayed in. This dropdown is supposed to display all the timezones, and default to the timezone that the user has selected to display. Everything works except for the default select. Any ideas?

simply echo word selected with space at line no 9 of your code
echo " selected "; //correct

DO NOT ASSIGN ANYTHING
echo 'selected="selected"' //incorrect

Thanks for the reply :)

simply echo word selected with space at line no 9 of your code
echo " selected "; //correct

DO NOT ASSIGN ANYTHING
echo 'selected="selected"' //incorrect

if($tz == $usertz)
		{
			echo " selected ";		
		}

It looks like this now and still doesn't work.

i m changing your line no 11 of first post, to verify whether tz=usertimezone. check of space and character case in $tz/$usertz. You may also want you use $off variable.

echo ">$tz (TZ={$tz},   USERTZ={$usertz})";

Hmmm...It appears that $usertz is blank...Thanks, thats good to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.