Return html from nested foreach in controller

Viewed 37

I have a controller from that I am fetching data using ajax. The problem is, there are nested foreach loop, in order to get relational data as well. What I have done so far is very chaotic :

foreach($productList as $key => $rows) {
   foreach($rows->productPrice as $prices) {
       if($prices->productVariantOne != NULL) { 
           $variantOne = $prices->productVariantOne->variant;
       }

       if($prices->productVariantTwo != NULL) {
           $variantTwo = '/'.$prices->productVariantTwo->variant;
       }

       if($prices->productVariantThree != NULL) {   
           $variantThree = '/'.$prices->productVariantThree->variant;
       }
       $output .= '<tr>
           <td>'.$key.'</td>
           <td>'.$rows->title.' <br> Created at : '.$rows->created_at.'</td>
           <td>'.$rows->description.'</td>
           <td>
               <dl class="row mb-0 variant" style="height:100px;overflow:hidden" id="">
                  <dt class="col-sm-3 pb-0">
                      '.$variantOne.$variantTwo.$variantThree.'
                  </dt>
                  <dd class="col-sm-9">
                      <dl class="row mb-0">
                         <dt class="col-sm-4 pb-0">Price : '.number_format($prices- 
                                 >price, 2).'</dt>
                            <dd class="col-sm-8 pb-0">InStock : '.number_format($prices- 
                                >stock,2).'</dd>
                       </dl>
                   </dd>
                 </dl>
               <button id="" class="btn btn-sm btn-link getFull">Show more</button>
            </td>
            <td>
               <div class="btn-group btn-group-sm">
                    
               </div>
            </td>
         </tr>';
      }
   }
0 Answers
Related