【问题标题】:How to make arithmetic ops on elements of fetched from DB arrays in PHP?如何对从 PHP 中的 DB 数组中提取的元素进行算术运算?
【发布时间】:2018-12-30 22:27:18
【问题描述】:

我正在学习 PHP 和 AJAX。

我的任务:

处理日历通知。用户输入事件日期和之前的 3 个通知(事件前几天)。

我该怎么做:

写入、更新和存储事件日期和三个通知(以“前几天”的形式并在 DB 中计算通知日期。请参阅我的表结构:

这里“select1, select2, select3” 表示事件发生前的天数,day_remind1, day_remind2, day_remind3 表示计算和保存的通知日期。它的外观:

用户可以更改通知日期,因此我需要:创建可编辑的 HTML 表格,该表格可以在用户对第一个单元格进行更改后计算、更改并立即在第二个单元格中显示数据。

我的变种:

(简而言之,我只展示了我的项目中与问题相关的部分):

  1. 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>
    


  2. 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;
         ?>
    
  3. Show.php (view) 显示计算日期:

    <?php
     require_once 'count.php';
     countDaysNewData();
    ?>
    

我的问题: 我的变体有效,但前提是数组中的元素数量恒定且很少。如果(如在我的任务中)用户可以在 DB 列中输入越来越多的元素,则这种方法不是最佳且错误的。

我应该如何首先循环 DB 以获取 4 个数组中的 4 列,然后循环每个数组,同时对其元素进行数学减法运算并保存结果?

【问题讨论】:

  • 你有一个foreach 循环,但是你没有使用每个循环创建的值。如果您使用循环值而不是 $res[0],那么您将不需要“9 行手动编写的代码”
  • 您想要实现的目标最好使用 JavaScript 来实现 - 使用响应式框架甚至更好,例如Vue.
  • @PatrickQ,抱歉,没理解这个想法。您能否举一个适当使用循环值的示例?
  • 你有这个循环:foreach ($res as $k =&gt; $value) {。你为什么不实际使用这些?你知道这个循环在做什么,对吧?循环的全部意义在于您不必一遍又一遍地编写相同的内容。

标签: php arrays loops date html-table


【解决方案1】:

在编程中,循环重复执行相同的事情。在您的情况下,您正在使用循环来处理记录中的数据:

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

    }
 }

让我们在这里识别模式。每次迭代都需要两行。您在任何地方都使用 $res[0] 和生日。你这里改的是select末尾的数字,比如select1,select2,...

然后将它们全部转换为时间,然后转换为date。让我们的循环变得有用:

$index = 1;
$dateArray = array();
while (isset($res[0][$key = 'select'.$index])) {
    $dateArray[($index++)] = date('Y-m-d', strtotime($res[0]['birthday']) - ($res[0][$key] * 86400));
}

现在你有一个$dateArray,它可以自己循环:

foreach ($dateArray as $index => $value) {
    //$index is the number and $value is the date
}

您的数据库格式不理想,您可能需要阅读database normalization 以改进您的架构。

【讨论】:

  • 感谢您的详细解答。不知道为什么,但是 while 循环有问题。服务器等待刷新页面,回答 504 错误。我做错了什么? (只需复制您的代码并回显一个数组..)
  • @aysee 那是我的错误。忘记在每次迭代中增加 $index 。我已经编辑了我的答案来解决这个问题,将 (isset($res[0][$key = 'select'.$index])) 更改为 (isset($res[0][$key = 'select'.($ index++)])) 以便在每次迭代中增加 $index 。 $index++ 返回 $index 的旧值并增加它,所以它的旧值在下一次迭代中会更高。
  • 明白。非常感谢!
  • @aysee 自您上次发表评论以来,我已对代码进行了进一步修复,请使用新代码。 $index 应该在最后一次使用循环后递增。
  • 美好的一天!试图根据我的情况修改您的代码。在其原始形式中,它只允许从第一行获取数据(因为我想是 res[0] 索引)。所以我尝试以这种形式重写循环:while (isset($res[$index++][$key = 'select'.$index])) { $dateArray[($index++)] = date('Y-m-d', strtotime($res[$index++]['birthday']) - ($res[$index++][$key] * 86400)); ,等待脚本将循环 res[0] , res [1] 等......但不幸的是这是不对的。请您评论一下,我应该使用什么方法来解决我的问题?
猜你喜欢
  • 2012-06-19
  • 2015-06-02
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多