gpt4 book ai didi

PHP回显从表单中选择的多个

转载 作者:行者123 更新时间:2023-11-29 03:32:34 25 4
gpt4 key购买 nike

创建.php

我做的第一件事是创建并将每一个发送到我的表中它自己的列。

    $subCategoryDirectory = $_POST["subCategoryDirectory"];
$subCategoryDirectory_1 = $subCategoryDirectory[0];
$subCategoryDirectory_2 = $subCategoryDirectory[1];
$subCategoryDirectory_3 = $subCategoryDirectory[2];
$subCategoryDirectory_4 = $subCategoryDirectory[3];
$subCategoryDirectory_5 = $subCategoryDirectory[4];


+----------------------+------------------------+------------------------+------------------------+------------------------+--+
| subCategoryDirectory | subCategoryDirectory_2 | subCategoryDirectory_3 | subCategoryDirectory_4 | subCategoryDirectory_5 | |
+----------------------+------------------------+------------------------+------------------------+------------------------+--+
| selection | selection2 | selection3 | selection4 | selection5 | |
+----------------------+------------------------+------------------------+------------------------+------------------------+--+
| | | | | | |
+----------------------+------------------------+------------------------+------------------------+------------------------+--+
| | | | | | |
+----------------------+------------------------+------------------------+------------------------+------------------------+--+

update.php

更新时,我调用每个变量并将其分配给一个变量。

$selectUpdate = "SELECT * from table where id=$id LIMIT 1";
$selectUpdateDisplay = $wpdb->get_results($selectUpdate, ARRAY_A);

foreach ($selectUpdateDisplay as $sqlStuf){
$cat1 = $sqlStuf[subCategoryDirectory];
$cat2 = $sqlStuf[subCategoryDirectory_2];
$cat3 = $sqlStuf[subCategoryDirectory_3];
$cat4 = $sqlStuf[subCategoryDirectory_4];
$cat5 = $sqlStuf[subCategoryDirectory_5];
}

所以我有上面的表格,我想在更新时将其回显到下面的选择表单中作为“已选择/突出显示”。现在,当我更新时,我丢失了初始上传的值。

<select id="subCategoryDirectory" name="subCategoryDirectory[]" style="width:100%;" multiple>
<option value="<?php echo $cat1; ?>"><?php echo $cat1; ?></option>
<optgroup label="Advertising & Media">
<option value="Advertising &amp; Marketing">Advertising &amp; Marketing</option>
<option value="Advertising, Marketing, Promotions">Advertising, Marketing, Promotions</option>
</optgroup>
<optgroup label="Transportation">
<option value="Delivery Service">Delivery Service</option>
<option value="Limousine Service ">Limousine Service </option>
<option value="Taxicab">Taxicab</option>
<option value="Transportation">Transportation</option>
</optgroup>
</select>

每次我尝试 echo 时,我都会得到一个单一的值,正如您所想象的那样。没有一个被选中。我是否应该测试以查看每个变量是否为空并进行长时间的测试?我是不是想多了?

UPDATE

array(1) {
[0]=>
array(32) {
["id"]=>
string(3) "112"
["nameDirectory"]=>
string(14) "test to delete"
["nameDirectoryAbv"]=>
string(4) ""
["descriptionDirectory"]=>
string(14) ""
["summaryDirectory"]=>
string(14) ""
["addressDirectoryStreet"]=>
string(11) ""
["addressDirectorySuiteNumber"]=>
string(0) ""
["addressDirectoryNumber"]=>
string(5) ""
["addressDirectoryPrefix"]=>
string(0) ""
["addressDirectoryTwo"]=>
string(0) ""
["zipDirectory"]=>
string(5) ""
["websiteDirectory"]=>
string(21) ""
["facebookDirectory"]=>
string(21) ""
["twitterDirectory"]=>
string(21) ""
["linkedinDirectory"]=>
string(21) ""
["emailDirectory"]=>
string(13) ""
["contactNameDirectory"]=>
string(4) ""
["phoneDirectory"]=>
string(12) ""
["faxDirectory"]=>
string(12) ""
["categoryDirectory"]=>
string(0) ""
["subCategoryDirectory"]=>
string(23) "Advertising & Marketing"
["subCategoryDirectory_2"]=>
string(34) "Advertising, Marketing, Promotions"
["subCategoryDirectory_3"]=>
string(0) ""
["subCategoryDirectory_4"]=>
string(0) ""
["subCategoryDirectory_5"]=>
string(0) ""
["cityDirectory"]=>
string(11) ""
["stateDirectory"]=>
string(2) ""
["hoursDirectory"]=>
string(13) ""
["image_1"]=>
string(80) ""
["image_2"]=>
string(92) ""
["image_3"]=>
string(85) ""
["image_4"]=>
string(85) ""
}
}

最佳答案

为您的 subCategoryDirectories 创建一个数组:

$cat = array($sqlStuf[subCategoryDirectory_1],$sqlStuf[subCategoryDirectory_2], ...);

除了 is_set 之外,在循环中使用数组(foreachfor)将允许您仅显示已设置的数组:

foreach:

<?php foreach ($cat as $item) { if (isset($item)) { echo "<option value=\""; echo "$item\">$item</option>\n"; } } ?>

对于:

<?php for ($i = 0; $i <= 4;) { if (isset($cat[$i])) { echo "<option value=\""; echo "$cat[$i]\">$cat[$i]</option>\n"; } $i++; } ?>

关于PHP回显从表单中选择的多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28402375/

25 4 0