代码之家  ›  专栏  ›  技术社区  ›  Rataiczak24

在另一个foreach循环中使用PHP foreach循环

  •  0
  • Rataiczak24  · 技术社区  · 6 年前

    我猜这是不可能的,还是我做错了什么?如何找到一个解决方法,以便在填充整个表的同时,还可以使用多个值填充下拉列表,但仍将其默认为数据库中的值?

    <?php
    $sql = "SELECT TOP 100 *
            FROM Table_OS_List
            ORDER BY [CURRENT_SKU] ASC";
    
    
    $drops = "SELECT [Purchasing_Group]
            FROM Table_OS_List
            GROUP BY [Purchasing_Group]";
    $drop = $dbh->query($drops);
    ?>
    
    <?php
        /* Foreach loop that brings in information to populate table */
        foreach ($dbh->query($sql) as $rows) {
        ?>
        <tr class="row">    
            <td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
            <td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
            <td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>
    
    
            <td class="dropdown-select" id="purchgroup">
                <select id="selected_group" class="selected_group" disabled>
                    <?php foreach($drop->fetchAll() as $dropdown) { ?>
    
                      <option class="choice" value="Purchasing Group"><?php echo $dropdown['Purchasing_Group'];?></option>
    
                    <?php } ?>
    
                </select>
            </td>
    
    
            <td><input type="button" class="edit" name="edit" value="Edit"></td>
            <td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
        </tr>
     <?php
      }
     ?>
    

    这段代码正确地填充了整个表,但是下拉列表只包含每行中的值,如果需要选择其他值选项,则不会填充其他值选项:

    <?php
        /* Foreach loop that brings in information to populate table */
        foreach ($dbh->query($sql) as $rows) {
        ?>
        <tr class="row">    
            <td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
            <td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
            <td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>
    
    
            <td class="dropdown-select" id="purchgroup">
                <select id="selected_group" class="selected_group" disabled>
    
                    <option class="choice" value="Purchasing Group"><?php echo $rows['Purchasing_Group'];?></option>
    
                </select>
            </td>           
    
            <td><input type="button" class="edit" name="edit" value="Edit"></td>
            <td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
        </tr>
     <?php
      }
     ?>
    

    编辑:

    enter image description here

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   RiggsFolly    6 年前

    是的,如果做得不对,您将在外部foreach的第一次迭代完成时使用第二个resultset,因此将下拉列表内容检索到一个数组中,以便可以多次重用它

    <?php
    $sql = "SELECT TOP 100 *
            FROM Table_OS_List
            ORDER BY [CURRENT_SKU] ASC";
    
    
    $drops = "SELECT [Purchasing_Group]
            FROM Table_OS_List
            GROUP BY [Purchasing_Group]";
    $drop = $dbh->query($drops);
    $allDrops = $drop->fetchAll();
    
    ?>
    
    <?php
        /* Foreach loop that brings in information to populate table */
        foreach ($dbh->query($sql) as $rows) {
        ?>
        <tr class="row">    
            <td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
            <td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
            <td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>
    
    
            <td class="dropdown-select" id="purchgroup">
                <select id="selected_group" class="selected_group" disabled>
                    <?php 
                    foreach($allDrops as $dropdown) { 
            //--------------^^^^^^^^^
            // Also changed the way you fill the option tag below
                    ?>
    
                      <option class="choice" 
                              value="<?php echo $dropdown['Purchasing_Group'];?>">
                        <?php echo $dropdown['Purchasing_Group'];?>
                      </option>
    
                    <?php } ?>
    
                </select>
            </td>
    
    
            <td><input type="button" class="edit" name="edit" value="Edit"></td>
            <td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
        </tr>
     <?php
      }
     ?>