if语句中的循环语句

时间:2019-09-30 19:43:38

标签: php if-statement foreach codeigniter-3

我想在表中使用循环显示所有行,但是如果没有可用数据,我想在其中回显一些文本。 我正在使用codeigniter。我想在if语句中使用循环,否则在没有数据时将回显任何文本。

这是代码

<?php 
$nextdate = date('Y-m-d',strtotime("+3 day"));
$this->db->order_by('tourdateID', 'ASC');
$this->db->where('tourid', $blogres[0]['tourid']);
$this->db->where('start >=', $nextdate); 
$subs = $this->db->get('tour_date')->result_array();

$i = 1;

foreach($subs as $sub){?>
    <div class="row priceRow priceRowReg  filter_12  guaranteedFlag">
        <div class="eq-pr2 small-4 medium-4 large-4 columns pad-lft-20">
            <div class="tcircle amber"></div><a class="date drk" rel="nofollow" href="#"><?php echo date('d M, Y', strtotime($sub['start'])); ?> - <?php echo date('d M, Y', strtotime($sub['end'])); ?></a>

        </div>

        <div class="eq-pr2 small-3 medium-3 large-3 columns cnt bld"><?php 

        $this->db->where('tourdateID',$sub['tourdateID']);
        $booking = $this->db->count_all_results('book_tour');                    
        $availibility = $sub['total_capacity'] - $booking;


        ?><?= $availibility; ?> Seats left</div>
        <div class="eq-pr2 small-3 medium-3 large-3 columns cnt pad-rgt-20"><a class="inl bld f19 wht wrBCol3 colLinkPad showinfo" rel="nofollow" href="<?=base_url();?>bookings/step1/<?= $blogres[0]['tourid'];?>/<?= $sub['tourdateID']?>">Book Now</a> </div>

    </div>

<? } ?>

1 个答案:

答案 0 :(得分:0)

但是循环中的if

if (count($subs) > 0) {
    foreach ($subs as $sub) {?>
        ...
    <?php } 
} else { ?>
    <div> No data available </div>
<?php }