如何插入和回显生日下拉值

时间:2012-12-20 00:09:17

标签: php codeigniter drop-down-menu

我正在为我的网站上的用户个人资料构建编辑页面。我正在使用codeigniter并且无法知道在哪里使用以及如何从我的表单助手插入生日下拉菜单。我也不确定我目前的情况是否正确。

此外,我希望回应用户为每个字段选择的值,以便在他们回来再次编辑其个人资料时保存。

以下是我的助手创建下拉列表的代码:

function month_dropdown($month="month", $top_months=array()) {
    $months = array(
            "choose"=>"Month",
            "----------",
            "Jan"=>"Jan",
            "Feb"=>"Feb",
            "Mar"=>"Mar",
            "Apr"=>"Apr",
            "May"=>"May",
            "Jun"=>"Jun",
            "Jul"=>"Jul",
            "Aug"=>"Aug",
            "Sep"=>"Sep",
            "Oct"=>"Oct",
            "Nov"=>"Nov",
            "Dec"=>"Dec"
    );

    $html = "<select name='{$months}'>";
    if(!empty($top_months)){
        foreach($top_months as $value){
            if(array_key_exists($value, $months)){
                $html .="<option value='{$value}'>{$months[$value]}</option>";
            }
        }
        $html .="<option>----------</option>";
    }
    foreach($months as $key => $month){
        $html .="<option value='{$key}'>{$month}</option>";
    }
    $html .="</select>";
    return $html;

}

function day_dropdown($day="day", $top_days=array()) {
    $days = array(
            "choose"=>"Day",
            "----------",
            "1"=>"1",
            "2"=>"2",
            "3"=>"3",
            "4"=>"4",
            "5"=>"5",
            "6"=>"6",
            "7"=>"7",
            "8"=>"8",
            "9"=>"9",
            "10"=>"10",
            "11"=>"11",
            "12"=>"12",
            "13"=>"13",
            "14"=>"14",
            "15"=>"15",
            "16"=>"16",
            "17"=>"17",
            "18"=>"18",
            "19"=>"19",
            "20"=>"20",
            "21"=>"21",
            "22"=>"22",
            "23"=>"23",
            "24"=>"24",
            "25"=>"25",
            "26"=>"26",
            "27"=>"27",
            "28"=>"28",
            "29"=>"29",
            "30"=>"30",
            "31"=>"31"
    );

    $html = "<select name='{$days}'>";
    if(!empty($top_days)){
        foreach($top_days as $value){
            if(array_key_exists($value, $days)){
                $html .="<option value='{$value}'>{$days[$value]}</option>";
            }
        }
        $html .="<option>----------</option>";
    }
    foreach($days as $key => $day){
        $html .="<option value='{$key}'>{$day}</option>";
    }
    $html .="</select>";
    return $html;

}

function year_dropdown($year="year", $top_years=array()) {
    $years = array(
            "choose"=>"Year",
            "----------",
            "1997"=>"1997",
            "1996"=>"1996",
            "1995"=>"1995",
            "1994"=>"1994",
            "1993"=>"1993",
            "1992"=>"1992",
            "1991"=>"1991",
            "1990"=>"1990",
            "1989"=>"1989",
            "1988"=>"1988",
            "1987"=>"1987",
            "1986"=>"1986",
            "1985"=>"1985",
            "1984"=>"1984",
            "1983"=>"1983",
            "1982"=>"1982",
            "1981"=>"1981",
            "1980"=>"1980",
            "1979"=>"1979",
            "1978"=>"1978",
            "1977"=>"1977",
            "1976"=>"1976",
            "1975"=>"1975",
            "1974"=>"1974",
            "1973"=>"1973",
            "1972"=>"1972",
            "1971"=>"1971",
            "1970"=>"1970",
            "1969"=>"1969",
            "1968"=>"1968",
            "1967"=>"1967",
            "1966"=>"1966",
            "1965"=>"1965",
            "1964"=>"1964",
            "1963"=>"1963",
            "1962"=>"1962",
            "1961"=>"1961",
            "1960"=>"1960",
            "1959"=>"1959",
            "1959"=>"1959",
            "1958"=>"1958",
            "1957"=>"1957",
            "1956"=>"1956",
            "1955"=>"1955",
            "1954"=>"1954",
            "1953"=>"1953",
            "1953"=>"1953",
            "1952"=>"1952",
            "1951"=>"1951",
            "1950"=>"1950",
            "1949"=>"1949",
            "1948"=>"1948",
            "1947"=>"1947",
            "1946"=>"1946",
            "1945"=>"1945",
            "1944"=>"1944",
            "1943"=>"1943",
            "1942"=>"1942",
            "1941"=>"1941",
            "1940"=>"1940",
            "1939"=>"1939",
            "1938"=>"1938",
            "1937"=>"1937",
            "1936"=>"1936",
            "1935"=>"1935",
            "1934"=>"1934",
            "1933"=>"1933",
            "1932"=>"1932",
            "1931"=>"1931",
            "1930"=>"1930"

    );

    $html = "<select name='{$years}'>";
    if(!empty($top_years)){
        foreach($top_years as $value){
            if(array_key_exists($value, $years)){
                $html .="<option value='{$value}'>{$years[$value]}</option>";
            }
        }
        $html .="<option>----------</option>";
    }
    foreach($years as $key => $year){
        $html .="<option value='{$key}'>{$year}</option>";
    }
    $html .="</select>";
    return $html;

}

以下是更新查询中我的模型中的部分内容: * 编辑 *

$ birthdate = $ this-&gt; input-&gt; post('month')。 “/”。 $ this-&gt; input-&gt; post('day')。“/”。$ this-&gt; input-&gt; post('year');'birthday'=&gt; $这 - &GT;输入 - &GT;交($出生日期),

此处显示在我的视图中: * 编辑 *

echo month_dropdown('month')." ".day_dropdown('day'). " ".year_dropdown('year');

提前致谢

1 个答案:

答案 0 :(得分:0)

我在功能方面做了一些改变。

我建议在 $ top_month 中,而不是发送数组,更好地发送所选值。

我认为在 $ month 您发送选择框的名称,所以最好使用它而不是使用 $ months

此外,我已经使代码在选择框中显示所选值。

function month_dropdown($month="month", $top_month='' ) {

   $months = array(
        "choose"=>"Month",
        "Jan"=>"Jan",
        "Feb"=>"Feb",
        "Mar"=>"Mar",
        "Apr"=>"Apr",
        "May"=>"May",
        "Jun"=>"Jun",
        "Jul"=>"Jul",
        "Aug"=>"Aug",
        "Sep"=>"Sep",
        "Oct"=>"Oct",
        "Nov"=>"Nov",
        "Dec"=>"Dec"
  );

  $html = "<select name='{$month}'>";

  foreach($months as $key => $month){

      $selected = "";
      //this will match for selected value and set the selected attribute
      if( $key == $top_month ) {
          $selected = "selected='selected'";
      }
      $html .="<option value='{$key}' $selected>{$month}</option>";
  }
  $html .="</select>";
  return $html;

}

function day_dropdown($day="day", $top_days="") {
   $days = array(
        "choose"=>"Day",
        "1"=>"1",
        "2"=>"2",
        "3"=>"3",
        "4"=>"4",
        "5"=>"5",
        "6"=>"6",
        "7"=>"7",
        "8"=>"8",
        "9"=>"9",
        "10"=>"10",
        "11"=>"11",
        "12"=>"12",
        "13"=>"13",
        "14"=>"14",
        "15"=>"15",
        "16"=>"16",
        "17"=>"17",
        "18"=>"18",
        "19"=>"19",
        "20"=>"20",
        "21"=>"21",
        "22"=>"22",
        "23"=>"23",
        "24"=>"24",
        "25"=>"25",
        "26"=>"26",
        "27"=>"27",
        "28"=>"28",
        "29"=>"29",
        "30"=>"30",
        "31"=>"31"
   );

   $html = "<select name='{$day}'>";

   foreach($days as $key => $day){
      $selected = "";
      if( $key == $top_days ) {
          $selected = "selected='selected'";
      }

      $html .="<option value='{$key}' $selected >{$day}</option>";
   }
   $html .="</select>";
   return $html;

}

function year_dropdown($year="year", $top_years='') {
   $years = array(
        "choose"=>"Year",
        "1997"=>"1997",
        "1996"=>"1996",
        "1995"=>"1995",
        "1994"=>"1994",
        "1993"=>"1993",
        "1992"=>"1992",
        "1991"=>"1991",
        "1990"=>"1990",
        "1989"=>"1989",
        "1988"=>"1988",
        "1987"=>"1987",
        "1986"=>"1986",
        "1985"=>"1985",
        "1984"=>"1984",
        "1983"=>"1983",
        "1982"=>"1982",
        "1981"=>"1981",
        "1980"=>"1980",
        "1979"=>"1979",
        "1978"=>"1978",
        "1977"=>"1977",
        "1976"=>"1976",
        "1975"=>"1975",
        "1974"=>"1974",
        "1973"=>"1973",
        "1972"=>"1972",
        "1971"=>"1971",
        "1970"=>"1970",
        "1969"=>"1969",
        "1968"=>"1968",
        "1967"=>"1967",
        "1966"=>"1966",
        "1965"=>"1965",
        "1964"=>"1964",
        "1963"=>"1963",
        "1962"=>"1962",
        "1961"=>"1961",
        "1960"=>"1960",
        "1959"=>"1959",
        "1959"=>"1959",
        "1958"=>"1958",
        "1957"=>"1957",
        "1956"=>"1956",
        "1955"=>"1955",
        "1954"=>"1954",
        "1953"=>"1953",
        "1953"=>"1953",
        "1952"=>"1952",
        "1951"=>"1951",
        "1950"=>"1950",
        "1949"=>"1949",
        "1948"=>"1948",
        "1947"=>"1947",
        "1946"=>"1946",
        "1945"=>"1945",
        "1944"=>"1944",
        "1943"=>"1943",
        "1942"=>"1942",
        "1941"=>"1941",
        "1940"=>"1940",
        "1939"=>"1939",
        "1938"=>"1938",
        "1937"=>"1937",
        "1936"=>"1936",
        "1935"=>"1935",
        "1934"=>"1934",
        "1933"=>"1933",
        "1932"=>"1932",
        "1931"=>"1931",
        "1930"=>"1930"

   );

   $html = "<select name='{$year}'>";

   foreach($years as $key => $year){

      $selected = "";
      if( $key == $top_years ) {
          $selected = "selected='selected'";
      }

     $html .="<option value='{$key}' $selected >{$year}</option>";
   }
   $html .="</select>";
   return $html;

 }

我认为这会解决你的疑虑。如果有的话,请告诉我。