0

how do i generate only 3 columns in a page using below code if i keep echo'(table)' inside it generates the whole table so i want only one table allow only 3 columns in a page after 3 columns append a new row

     <?php
        $i = 0;
        echo  '<table style="float: left;width: -weekbit-fill-available"; ><tr>';
        ?>
        <?php foreach ($product as $p) {
            $i++;



           // $i++;
            echo "<tr><td>Name:<b>". $p->name ."</td></tr>";
            echo "<tr><td>productID:<b>".$p->proid ."</td></tr>";
            echo "<tr><td>Price:<b>". $p->price ."</td></tr>";
            echo "<tr><td><a href='/cart/cart/add?id=$p->proid'><input type='button' value='addtocart'/></td></tr>";


            if($i==3){
                echo '</tr><tr>';
            }

            echo"</tr>
                        </table>";
            ?>
        <?php }?>
        <?php  echo"</tr>
                        </table>"?>

thanks in advance
clemens
  • 6,653
  • 2
  • 19
  • 31
Lochan SS
  • 109
  • 1
  • 1
  • 8
  • Possible duplicate of [Insert tr after every third loop](https://stackoverflow.com/questions/9008522/insert-tr-after-every-third-loop) – mx0 Nov 01 '18 at 09:55

2 Answers2

1

Try below

<table style="float: left; width: -weekbit-fill-available">
<tr>
    <th>Name</th>
    <th>ProductID</th>
    <th>Price</th>
    <th>Action</th>
</tr>
<?php foreach ($product as $p) : ?>
    <tr>
        <td><?= $p->name ?></td>
        <td><?= $p->proid ?></td>
        <td><?= $p->price ?></td>
        <td colspan="3"><?= \yii\helpers\Html::a('addtocart',
        ['/cart/cart/add', 'id' => $p->proid],
        [
            'title' => 'Add to Cart',
            'class' => 'btn btn-warning btn-sm',
        ]); ?></td>
    </tr>
<?php endforeach; ?>
</table>
vishuB
  • 4,173
  • 5
  • 31
  • 49
  • thanks but i want the same with 3 columns in a row and after 3 columns append to next row(like $p->name,proid,price there should 3name,price,proid in a row ) – Lochan SS Nov 01 '18 at 10:24
  • '; ?> Name:". $p->name .""; echo "productID:".$p->proid .""; echo "Price:". $p->price .""; echo ""; if($i%3==0){ echo ''; } ?> "; ?> i want like this but i want proid and price and also button below respective name – Lochan SS Nov 01 '18 at 10:48
0

Use <tr> for each row and <td> for each column

    <table style="float: left; width: -weekbit-fill-available"; >
        <?php foreach ($product as $p): ?>
        <tr>
            <td>Name: <b><?= $p->name ?></td>
            <td>productID: <b><?=  $p->proid ?></td>
            <td>Price: <b><?= $p->price ?></td>
            <td><a href='/cart/cart/add?id=<?= $p->proid ?>'><input type='button' value='addtocart'/></td>
        </tr>
        <?php endforeach; ?>
    </table>";

See also:

Anton Rybalko
  • 1,229
  • 17
  • 23