How can I delete 'AND' in last Loop (for each)

Viewed 65

I have a question. Heres my code

<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
    <p><?php echo $key['symptoms'] ?></p>
    <p><strong style="color: green;">AND</strong></p>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>

But, The output is like this

IF
symptoms A
AND
symptoms B
AND
symptoms C
AND
THEN
disease A

I want the last AND to be disappear. Can anyone help me pls? :( I'm sorry because I'm a newbie in here. Thanks!

1 Answers

You need to check if $i is less than the last element in the array: $i < count($data) - 1. Here is an example:

<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
    <p><?php echo $key['symptoms'] ?></p>
   <!-- only print AND if it is not the last element --> 
   <?php if ($i < count ($data) - 1) { ?>
         <p><strong style="color: green;">AND</strong></p>
    <?php } ?>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>
Related