关于简单待办事项列表的PHP if-else问题
待办事项清单: 我已经创建了HTML和php代码来将数据插入数据库,并从中读取数据。 数据库如下所示:关于简单待办事项列表的PHP if-else问题,php,mysql,Php,Mysql,待办事项清单: 我已经创建了HTML和php代码来将数据插入数据库,并从中读取数据。 数据库如下所示: task_id |task_desc |task_target |task_finished_date |task_status 40 |test p |2020-07-25 |2020-07-23 |completed $finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHE
task_id |task_desc |task_target |task_finished_date |task_status
40 |test p |2020-07-25 |2020-07-23 |completed
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date < task_target ");
$finished_failed_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date > task_target ");
任务目标和任务完成日期在数据库中使用日期类型。
task_target在创建任务时获得值,task_finished_date在我们使用now()标记作业已完成时自动获得值:
当某项任务在“失败”表中显示的截止日期后完成,并且if在完成表中显示的截止日期前关闭时,我当前正在尝试对其进行编程
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd < $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
我试着获取截止日期(task_target)和目标日期(task_finished_date)并将它们插入两个变量中,然后进行比较,并根据比较结果将它们显示在我想要的位置,但我认为这里的逻辑不正确
如何根据所需条件分离任务
完整代码如下:
<?php
// initialize errors variable
$errors = "";
// connect to database
$db = mysqli_connect("localhost", "root", "", "todo");
// insert a quote if submit button is clicked
if (isset($_POST['submit'])) {
if (empty($_POST['task_desc'])) {
$errors = "You must fill in the task";
}else{
$task_desc = $_POST['task_desc'];
$task_target = $_POST['task_target'];
$sql = "INSERT INTO tasks (task_desc, task_target) VALUES ('$task_desc', '$task_target')";
mysqli_query($db, $sql);
header('location: index.php');
}
}
// delete task
if (isset($_GET['del_task'])) {
$task_id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE task_id=".$task_id);
header('location: index.php');
}
if(isset($_GET['completed'])){
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
// select all tasks if page is visited or refreshed
$tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'Active' ");
?>
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<title>ToDo List Application PHP and MySQL</title>
</head>
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-12">
<div class="heading">
<h2 style="font-style: 'Hervetica';">ToDo List Application PHP and MySQL database</h2>
</div>
<form method="post" action="index.php" class="input_form">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
<label for="task_target">Select Date:</label>
<input type="date" class="form-control" name="task_target">
<label for="task_desc">Describe your task:</label>
<textarea class="form-control" name="task_desc" id="body" cols="30" rows="5"></textarea>
<button type="submit" name="submit" id="add_btn" class="form-control">Add Task</button>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Ongoing Tasks</th>
<th>Target Date</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row['task_desc']; ?> </td>
<td> <?php echo $row['task_target']; ?> </td>
<td>
<a href="index.php?del_task=<?php echo $row['task_id'] ?>">Delete</a>
</td>
<td>
<a href="index.php?completed=<?php echo $row['task_id'] ?>">Complete</a>
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
?>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Successful Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php } ?>
</div>
</div>
</div>
</body>
</html>
N
成功的任务
任务完成时间
N
失败的任务
任务完成时间
你的问题很含糊,你得到了什么样的错误或意外的输出?不得不去掉你的代码中的HTML部分和PHP部分,而不影响逻辑,以下是我得到的
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
$i++;
}
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
}
$i++;
}
}
我设法找到了一个简单的办法来解决我的问题。 首先,感谢所有试图帮助我并建议有效学习方法的人。 我真的很喜欢这个地方。 因此,关于答案: 正如在begging中所描述的,任务的截止日期是在创建任务的过程中定义的,而关闭时间是在用户单击Completed按钮时到达数据库的时间。 因此,我决定将排序直接放入数据库,修改如下查询:
task_id |task_desc |task_target |task_finished_date |task_status
40 |test p |2020-07-25 |2020-07-23 |completed
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date < task_target ");
$finished_failed_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date > task_target ");
$finished_tasks=mysqli_query($db,“从任务中选择*,其中任务状态='completed'和任务完成日期任务目标”);
通过这种方式,我在$finished_tasks变量中设置了所有成功完成的任务,在$finished_failed_tasks中设置了所有失败的任务,之后在页面中的显示就很容易了。
我不太确定这是否是一个切实可行的解决方案,但以这种方式工作,我希望。
下一步是按照您的建议剥离和重构代码。
再一次,非常感谢,祝你有一个愉快的一天。
尊敬的Kristiyan谢谢,这不会上传到公众。仅用于学习目的。首先,尽量不要将PHP和HTML混合使用。在显示HTML之前准备好数据。不管是为谁准备的。您有一个bug,需要尽快修复。“谢谢,这不会上传到公共网站。只用于学习目的。”–Kristiyan Rumenov“然后建议您以正确的方式学习。嗨,Samuel,似乎逻辑没有像我想象的那样起作用。我没有错误,只是没有按预期工作。在我看来,它必须是这样的:读取数据库的每一行并比较两个日期,然后根据比较将每一行标记为成功或失败。我自己很难获得代码的逻辑流,这是因为不健康的PHP和HTML混合。使代码很难阅读和理解。让我再试一次。