dt3999 2017-10-04 04:07
浏览 67
已采纳

如果不是当前日期,请将SQL日期变为红色

I'm creating a simple PHP to do list. I want to turn the date red if it's not the current date. Here's part of my code, with the HTML removed:

<?php include "connection.php";?>

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

$task = isset($_POST['task']) ? $_POST['task'] : null;
$importance = isset($_POST['importance']) ? $_POST['importance'] : null;
$due_date = isset($_POST['due_date']) ? $_POST['due_date'] : null;
$order = isset($_POST['order']) ? $_POST['order'] : null;

if(isset($task,$importance,$due_date)){
$sql = "INSERT INTO tasks (task, importance, due_date) VALUES ('$task', '$importance', '$due_date')";

$result = mysqli_query($connection, $sql);

if(!$result){
  die("Database query failed.");
}

}

}

if(isset($order)){
$sql = "SELECT * FROM tasks ORDER BY {$order}";
} else {
$sql = "SELECT * FROM tasks";
}
$result = mysqli_query($connection, $sql);

if(!$result){
  die("Database query failed.");
}

$current_date = date("Y-m-d");
if(isset($due_date)!=$current_date){
  $due_date_class="overdue";
} else {
  $due_date_class="not_overdue";
}

?>

<table>
<?php
while($column = mysqli_fetch_assoc($result)){
?>
<tr><td><?php echo $column["task"]?></td><td class="<?php echo $column["importance"] ?>"><?php echo $column["importance"]?></td><td class="<?php echo $due_date_class ?>"><?php echo $column["due_date"]?></td><td><?php echo "<a href='delete_one.php?id=".$column['id']."'>Delete</a>" ?></td></tr>
<?php
}
?>
</table>

<?php include "footer.php";?>

This is the part that isn't working. It's either turning all the rows red or black, depending on what date I enter:

$current_date = date("Y-m-d");
    if(isset($due_date)!=$current_date){
      $due_date_class="overdue";
    } else {
      $due_date_class="not_overdue";
    }
  • 写回答

1条回答 默认 最新

  • dongraa1986 2017-10-04 04:16
    关注
    isset($due_date)
    

    should be changed to

    $due_date
    

    Because, isset will return value 1 or 0, which will be compared with date, which is going to be false all the time.

    Instead, you could do following:

    if(isset($due_date))
    {
    $current_date = date("Y-m-d");
        if($due_date!=$current_date){
          $due_date_class="overdue";
        } else {
          $due_date_class="not_overdue";
        }
    }
    else
    {
       echo "Not a valid Due Date";
    }
    

    >>>Demo<<<


    Regarding variable scoping.

    You need to declare $due_date_class at very top, to increase its scope, otherwise you will get error. Since, scope of this variable will be limited to if and else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?