Foreach循环在Codeigniter视图中不起作用

时间:2013-12-20 16:03:46

标签: php codeigniter foreach

我必须在单个视图文件中列出两个区域中的区域。顶级foreach循环正在工作,但是在foreachloop下方不起作用。

<div class="wiyo_reg">
    <div class="wiyo_region_select span12">
        Choose Destination
    </div>
    <div class="wiyo_region_option_holder">
        <ul>
            <?php foreach($regions_list as $regions_list)
            {

            ?>
            <li class="wiyo_reg_li">
                <a id="wiyo_reg_id" data-value="<?php echo $regions_list -> slug; ?>" data-image="<?php echo base_url(); ?>app/resources/wiyo/img/flags/<?php echo $regions_list -> code; ?>.png" data-txt="<?php echo $regions_list -> name; ?>">
                    <img src="<?php echo base_url(); ?>app/resources/wiyo/img/flags/<?php echo $regions_list -> code; ?>.png" /> <?php echo $regions_list -> slug; ?>
                </a>
            </li>
            <?php } ?>
        </ul>
    </div>
    <div class="destination_error"></div>
</div>

<?php echo form_open(base_url().'index.php/bookings/search', array('id' => 'booking_regions_form', 'class' => 'booking_regions_form')); ?>
<select class="makefancy indexselect" name="booking_form_regions" style="display:none;">
    <option value="0" selected="selected" data-skip="1">Choose Destination</option><?php foreach($regions_list as $regions)
            {

            ?>
    <option value="<?php echo $regions -> slug; ?>"><?php echo $regions -> name; ?></option>

        <?php } ?>
</select>

<div class="form_label_wi_input indexform">
                <label>Departure Date</label>
    <input type="text" id="dpd6" name="booking_form_outbound_date" required="required" readonly="readonly" />
</div>

<div class="form_label_wi_input indexform">
    <label>Return Date</label>
        <input type="text" id="dpd7" name="booking_form_intbound_date" required="required" readonly="readonly" />
        </div>

<div class="form_label_wi_input">
    <input type="submit" name="submit_region" id="submit_region" class="span5 btn btn-info" value="Book" />
        </div>

<?php echo form_close(); ?>

我得到了顶级foreach循环中的区域列表,但是,我在foreach循环中得到了这种类型的错误。

<option value="<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Trying to get property of non-object</p>
<p>Filename: partials/banner.php</p>
<p>Line Number: 74</p>

</div>"><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Trying to get property of non-object</p>
<p>Filename: partials/banner.php</p>
<p>Line Number: 74</p>

</div></option>

2 个答案:

答案 0 :(得分:1)

试试这个,您已经习惯在$regions_list循环foreach

中再次定义$regions_list as $regions_list
foreach($regions_list as $region_list)

而不是

 foreach($regions_list as $regions_list)

注意:您需要在代码中将$regions_list ->替换为$region_list ->

即。使用<?php echo $region_list -> code; ?>

而不是<?php echo $regions_list -> code; ?>

答案 1 :(得分:0)

您将变量$regions_list重新分配给同一个变量,尝试使用另一个变量,例如:$region_list
试着像这样改变你的foreach:

<?php foreach($regions_list as $region_list){?>
     <li class="wiyo_reg_li">
        <a id="wiyo_reg_id" data-value="<?php echo $region_list -> slug; ?>" data-image="<?php echo base_url(); ?>app/resources/wiyo/img/flags/<?php echo $region_list -> code; ?>.png" data-txt="<?php echo $region_list -> name; ?>">
         <img src="<?php echo base_url(); ?>app/resources/wiyo/img/flags/<?php echo $region_list -> code; ?>.png" /> <?php echo $region_list -> slug; ?></a>
      </li>
 <?php } ?>