创建.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];
}

因此,我有上面的表,我想在更新时将其作为下面的“selected/highlighted”回显到select表单中。现在,当我更新时,我丢失了最初上传的值。
<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>

每次我尝试回声,我得到一个单一的价值,你可以想象。没有选择任何一个。我应该测试一下每个变量是否都是空的,然后做很长的一段路吗?我是不是想得太多了?
更新
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) ""
  }
}

最佳答案

为子类别目录创建数组:

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

除了foreach之外,在循环中使用数组(aforis_set)将允许您仅显示已设置的数组项:
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++; } ?>

10-08 17:45