473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Populating a Select with options from an array

Alright im having an issue trying to get the months in the select. Can anyone tell me what I'm doing wrong here
Expand|Select|Wrap|Line Numbers
  1. $arrMonths = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  2.  
  3. <select name="txtDOHMonth">
  4.     <?php
  5.         for ( $i=0; $i<count($arrMonths); $i++ )
  6.         {
  7.             $i2 = $i + 1;
  8.             $t1 = "<option value=\"".$i2."\" "; 
  9.             if ( ( isset($_POST['txtDOHMonth']) ) && ( $arrMonths[$i] == $_POST['txtDOHMonth'] ) ) { $t1 .= "selected=\"true\" "; }
  10.             $t1 .= ">".$arrMonths[$i]."</option>";
  11.             echo $t1;
  12.         }
  13.     ?>
  14. </select>
  15.  
Feb 17 '09 #1
2 2004
Markus
6,050 Expert 4TB
Urgh! You can do it much cleaner. Here:

Expand|Select|Wrap|Line Numbers
  1. // We set a variable to avoid multiple calls to the count() function (quicker).
  2. $count = count( $arrMonths );
  3. // For loop through the array - using ++$i (instead of $i++) - generally quicker.
  4. for ( $i = 0 ; $i < $count ; ++$i )
  5.         // @ symbol surpress error, so we can skip isset()
  6.         // does the current POST value match our current array index?
  7.     if ( @$_POST['txtDOHMonth'] == $arrMonths[$i] )
  8.     {
  9.                 // echo out the option with the 'select' attribute.
  10.         echo "<option value='{$arrMonths[$i]}' selected='selected'>{$arrMonths[$i]}</option>";
  11.     }
  12.     else
  13.     {
  14.                 // otherwise, echo the option without the 'selected' attr.
  15.         echo "<option value='{$arrMonths[$i]}'>{$arrMonths[$i]}</option>";
  16.     }
  17.  
  18. }
  19.  
Feb 17 '09 #2
dlite922
1,584 Expert 1GB
or if using Smarty, it can be done in one line!

Expand|Select|Wrap|Line Numbers
  1. {html_options options=$myArray selected=$selectedNode}
So Short, huh!?



Dan
Feb 19 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Suzanne | last post by:
Hi, I have a form which our clients can fill in with their personal details. As part of the information we store there is a section - areas of interest - this is a number of radio buttons. ...
4
by: point | last post by:
Hello there... I'm a PHP programmer and starting to learn JS... I have a following problem.... I have 3 select boxes! one is hotel one is destination and one is country... if someone...
4
by: bobsawyer | last post by:
I've been building a series of SELECT lists that are populated dynamically using HTTPRequest. Things are going pretty well, and I've got the whole thing working flawlessly in Mozilla/Firebird....
3
by: Stewart | last post by:
Dear comp.lang.javascript, I have more than once wanted to manipulate the contents of select boxes dynamically, whilst the boxes contain <optgroup> tags. Manipulation of a select box containing...
2
by: Billy Smith | last post by:
How do you go about populating a select list from an XML file? I can open the XML file fine and get at all of the data, but I'm stuck on how to use that data in my <option> tags. Is it even...
8
by: jack-b | last post by:
Hi, I have a list box which displays countries names and a second listbox which displays their cites (based on the selection made in ListBox 1) If the user selects USA i want to display cities...
1
by: Stephene | last post by:
New to the world of web design/php/mysql and need help please. What I'm trying to do: I would like a web page with three drop down menus each populated by a query The first represents...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
1
by: chandhseke | last post by:
Hi Folks, I have designed a dynamic drop down list but having problems since it is not working as intended. Please help <html> <head> <Script language="Javascript"> var Select = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.