首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在多个foreach循环表中添加if else语句

问在多个foreach循环表中添加if else语句
EN

Stack Overflow用户
提问于 2019-06-28 10:06:17
回答 1查看 367关注 0票数 0

我有一个表,它使用FOREACH LOOP从数据库循环它的值。当我在第一个循环内的另一个FOREACH LOOP中插入IF ELSE STATEMENT时,第一个循环看起来没有问题,td开始不再对齐。这意味着如果值为空,它将隐藏单元格,而第一个循环中的其他值将被向前推。

供您参考,我使用的是不同的服务器数据库,它们是SQL和MYSQL。SQL是我的主要数据库,mysql是用来在表格中插入、更新和删除被称为“备注”的文本区域。

下面是来自我的控制器的查询

代码语言:javascript
复制
 $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);

下面是我的表代码

代码语言:javascript
复制
<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>

这是我的截图

​

这是我想要的表格

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-28 10:21:56

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

代码语言:javascript
复制
<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>

然后它就会很好地对齐

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56800150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档