Asfi Tanoli Asfi Tanoli - 5 months ago 15
PHP Question

Add a button in foreach loop in php

Each school has multiple payment records. I have a

summary
page where im displaying only 3 records and i have created another detail page where im displaying all payment records for that particular school. Im using Yii Framework.

For example, I have a school, which has total of
10 payment records
. I'm using
foreach
loop to display only 3 records on summary page. But what i want now is to
add a button
on summary page. As you know total records are
10
then after those 3 records on summary page, I want to display a button which will be linked to detail page where im showing all records.

and secondly, I don't want to display a button if records are already 3, if records are more than 3 then it should display a
View More
button. Where should i put button in my code?

How can i achieve this? Here is my
foreach
loop showing 3 records.

<?php
$i = 0;
foreach($model->payments as $modelPayment){ ?>
<tr>
<td><?PHP echo $modelPayment->amount; ?></td>
<td><?PHP echo $modelPayment->deposit_date; ?></td>
</tr>
<?PHP
$i++;
if($i==2){
break;
}
}
?>

Answer

As you are using Yii, so add count($model->payments) > 3 this to your if statement like this below.

<?PHP
$i++;
if($i==3){
      if(count($model->payments) > 3){
        ?>
        <tr>
        <td><button class="btn btn-warning btn-md">More</button></td>
        </tr>
<?php
}
break;
}