0

我有一个每年更新的表单,因此用户输入日期和其他信息,然后提交存储在数据库中的表单。当他们一年后回来时,他们会做同样的事情来覆盖旧记录,问题是当他们回来时,旧日期会被回显,这是不应该发生的。所有其他字段都是空白的,这应该发生。我不是要求在代码中回显日期,但它仍然是。

定义日期下拉的代码

// parameters: $fname - main name of field
//             $date - actual date value from database
//             $beginYear - first value in year list
//             $endYear - last value in year list
//     return: none
function make_date_pulldown($fname, $date, $beginYear, $endYear)
{
  // read the date and break it up into $Year, $Month and $Day
  // so that we can set the "SELECTED" in the option list
  if ($date == ""){
    // set some default values to be safe
$Year = 0;
$Month = 0;
$Day = 0;
} else {
$Year = (int) substr($date,0,4);
$Month = (int) substr($date,5,2);
$Day = (int) substr($date,8,2);
}

// need to build a table around these guys so that there won't
// be any word wrap... it's going to be a 1 row by 5 cols.
echo "<table border=0 cellspacing=0 cellpadding=0>\n";
echo "<tr>\n";

// build month list
echo "  <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "  <center>Month<br>\n";
echo "<select name='month_$fname'>\n";
echo "  <option value='00'></option>\n";
for ($i=1;$i<=12;$i++){
printf ("  <option value='%02d'",$i);
if ($i == $Month) {
  printf (" SELECTED");
}
printf (">%02d</option>\n",$i);
}
echo "</select>\n";
echo "  </center>\n";
echo "  </td>\n";

echo "  <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "/";
echo "  </td>\n";

// build day list
echo "  <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "  <center>Day<br>\n";
echo "<select name='day_$fname'>\n";
echo "  <option value='00'></option>\n";
for ($i=1;$i<=31;$i++){
printf ("  <option value='%02d'",$i);
if ($i == $Day) {
  printf (" SELECTED");
}
printf (">%02d</option>\n",$i);
}
echo "</select>\n";
echo "  </center>\n";
echo "  </td>\n";

echo "  <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "/";
echo "  </td>\n";

// build year list
echo "  <td><font class=bluetext face=verdana,arial size=-2>\n";
echo "  <center>Year<br>\n";
echo "<select name='year_$fname'>\n";
echo "  <option value='0000'></option>\n";
for ($i=$beginYear;$i<=$endYear;$i++){
printf ("  <option value='%d'",$i);
if ($i == $Year) {
  printf (" SELECTED");
}
printf (">%d</option>\n",$i);
}
echo "</select>\n";
echo "  </center>\n";
echo "  </td>\n";

echo "</tr>\n";
echo "</table>\n";
}

网站上 PHP 表单的代码

<u>Date Of Submittal</u><br>
<?php
$startYear  =   date("Y")-1;
$endYear    =   date("Y")+1;
make_date_pulldown("submitdate", $submitdate, $startYear, $endYear);
?>
4

2 回答 2

0

想通了,在调用数据库表的 get_() 函数中,我正在读取 submitdate 的行,它正在根据该命令和 make_date_pulldown 函数提取和显示日期。

这是该功能的样子(显然是精简了):

<?
function get_submit($id, &$submitdate)
{                                                             
$query = "select * from submit where id = $id";
$result = mysql_query($query);                              
if ($result) {                                              
$total_rows = mysql_numrows($result);                     
} else { $total_rows = 0; }                                 
if ($total_rows > 0) {                                      
$submitdate = trim(mysql_result($result, "", "submitdate"));
} else {
$submitdate = "";
$found = 1;
}            
}              
?>             
于 2012-10-30T20:33:05.000 回答
0

看起来你想创建一个基于年份的下拉菜单......

$date = new DateTime();

//Years
echo makeSelect($date->format("Y")-1, $date->format("Y")+1,$date->format("Y"));

//Month
echo makeSelect(1, 12,$date->format("m"));

//Day
echo makeSelect(1, $date->format("t"),$date->format("d"));

使用的功能

function makeSelect($start, $end, $default = null) {
    $content = "<select>";
    $range = range($start, $end);
    $selected = "";
    foreach ( $range as $value ) {
        $selected = ($default == $value) ? "selected" : null;
        $content .= sprintf('<option value="%s" %s>%1$s</option>', $value, $selected);
    }
    $content .= "</select>";
    return $content;
}
于 2012-10-30T16:24:29.317 回答