首页 > 解决方案 > 在多个 foreach 循环表中添加 if else 语句

问题描述

我有一个表,它使用FOREACH LOOP. 当我在第一个循环IF ELSE STATEMENT中插入另一个循环时,第一个循环似乎没问题FOREACH LOOPtd开始不再对齐。这意味着如果该值为空,它将隐藏单元格,并且第一个循环中的其他值将被向前推。

供您参考,我使用的是不同的服务器数据库,它们是SQLMYSQL. SQL 是我的主要数据库,mysql 用于插入、更新和删除表中称为“NOTES”的文本区域。

这是来自我的控制器的查询

 $getNote1 = DB::connection('mysql')->table('note1')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
 $getNote2 = DB::connection('mysql')->table('note2')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
 $getNote3 = DB::connection('mysql')->table('note3')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();
 $getNote4 = DB::connection('mysql')->table('note4')->select('stockcode', 'note')->orderBy('stockcode', 'ASC')->get();

 return view('posts.index')->with('title', $title)->with('query', $query)->with('getNote1', $getNote1)->with('getNote2', $getNote2)->with('getNote3', $getNote3)->with('getNote4', $getNote4);

这是我的表格代码

<table id="table1" class="table table-bordered"> 
   <thead>
      <tr>
         <th style="display:none;">Stock Code</th>
         <th>Stock Name</th>
         <th>Note 1</th>
         <th>Note 2</th>
         <th>Note 3</th>
         <th>Note 4</th>
         <th>Order Level</th>
         <th>Qty Level</th>
         <th>Balance</th>  
         <th>Purchase Price</th>  
         <th>UOM</th>                    

         @for ($i=0; $i<=12; $i++)
            <th><?php echo date('Y/m', strtotime('-'.$i.' month', strtotime($getDate)));?></th>
         @endfor                                          
      </tr>
      </thead>
      <tbody>                    
         <!-- Foreach loop from db -->
         @foreach ($query as $val)   
            <?php
               $stockcode = $val->StockCode;
               $stockname = $val->StockName;
               $currentbalance = $val->CurrentBalance;
               $purchaseprice = $val->PurchasePrice;
               $baseuom = $val->BaseUOM;
             ?>
             <tr>                    
                <td style="display:none;">{{ $val->StockCode }}</td>
                <td class="text-nowrap align-middle">{{ $val->StockName }}</td> 

                @foreach($getNote1 as $note1)   
                   @if($note1->stockcode == $stockcode)              
                      <td><textarea rows="2" cols="10" name="note1" class="note1 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}">{{ $note1->note }}</textarea></td>
                   @else
                       <td><textarea rows="2" cols="10" name="note1" class="note1 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea><td>
                   @endif
                @endforeach

                <td><textarea rows="2" cols="10" name="note2" class="note2 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea></td>
                <td><textarea rows="2" cols="10" name="note3" class="note3 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea></td>
                <td><textarea rows="2" cols="10" name="note4" class="note4 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}"></textarea></td> 
                <td><input type="text" id="orderlevel" class="text-right"></td>
                <td><input type="text" id="qtylevel" class="text-right"></td>
                <td class="text-right align-middle">{{ number_format($currentbalance, 2) }}</td>
                <td class="text-right align-middle">{{ number_format($purchaseprice, 2) }}</td>
                <td class="text-center align-middle">{{ $baseuom }}</td>

                @for ($i=1; $i<=13; $i++)
                <?php 
                   //looping variables
                   $Qty = 'Qty'.$i;
                   $Date = 'Date'.$i;
                ?>
                <td class="text-right align-middle">                                    
                   @if($val->$Qty > 0) 
                      <a id="viewdetails" data-toggle="modal" 
                             data-productid="{{$val->Id}}"
                             data-productname="{{$val->StockName}}"
                             data-getdate="{{date('Y-m', strtotime($val->$Date))}}"
                             href="#" data-target="#dataModal">{{ number_format($val->$Qty, 2) }}</a>
                   @endif
                </td>                                                 
             @endfor 
          </tr> 
       @endforeach  
       <!-- End Foreach loop -->                     
     </tbody>
  </table>

这是我的截图 图片1

这是我想要的表格 图片2

标签: phpmysqllaravel

解决方案


不要隐藏单元格,而是在其中放置一个空值。

<td><textarea rows="2" cols="10" name="note1" class="note1 align-middle" data-stockcode="{{ $stockcode }}" data-stockname="{{ $stockname }}">
 @foreach($getNote1 as $note1)   
               @if($note1->stockcode == $stockcode)              
                  {{ $note1->note }}
               @endif
            @endforeach
</textarea></td>

然后它会很好地对齐


推荐阅读