473,434 Members | 1,409 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,434 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 2006
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.