Richmond
Richmond

Reputation: 473

Adding if else statement inside multiple foreach loop table

I have a table that loops its value from the database using FOREACH LOOP. The first loop seems okay when I inserted IF ELSE STATEMENT in another FOREACH LOOP inside the first loop, the td begin to not align anymore. Which means if the value is empty, it will hidden the cell and other values from the first loop will be push forward.

For your information, I'm using different server database which are SQL and MYSQL. SQL is my main db and mysql is for inserting, updating and deleting the textarea known as 'NOTES' in the table.

Here's the query from my controller

 $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);

Here's my table code

<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>

Here's my screenshot Image1

Here's the table that I want it to be Image2

Upvotes: 0

Views: 719

Answers (1)

Jovs
Jovs

Reputation: 892

don't hide the cell instead put an empty value in it.

<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>

then it will align just fine

Upvotes: 2

Related Questions