Jump to content

Populating dropdown list

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Slick

Slick

    Newbie

  • Members
  • Pip
  • 5 posts
I am trying to pick up some knowledge of web programming through tutorials and such. I wanted to expand on a tutorial I saw online as to help myself see all the connections and relationships etc.

One thing was a form. I wanted to be able to make the day in a drop down list populate according to the month. I can get everything working fine, just not the changing of the days to coincide with the months.

Doing some googling I found this code:


function days_of_month($month = false){

$aDateTime = getdate();

for ($i = 1; $i < 13; $i++){

switch ($i){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

$aMonthDays[$i] = 31;

break;

case 4:

case 6:

case 9:

case 11:

$aMonthDays[$i] = 30;

break;

case 2:

if (checkdate($i, 29, $aDateTime["year"])) {

$aMonthDays[$i] = 29;

} else {

$aMonthDays[$i] = 28;

}

break;

}

}

if ($month){

return $aMonthDays[$month];

} else {

return $aMonthDays;

}

}

but without any info to go along with it. I can see what it is doing. It is quite simple in what it does and does what pictured in my mind, just syntactically correct, I guess. My problem is I do not know how to implement this function into the actual page. Particularly, I dont exactly understand the parameters of the function. I know I basically have to do onChange(days_of_monthXXXX) type of deally, but Im not sure exactly how to go about it Any insight is appreciated.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Let me spiff up the code a little with formatting, it is always a good idea:
function days_of_month($month = false){
   $aDateTime = getdate();
   for ($i = 1; $i < 13; $i++){
      switch ($i){
         case 1:
         case 3:
         case 5:
         case 7:
         case 8:
         case 10:
         case 12:
         $aMonthDays[$i] = 31; 
         break;
         case 4:
         case 6:
         case 9:
         case 11:
         $aMonthDays[$i] = 30;
         break;
         case 2:
         if (checkdate($i, 29, $aDateTime["year"])) {
             $aMonthDays[$i] = 29;
         } else {
             $aMonthDays[$i] = 28;
         }
         break;
      }
   }
   if ($month){
      return $aMonthDays[$month];
   } else {
      return $aMonthDays;
   }
}
Essentially $month is FALSE by default. This means it will get the current month (June) through getdate() and list the days in the current month if no parameter is passed.

If you want to get a certain month, say April you'd do this:

$month = 4; // April
$daysInMonth = days_of_month($month);
As you see, it will pass 4 (April) to the function and fall through the CASE statements, going to the appropriate number of days. The RETURN statement simply returns the proper number of days, fetched from the function statement.

EDIT: Onchange? You'll need AJAX for to call the PHP function from within JS.

#3
Slick

Slick

    Newbie

  • Members
  • Pip
  • 5 posts
Sorry, my wording wasnt 100% clear. What it was is that I want the days field in the dropdown list selection to populate based on the month that is selected in its dropdown list selection.

I already have a working function that will list however many days I want in the field so all I need to do is to be able to call the month selection and put it into a variable so I can use that function. I guess that is really what I dont understand how to do.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

Slick said:

Sorry, my wording wasnt 100% clear. What it was is that I want the days field in the dropdown list selection to populate based on the month that is selected in its dropdown list selection.

I already have a working function that will list however many days I want in the field so all I need to do is to be able to call the month selection and put it into a variable so I can use that function. I guess that is really what I dont understand how to do.

Sorry, I didn't read it all the way through, it was late. You may want to just go ahead and do it in JS, look for Rajganesh's example down this page:
Need help to populate dropdownlist using Javascript - ASP.NET Forums

if you want to do it in PHP with that function you'd need to use AJAX (a JS call to the PHP code) to get the number of days dynamically, those are the two options.