[英]Assigning DIVs to php variables and looping on multiple hidden inputs
我有一個內置的CMS,允許用戶創建一個由不同面板組成的“頁面”,每個面板都有自己的內容。 我做了這項工作,以便用戶可以創建一個包含一個面板和一個文本區域內容的頁面,但是我仍然不知道如何對多個面板/內容執行此操作。
當前,如果頁面加載從URL值中獲取值“ 1”,則它將加載全角div和半角div的html模板。 我試圖用隱藏的輸入類型設置每個面板的類型,它們每個都有自己的tinymce文本區域。
<?php if($value == 1){?>
<form method="post">
<div class="col-lg-12 fullWidth" id="full">
<input type="hidden" name="fullWidth" value="">
<div class="fullContent" style="background-color: white; height: 100%;">
<form id="form-data3" method="post">
<textarea class="original" id="mytextarea3" name="fullText">Some Text Here</textarea>
<input type="submit" value="Save Content">
</form>
</div>
</div>
<div class="col-lg-12 halfWidth" id="half">
<input type="hidden" name="halfWidth" value="">
<div class="halfContent" style="background-color: white; height: 100%;">
<form id="form-data4" method="post">
<textarea class="original" id="mytextarea4" name="halfText">Some Text There</textarea>
<input type="submit" value="Save Content">
</form>
</div>
</div>
</form>
<?php } ?>
完成此操作后,他們進入保存頁面,還有另一種形式,可以讓他們設置頁面的標題並獲得頁面類型(上面的$ value)
<form action="addPage.php" method="post">
<input type="hidden" name="pageType" value="<?php echo $value;?>">//This comes from the url value
<input class="form-control" id="addTitle" name="addTitle">
<input type="submit" name="Save Page">
問題是,當我現在調用addPage.php腳本插入記錄時,我不知道如何正確傳遞值,因此我添加了一個頁面記錄(page_type_id和Title的$ value),然后插入2文本區域的內容和面板記錄。
在上述代碼的情況下,這是我的預期插入:
pages
ID | Title | page_type_id
1 | TitleNew | 1 /*this comes from $value*/
content
ID | Content
1 | Some Text Here
2 | Some Text There
panels
ID | panel_type_ID | page_id | content_id
1 | 1 | 1 | 1
2 | 2 | 1 | 2
這適用於所有3個表中的一個插入,但是如果我可以為每個div設置多個面板類型,我如何修改它以仍然插入一頁記錄但成功地考慮了多個面板和內容?
這是添加頁面腳本
//Insert Page
$title = $_POST['addTitle'];
$page_type = $_POST['pageType'];
$addpage = "
INSERT INTO pages (title, page_type_id)
VALUES ('$title','$page_type');
";
$mysqlConn->query($addpage)
$page_id = $mysqlConn->insert_id;
//Insert Content
$content = $_POST['page_content'];
$addContent = "
INSERT INTO content(content)
VALUES('$content');
";
$mysqlConn->query($addContent);
$cont_id = $mysqlConn->insert_id;
//Insert panel(s)
$panelID = $_POST['panelType'];
$addPanel = "
INSERT INTO panels(panel_type_id, page_id, cont_id)
VALUES ('$panelID', '$page_id', '$cont_id');
";
$mysqlConn->query($addPanel);
我想您的問題是處理多個數據收集。 在元素名稱中使用[]可以收集同一集合的多個數據。
<textarea class="original" id="mytextarea3" name="fullText[]">Some Text Here</textarea>
並且在保存的同時從fullText獲得數組。 循環瀏覽並保存。
如果您有多個相同格式的文檔。 在第一個循環中使用鍵訪問其他元素數組中的相應數據。
希望能幫助到你!!
您管理錯誤,應該是:
pages
ID | Title | page_type_id
1 | TitleNew | 1 /*this comes from $value*/
content
ID | Content
1 | Some Text Here
2 | Some Text There
panels
ID | panel_type_ID | page_id | content_id
1 | 1,2 | 1 | 1,2
我在您的表單中找不到panelType
,但是,您應該檢測用戶選擇的panels Ids
,然后將其發送到php
,例如:
如果用戶選擇面板(1)和面板(2),則您的腳本應在此處編寫:
<input type="hidden" value="1,2">
我們用逗號分隔panels Ids
,因為從database
獲取數據時將需要它。
如何獲取panels IDs
:
從database
獲取數據后,它將變為1,2
,這對您不起作用,那么如何將它們放入數組中?
這樣做很簡單:
<?php
$string = "1,2";
$get = explode(',', $string ); // explode with ,
?>
現在,您將擁有如下所示的數組:
Array ( [0] => 1 [1] => 2 )
如何使用它?
您可以使用循環:
<?php
$string = "1,2";
$get = explode(',', $string ); // explode with and
$count = count($get);
for ($i=0; $i < $count; $i++) {
$PaN = $get[$i];
/*Some code....*/
}
?>
我希望我能理解你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.