Mark
Mark

Reputation: 37

conditional display php and mysql

<?php echo $row['time']; ?>&nbsp;Normal time (<?php echo $row['time']/2; ?> years)

I want to have a case when ['time']/2 > 1 it is shown as years but if it is 1 it shall be shown only as year

How can I do this using if else

any idea?

Upvotes: 0

Views: 60

Answers (3)

Jemaclus
Jemaclus

Reputation: 2376

You only want to show "year" if $row['time']/2 is 1 (0 is still plural), so you want to do something like:

   <? $years = $row['time'] / 2; ?>
   <?= $row['time'] ?> Semester (<?= $years == 1 ? '1 year' : $years.' years' ?>)

Good luck. :)

Upvotes: 0

Galen
Galen

Reputation: 30170

<?php echo $row['time']; ?>&nbsp;Semster (<?php echo $row['time']/2; ?> year<?php if( $row['time']/2 > 1 ): ?>s<?php endif; ?>)

Leave off the s and only add it if time/2 > 1

Upvotes: 0

Jason Brumwell
Jason Brumwell

Reputation: 3550

<?php $years = $row['time']/2; ?>
<?php echo $row['time']; ?>&nbsp;Semster (<?php echo $years > 1 ? $years.' years' : $years.' year'; ?>)

Upvotes: 1

Related Questions