【发布时间】:2018-12-30 22:27:18
【问题描述】:
我正在学习 PHP 和 AJAX。
我的任务:
处理日历通知。用户输入事件日期和之前的 3 个通知(事件前几天)。
我该怎么做:
写入、更新和存储事件日期和三个通知(以“前几天”的形式并在 DB 中计算通知日期。请参阅我的表结构:
这里“select1, select2, select3” 表示事件发生前的天数,day_remind1, day_remind2, day_remind3 表示计算和保存的通知日期。它的外观:
用户可以更改通知日期,因此我需要:创建可编辑的 HTML 表格,该表格可以在用户对第一个单元格进行更改后计算、更改并立即在第二个单元格中显示数据。
我的变种:
(简而言之,我只展示了我的项目中与问题相关的部分):
-
Days.php 绘制我的 HTML 表格:
<?php function showImen() { global $pdo, $loged, $day_remind1_updated, $day_remind2_updated, $day_remind3_updated; global $show, $id, $value, $k; if (!empty($loged)) { $stmt2 = $pdo->prepare("SELECT * FROM day_table WHERE login=?"); $stmt2->execute([$loged]); $show = $stmt2->fetchAll(); } } ?> <tbody> <?php foreach($show as $k=>$value) { $name = $value['name']); $surname = $value['surname']); ?> <div id="birthday-data"> <tr class="table-row"> <td aria-label="Name"> <?php echo $name;?></td> <td aria-label="Surname"><?php echo $surname;?></td> <td aria-label="Event date"><?php echo $value['birthday']; ?></td> <td aria-label="First notification" contenteditable="true" onBlur="saveToDatabase(this,'select1','<?php echo $show[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $show[$k]["select1"]; ?></td> <td id="one"><?php echo $value['day_remind1']; ?></td> <td aria-label="Second notification" contenteditable="true" onBlur="saveToDatabase(this,'select2','<?php echo $show[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $show[$k]["select2"]; ?></td> <td id="two"><?php echo $value['day_remind2']; ?></td> <td aria-label="Third notification" contenteditable="true" onBlur="saveToDatabase(this,'select3','<?php echo $show[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $show[$k]["select3"]; ?></td> <td id="three"><?php echo $value['day_remind3']; ?></td> <td align='center'> <span class='delete' id='del_<?php echo $show[$k]["id"]; ?>'><img id="delete_img" src="/download/a/img/delete_icon.png"></span> </td> </tr> -
Count.php(控制器)进行计算:
function countDaysNewData() { global $pdo, $loged; if (!empty($loged)) { $stmt = $pdo->prepare("SELECT select1, select2, select3, birthday FROM day_table WHERE login=?"); $stmt->execute([$loged]); $res = $stmt->fetchAll(); foreach ($res as $k => $value) { $arr = strtotime($res[0]['birthday']) - ($res[0]['select1'] * 86400); //0 element of 'select1' column $arr_to_date = date('Y-m-d', $arr); $arr2 = strtotime($res[0]['birthday']) - ($res[0]['select2'] * 86400); //0 element of 'select2' column $arr2_to_date = date('Y-m-d', $arr2); $arr3 = strtotime($res[0]['birthday']) - ($res[0]['select3'] * 86400); //0 element of 'select3' column $arr3_to_date = date('Y-m-d', $arr3); /* .. and so one. If user entered 3 events with 3 notification in each of them, here must be 9 manually written lines of code with same substarction ops - it is very unuseful of course, and that's the question */ } } echo $arr_to_date; echo $arr2_to_date; echo $arr3_to_date; ?> -
Show.php (view) 显示计算日期:
<?php require_once 'count.php'; countDaysNewData(); ?>
我的问题: 我的变体有效,但前提是数组中的元素数量恒定且很少。如果(如在我的任务中)用户可以在 DB 列中输入越来越多的元素,则这种方法不是最佳且错误的。
我应该如何首先循环 DB 以获取 4 个数组中的 4 列,然后循环每个数组,同时对其元素进行数学减法运算并保存结果?
【问题讨论】:
-
你有一个
foreach循环,但是你没有使用每个循环创建的值。如果您使用循环值而不是$res[0],那么您将不需要“9 行手动编写的代码” -
您想要实现的目标最好使用 JavaScript 来实现 - 使用响应式框架甚至更好,例如Vue.
-
@PatrickQ,抱歉,没理解这个想法。您能否举一个适当使用循环值的示例?
-
你有这个循环:
foreach ($res as $k => $value) {。你为什么不实际使用这些?你知道这个循环在做什么,对吧?循环的全部意义在于您不必一遍又一遍地编写相同的内容。
标签: php arrays loops date html-table