如何动态命名生成的 PHP 表单元格


How to dynamically name generated PHP table cells

我正在尝试制作一个每周日历应用程序,它是我正在设计的网页的一部分。我想实现一个编辑功能,允许管理员使用每个单元格中的文本区域编辑日历的内容。日历本质上只是一个表格。该表是根据数据库中的用户数动态生成的。我遇到的问题是尝试正确命名每个单元格,然后从该单元格中获取文本并将其放置在数据库中的正确行中

目前,我正在使用循环来尝试使用递增变量$i为每个单元格动态生成名称。

  for($i=0; $i < $rows; $i++){
        echo "<tr>";
        $q2 = mysqli_query($connect,"SELECT firstname FROM users WHERE id='".($i+1)."'");
        $q3 = mysqli_query($connect,"SELECT lastname FROM users WHERE id='".($i+1)."'");
        $qDays = mysqli_query($connect,"SELECT * FROM schedule WHERE id='".($i+1)."'");
        $rowDays = mysqli_fetch_assoc($qDays);
        $row2 = mysqli_fetch_assoc($q3);
        while($row = mysqli_fetch_assoc($q2)){
            $db_firstname = $row['firstname'];
            $db_lastname = $row2['lastname'];
            $sun = $rowDays['sun'];
            $mon = $rowDays['mon'];
            $tue = $rowDays['tue'];
            $wed = $rowDays['wed'];
            $thu = $rowDays['thu'];
            $fri = $rowDays['fri'];
            $sat = $rowDays['sat'];
            echo "<td align='center'> $db_lastname, $db_firstname </td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  name='sun.($i+1)'>$sun</textarea></td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  name='mon.($i+1)'>$mon</textarea></td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  name='tue.($i+1)'>$tue</textarea></td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  **name='wed.($i+1)'>$wed</textarea></td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  name='thu.($i+1)'>$thu</textarea></td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  name='fri.($i+1)'>$fri</textarea></td>";
            echo "<td td  height='50px' align='center'><textarea style='height:50px; resize:none; width:100%;'  name='sat.($i+1)'>$sat</textarea></td>";
        }

在我的程序的上述块中,我正在尝试动态命名(通过将字符串与变量 $i+1( 连接起来,然后用数据库中已有的文本填充文本框。

<input style='width:100px;' type='button' value='Confirm Edit' name='edit' class="edit_button">  
<?php
if(isset($_POST['edit'])){
        for($j=0; $j < $srows; $j++){        --srows is the number of rows in the DB
            $sun_post = @$_POST['sun.($j+1)'];
            $mon_post = @$_POST['mon.($j+1)'];
            $tue_post = @$_POST['tue.($j+1)'];
            $wed_post = @$_POST['wed.($j+1)'];
            $thu_post = @$_POST['thu.($j+1)'];
            $fri_post = @$_POST['fri.($j+1)'];
            $sat_post = @$_POST['sat.($j+1)'];
            mysqli_query($connect, "INSERT INTO schedule WHERE id='".($j+1)."' (`id`,`mon`,`tue`,`wed`,`thu`,`fri`,`sat`,`sun`) VALUES ('','".$mon_post."','".$tue_post."','".$wed_post."','".$thu_post."','".$fri_post."','".$sat_post."','".$sun_post."')");
    }
    }
?>


    </body>

在这个块中,我试图通过连接一个字符串与变量 $i+1 并将值插入数据库来再次获取动态生成的名称。我的问题是我如何正确动态命名文本区域,以便我可以从中获取数据以放入数据库,这可能吗?

简单的解决方案

我多年前就解决了同样的问题。您唯一可以做的可靠事情实际上是使用日期。

$startDay = date('N', date('Y-m-01')); // weekdayNum of the first of this month. 
// loop printing empty cells up to the first day in this month
// loop for month starts $day = numerical day this month.
$cellName = "day".$day

最终,您需要使用每个月通用的名称,以简化帖子页面。类似于那个月第四天的name="day04"。然后,您的数据库 ID 可以是时间戳。这是可靠地执行此操作的唯一方法,并且仍然允许支持超过一年的日历(作为您当前的方法(。

为了获取该时间戳,ID只需几个额外的隐藏字段来存储此特定条目的年份和月份。