带日期的选项菜单显示为1969-12-31

时间:2016-07-10 05:49:59

标签: php mysql

我有一个选项菜单,可以在有要显示的记录时正常工作。我有时会有一个新项目仍然没有记录,我希望选项菜单不显示任何日期。相反它显示1969-12-31。

<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
    <?php
do {  
?>
    <option value="<?php echo $row_RecordsetDate['date']?>"<?php if   
($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?   
php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); 
    ?></option>
    <?php
} while ($row_RecordsetDate = mysql_fetch_assoc($RecordsetDate));
$rows = mysql_num_rows($RecordsetDate);
if($rows > 0) {
  mysql_data_seek($RecordsetDate, 0);
  $row_RecordsetDate = mysql_fetch_assoc($RecordsetDate);
 }
 ?>
</select>

如果没有记录或只是说明没有可用日期,如何让选项菜单显示为空白?

2 个答案:

答案 0 :(得分:0)

<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
    while($row = mysql_fetch_assoc($RecordsetDate)){
        if(isset($row['date'])){
             if(strlen($row['date'])){
                 echo '<option value="'.$row['date'].'"';
                 if($varDate_Recordset1 == $row['date']){
                    echo 'selected';
                 }
                 $date = date('l, F d, Y',strtotime($row['date']));
                 echo '>'.$date.'</option>';
             }
        }
    }
?>
</select>

答案 1 :(得分:0)

通过反复试验,我提出了以下解决方案。我在选项值中放了一个if else语句。

 <option value="<?php echo $row_RecordsetDate['date']?>"<?php if ($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); $rows = mysql_num_rows($RecordsetDate); if(!empty($rows)){
                echo $dates; 
            }else{
          echo ""; }?></option>

以下是完整的解决方案。

<select name="selDate" id="selDate" onchange="formDate.submit()">
      <option value="%">all dates</option>
            <?php
    do {  
    ?>
            <option value="<?php echo $row_RecordsetDate['date']?>"<?php if ($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); $rows = mysql_num_rows($RecordsetDate); if(!empty($rows)){
                echo $dates; 
            }else{
          echo ""; }?></option>
            <?php
    } while ($row_RecordsetDate = mysql_fetch_assoc($RecordsetDate));
      $rows = mysql_num_rows($RecordsetDate);
      if($rows > 0) {
          mysql_data_seek($RecordsetDate, 0);
          $row_RecordsetDate = mysql_fetch_assoc($RecordsetDate);
      }
    ?>
        </select>