簡體   English   中英

如何動態命名生成的PHP表單元格

[英]How to dynamically name generated PHP table cells

我正在嘗試制作一個每周日歷應用程序,這是我正在設計的網頁的一部分。 我想實現一種編輯功能,使管理員能夠使用每個單元格中的textareas編輯日歷的內容。 日歷本質上只是一張桌子。 該表是根據數據庫中有多少用戶動態生成的。 我遇到的問題是嘗試正確命名每個單元格,然后從該單元格中獲取文本並將其放置在數據庫的正確行中

目前,我正在使用循環嘗試使用遞增變量$ 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"類似“ name="day04" 然后,您的數據庫的ID可以是一個時間戳。 這是可靠地執行此操作的唯一方法,並且仍然允許支持超過一年的日歷(作為當前方法)。

為了獲得該時間戳ID只需幾個額外的隱藏字段即可存儲此特定條目的年和月。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM