Aravinth E
Aravinth E

Reputation: 491

How to Html into PHP table inside foreach

I tried Laravel Controller Inside Function Make Html Table All are Worked But table body inside i want multiple row so tried this code not worked properly code

 $contents = '<table class="border" style="margin-top: 2%">
                           <thead>
                                <tr>
                                    <th>SNo</th>
                                    <th>Specification</th>
                                    <th>Description</th>
                                    <th>Qty</th>
                                    <th>UOM</th>
                                    <th>Unit Price</th>
                                    <th>Disc</th>
                                    <th>Net Price</th>
                                </tr>
                           </thead>
                           <tbody>
                              '.foreach ().'
                            </tbody>
                        </table>';

I want this type of code inside the body of table

echo '<tr>
                        <td>'.$k.'</td>
                        <td>'.$v->Specification.'</td>
                        <td>'.$v->Description.'</td>
                        <td>'.$v->Quantity.'</td>
                        <td>'.$v->UOM.'</td>
                        <td>'.$v->Unit_Price.'</td>
                        <td>'.$v->Discount.'</td>
                        <td>'.$v->Net_Price.'</td>
                   </tr>';

Upvotes: 0

Views: 55

Answers (2)

little_coder
little_coder

Reputation: 564

You can do it like this.

<?php

$contents = '<table class="border" style="margin-top: 2%">
                           <thead>
                                <tr>
                                    <th>SNo</th>
                                    <th>Specification</th>
                                    <th>Description</th>
                                    <th>Qty</th>
                                    <th>UOM</th>
                                    <th>Unit Price</th>
                                    <th>Disc</th>
                                    <th>Net Price</th>
                                </tr>
                           </thead>
                           <tbody>';

foreach( $array as $value ){ // your array here                          
   $contents .=  "<tr>";

   $contents .=  "<td>".$k."</td>";
   $contents .=  "<td>".$v->Specification."</td>";
   $contents .=  "<td>".$v->Description."</td>";
   $contents .=  "<td>".$v->Quantity."</td>";
   $contents .=  "<td>".$v->UOM."</td>";
   $contents .=  "<td>".$v->Unit_Price."</td>";
   $contents .=  "<td>".$v->Discount."</td>";
   $contents .=  "<td>".$v->Net_Price."</td>";

   $contents .= "</tr>";

};

$contents .= '</tbody></table>';              

echo $contents;

Upvotes: 2

u_mulder
u_mulder

Reputation: 54796

 $contents = '<table class="border" style="margin-top: 2%">
                           <thead>
                                <tr>
                                    <th>SNo</th>
                                    <th>Specification</th>
                                    <th>Description</th>
                                    <th>Qty</th>
                                    <th>UOM</th>
                                    <th>Unit Price</th>
                                    <th>Disc</th>
                                    <th>Net Price</th>
                                </tr>
                           </thead>
                           <tbody>';
foreach () {
    $contents .= '<tr><td>'.$k.'</td></tr>'; // other tds here too
}
$contents .= '</tbody></table>';

Upvotes: 1

Related Questions