【问题标题】:Loop statement inside if else statementif else 语句中的循环语句
【发布时间】:2019-09-30 19:43:38
【问题描述】:

我想在表格中使用循环显示所有行,但如果没有可用的数据,我想在那里回显一些文本。 我正在使用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>

<? } ?>

【问题讨论】:

  • 你到底有什么问题?听起来您很清楚自己想做什么,只是还没有完成
  • 它已经解决了,只是 if 语句后我的代码中缺少大括号,我对循环大括号感到困惑。
  • 下次,请务必在问题中包含您的尝试。它可能会因为错字而很快被关闭。
  • 是的,你是对的,谢谢你的建议。

标签: php if-statement foreach codeigniter-3


【解决方案1】:

但是if 在循环中:

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

【讨论】:

  • 谢谢,它成功了。我只是错过了 if 标记的大括号。
最近更新 更多