PHP for each with an if rule

Asked

Viewed 149 times

1

I have a table that brings paid and unpaid commission results.

I want him to bring only the pay, I mean, if number_format($row->amount_paid,2) = 0 not bring that line.

<?php 
    $i=-1;
    foreach($this->rows->rows as $row){?>
        <tr class="sectiontableentry<?php echo ($i%2)+1;?>">
            <td><?php echo $row->name; ?></td>
            <td align="right"><?php echo empty($row->timestamp) ? '' : date('Y-m-d',$row->timestamp); ?></td>
            <td align="right"><?php echo $row->order_number; ?></td>
            <td align="right"><?php echo number_format($row->order_total,2); ?></td>
            <td align="right"><?php echo number_format($row->commission,2); ?></td>
            <td align="right"><?php echo number_format($row->amount_paid,2);?></td>
            <td align="right"><?php echo number_format($row->balance,2); ?></td>
            <td><?php echo $row->note; ?>&nbsp;</td>
        </tr>
<?php  }?>

I believe I have to use if somewhere, right? But I don’t know how.

  • if(number_format($row->amount_paid,2) != 0){ //...código} doesn’t work?

  • then, my question is where to put the if put here: foreach($this->Rows->Rows as $Row){if(number_format($Row->amount_paid,2) != 0){ //...code}} but did not give

  • After the foreach and before the <tr> and closes after the </tr>

  • 1

    Sorry, gave yes, I had put a "}" less, thank you!

1 answer

1

Just put the if within the loop, before any HTML code, conditioning it:

<?php 
  $i=-1;
  foreach($this->rows->rows as $row):
    if (number_format($row->amount_paid,2) != 0):
?>
      <tr class="sectiontableentry<?php echo ($i%2)+1;?>">
        <td><?php echo $row->name; ?></td>
          <td align="right"><?php echo empty($row->timestamp) ? '' : date('Y-m-d',$row->timestamp); ?></td>
          <td align="right"><?php echo $row->order_number; ?></td>
          <td align="right"><?php echo number_format($row->order_total,2); ?></td>
          <td align="right"><?php echo number_format($row->commission,2); ?></td>
          <td align="right"><?php echo number_format($row->amount_paid,2);?></td>
          <td align="right"><?php echo number_format($row->balance,2); ?></td>
          <td><?php echo $row->note; ?>&nbsp;</td>
        </tr>
<?php
    endif;
  endforeach;
?>

Note: The value of the variable i is not changed inside the loop, I believe you forgot to increment it, probably.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.