这让我很难过。我可以 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;
}
}
?>