没有'echo'命令的日期是回声 - php

时间:2012-10-30 16:12:24

标签: php

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

用于定义日期下拉的代码

// 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);
?>

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
}            
}              
?>