RE: selecting current month from a database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



$cur_month = date("m");
while ($r = mysql_fetch_array($month_query))
{
	$v = $r["m_id"];
      $out = $r["months"];
	if( $cur_month = $v )
	{
		echo("<option  selected=".$cur_month.
"value=".$v.">".$out."</option>")
	}
	....
}




Jef

-----Original Message-----
From: Paul Novitski [mailto:paul@xxxxxxxxxxxxxxxxxxx] 
Sent: Sunday, May 21, 2006 2:55 AM
To: php-general@xxxxxxxxxxxxx
Subject: Re:  selecting current month from a database

At 11:45 PM 5/20/2006, Paul Goepfert wrote:
>I have a drop down menu where I have the
>months of year as menu items.  I want to be able to have the current
>month be the selected month.  I have tried using the date function as
>the way to set the current month as the selected value but it seems
>that every value entered in the select box is set to selected.
>
>Here is my code maybe someone can help me out
>
>$month_query = mysql_query("SELECT m_id, months FROM Month");
>         while ($r = mysql_fetch_array($month_query))
>         {
>                 $v = $r["m_id"];
>                 $out = $r["months"];
>                 echo("<option  selected=" . 
> date("F") . "value=$v>$out</option>\n");
>         }
>
>Just incase you want to know
>
>m_id = 1..12
>months = January..December
>
>How do I set only the current month to selected ?


Paul,

The syntax for pre-selected options is:

HTML:  <option selected>            [1]
XHTML: <option selected="selected"> [2]

Options that are not pre-selected simply omit the 
selected attribute altogether.

Therefore you'll want code something like this:

         while ($r = mysql_fetch_array($month_query))
         {
                 $v = $r["m_id"];        // numeric month
                 $out = $r["months"];    // verbal month

                         // select option for current month
                         if ($v == date("n"))
                         {
                                 $selected = 'selected="selected"';
                         }else{
                                 $selected = '';
                         }

                 echo "<option $selected value="$v">$out</option>\n";
         }

Notes:

- date("F") is the verbal month; date("n") is 
numeric [3].  Therefore you'll want to compare $v 
with date("n") or $out with date("F").

- Adhering to XHTML syntax, I've put the attribute values in quotes. [2]

- FYI, you can harmlessly include parentheses in 
the echo statement but echo is actually not a 
function and doesn't require parens [4].

Regards,
Paul
________________________

[1]
W3C HTML 4.01 Specification
17 Forms
17.6.1 Pre-selected options
http://www.w3.org/TR/html4/interact/forms.html#h-17.6.1

[2]
XHTML(tm) 1.0 The Extensible HyperText Markup Language (Second Edition)
4. Differences with HTML 4
4.4. Attribute values must always be quoted
http://www.w3.org/TR/xhtml1/#h-4.4

[3]
PHP Manual
VI. Function Reference
CLI. String Functions
echo
http://php.net/echo

[4]
PHP Manual
VI. Function Reference
XXII. Date and Time Functions
date
http://php.net/date

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux