我使用foreach循环来显示表记录。下面是我的代码indexSuccess.php模板
<?php $counter = 0; ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<?php $counter++; ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php if($counter==0): ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php endif; ?>
我似乎找不到回音的方法
<td colspan="5">No items found</td>
当表没有显示任何记录时。
如果我使用
<?php echo var_dump ($counter); ?>
显示
下面的结果int 1
int 2
int 1
int 2
int 1
int 2
int 1
int 2
int 1
int 2
<?php if($prescription->getPrescriptionDrugs()->count() == 0): ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php else: ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
您可以使用count()函数返回行数。
<?php $rows = count($prescription->getPrescriptionDrugs()); ?>
<?php if($rows==0){ ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php } else { ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php } ?>