这让我很难过。我可以print_r名为$item_pages
的数组,它可以正确输出所有的键和值。但是,一旦在foreach中使用该数组,它似乎变空。当我在foreach之外print_r时,这是数组:
Array ( [0] => 1 [1] => 6 [2] => 12 [3] => 15 [4] => 16 [5] => 17 [6] => 18 [7] => 19 [8] => 78 [9] => 79 [10] => 87 [11] => 90 [12] => 94 [13] => 95 [14] => 98 [15] => 99 [16] => 102 [17] => 103 [18] => 105 [19] => 107 [20] => 108 [21] => 110 [22] => 111 [23] => 112 [24] => 113 [25] => 114 [26] => 119 [27] => 120 [28] => 121 [29] => 125 [30] => 126 [31] => 134 [32] => 135 [33] => 136 [34] => 138 [35] => 142 [36] => 186 [37] => 193 [38] => 197 [39] => 198 [40] => 199 [41] => 206 [42] => 327 [43] => 452 [44] => 470 [45] => 487 [46] => 601 [47] => 614 [48] => 630 [49] => 684 [50] => 726 [51] => 727 [52] => 789 [53] => 871 )
这是控制器:
public function view_item_pages() {
if ($this->input->is_ajax_request()) {
$item_id = $this->input->post('item_id');
$this->data['item_id'] = $item_id;
$this->data['pages'] = $this->affiliate_model->get_all_affiliates();
$this->data['item_pages'] = $this->item_model->get_item_pages($item_id);
$this->load->view('/admin/item/pages', $this->data);
}
}
以下是观点:
<fieldset>
<table id="all-pages" class="table table-striped table-bordered">
<thead>
<tr>
<th><input type="checkbox" class="checkbox" id="select_all"></th>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php foreach ($pages as $page):
$checked = FALSE;
if (!empty($item_pages)) {
if (in_array($page['aff_id'], $item_pages)) {
$checked = TRUE;
}
}
?>
<tr>
<td><?php echo form_checkbox('page', $page['aff_id'], $checked); ?></td>
<td><?php echo $page['aff_id']; ?></td>
<td><?php echo $page['name']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</fieldset>
因此,如果我执行以下操作,则数组将起作用:
<?php print_r($item_pages); ?>
<?php foreach ($pages as $page):
$checked = FALSE;
if (!empty($item_pages)) {
if (in_array($page['aff_id'], $item_pages)) {
$checked = TRUE;
}
}
?>
如果我这样做,它不返回任何内容,就像在做echo '';
:
<?php foreach ($pages as $page):
print_r($item_pages);
$checked = FALSE;
if (!empty($item_pages)) {
if (in_array($page['aff_id'], $item_pages)) {
$checked = TRUE;
}
}
?>
答案 0 :(得分:0)
在您的控制器中打印$pages
。如果$pages
为空,则执行不会进入视图内的foreach
循环。
$this->data['pages'] = $this->affiliate_model->get_all_affiliates();
print_r($this->data['pages']);