Populate Dropdown Boxes while also Populating Table

≯℡__Kan透↙ 提交于 2019-12-12 01:39:20

问题


I have a table that is populated using a foreach loop. One of my columns is SKU Group, which is a column of dropdown boxes. However, whenever the foreach loop runs, only the one corresponding value for that row is displayed inside the dropdown box.

How can I get it so that I can run the foreach loop and it correctly populates the table, while the dropdowns are also populated with every SKU Group name and not just the single value that corresponds with each row?

Normally, I would just run this foreach($var->fetchAll() as $var1) for the dropdowns to populate them, but I dont think that it can be ran properly inside another loop that is already running. So that is why I am having this issue.

HTML Table:

<table id="skuTable" cellspacing="5" class="ui-widget ui-widget-content">
    <thead>
        <tr class="ui-widget-header">
            <th style="display: none">Product ID</th>
            <th class="skuRow">Major Category</th>
            <th class="skuRow">Minor Category</th>
            <th class="skuRow">Report Code</th>
            <th class="skuRow">SKU</th>
            <th class="skuRow">SKU Description</th>
            <th class="skuRow">SKU Status</th>
            <th class="skuRow">Create Date</th>
            <th class="skuRow">SKU Group</th>
            <th class="skuRow">Edit</th>
        </tr>
    </thead>
    <tbody>

        <?php foreach ($dbh->query($query) as $row) {?>

        <tr>
            <td style="display: none" class="prod_id" id="product_id-<?php echo intval ($row['Product_ID'])?>"><?php echo $row['Product_ID']?></td>
            <td class="major_cat" id="major_cat-<?php echo intval ($row['Major Category'])?>"><?php echo $row['Major Category']?></td>
            <td class="minor_cat" id="minor_cat-<?php echo intval ($row['Minor Category'])?>"><?php echo $row['Minor Category']?></td>
            <td class="rep_code" id="rep_code-<?php echo intval ($row['Product Report Code'])?>" align="center"><?php echo $row['Product Report Code']?></td>
            <td class="sku" id="sku-<?php echo intval ($row['SKU'])?>" align="center"><?php echo $row['SKU']?></td>
            <td class="sku_desc" id="sku_desc-<?php echo intval ($row['SKU Description'])?>"><?php echo $row['SKU Description']?></td>
            <td class="sku_status" id="sku_status-<?php echo intval ($row['SKU Status'])?>" align="center"><?php echo $row['SKU Status']?></td>
            <td class="create_date" id="create_date-<?php echo intval ($row['Date'])?>" align="center"><?php echo $row['Date']?></td>
            <td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">

                <select id="sku_group_dropdown">
                    <option
                        value=""
                        data-name="<?php echo $row ['SKU Group'];?>"
                    >
                        <?php echo $row ['SKU Group'];?>
                    </option>
                </select>

            </td>
            <td><input type="button" class="edit" name="edit" value="Edit"></td>
        </tr>

    <?php } ?>

    </tbody>
</table>

回答1:


first need to populate your dropdown list using loop

<?php 
 $i = 0;
 $content = '';
  $names =  array('Test1','Test2');
  foreach ($names as $row) {
  if($i== 0){  
  $names =  array('Test1','Test2');// replace it  echo $row ['SKU Group'];

  foreach($names as $key)
  {
   $content .= '<option value="'.$key.'">'.$key.'</option>';    
  }
   } 
  $i++;
  ?>

  <tr>
   <td>sadasdsa</td>
    <td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">

    <select id="sku_group_dropdown">
    <?php echo $content?>
    </select>

     </td>
        <td><input type="button" class="edit" name="edit" value="Edit">                  </td>
       </tr>

          <?php } ?>

in this code replace your array data or if you want to populate dropdown data outside your loop then first get your group data for you can use this code



来源:https://stackoverflow.com/questions/42375534/populate-dropdown-boxes-while-also-populating-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!