为foreach()第48行提供的参数无效

时间:2016-10-23 05:24:32

标签: php codeigniter

如何修复此错误消息:

  

遇到PHP错误   严重性:警告消息:无效
  为foreach提供的论据()
  文件名:views / editpages.php
  行号:48

第48行:<div class="row-fluid"> <div class="span12"> <?php foreach ($pages as $pages_item): ?> <div class="widget-box"> <div class="widget-title"><h5>Pages</h5></div> <div class="widget-content"> <?php echo form_open('cpages/editpagesupdate'); ?> <table border="0" style="width: 100%; height: 90px;"> <tr> <td>Pages Name</td> <td><input type="text" name="pages_name" value="<?php echo $pages_item['pages_name']; ?>"></td> </tr> <tr> <td>Create Date</td> <td><input type="text" name="create_date" value="<?php echo $pages_item['create_date']; ?>"></td> </tr> <tr> <td>Order</td> <td><input type="text" name="pages_order" value="<?php echo $pages_item['pages_order']; ?>"></td> </tr> <tr> <td>View Content</td> <td><textarea rows="3" cols="20" name="pages_content"><?php echo $pages_item['pages_content']; ?></textarea></td> </tr> <tr> <td></td> <td><input type="submit" class="edit" name="submit" value="SUBMIT"></td> </tr> </table> </div> </div> <?php endforeach; ?> </div> </div>

视图/ editpages.php

public function editpages() {

    $data['pagessuccess'] = '';

    $pages_id = $this->uri->segment(3);

    $data['pages'] = $this->Mpages->call_point_pages($pages_id);

    $this->load->view('editpages', $data); 

}

控制器/ Cpages.php

public function call_point_pages($pages_id)
{

    $this->db->where('pages_id', $pages_id);
    $query = $this->db->get('pages');
    //return $query->result_array();
    return $query->result_array();

}

模型/ Mpages.php

initialize: function(options){
    this.err= options.err;
},

2 个答案:

答案 0 :(得分:0)

控制器/ cpages.php

public function editpages() {
    $data['pagessuccess'] = '';
    $pages_id = $this->uri->segment(3);
    $data['pages'] = $this->Mpages->call_point_pages($pages_id);
    $this->load->view('editpages', $data); 
}

模型/ Mpages.php

public function call_point_pages($pages_id) {
    $this->db->where('pages_id', $pages_id);
    $query = $this->db->get('pages');
    return $query->result();
}

只有一个结果会被获取,因为有一个where条件,而且看起来像一个主键

所以你的视图必须

<div class="row-fluid">
<div class="span12">    
<div class="widget-box">
<div class="widget-title"><h5>Pages</h5></div>
<div class="widget-content">
<?php echo form_open('cpages/editpagesupdate'); ?>
<table border="0" style="width: 100%; height: 90px;">
<?php foreach ($pages as $pages_item) { ?>
<tr>
<td>Pages Name</td>
<td><input type="text" name="pages_name" value="<?php echo $pages_item->pages_name; ?>"></td>
</tr>
<tr>
<td>Create Date</td>
<td><input type="text" name="create_date" value="<?php echo $pages_item-   >create_date; ?>"></td>
</tr>   
<tr>
<td>Order</td>
<td><input type="text" name="pages_order" value="<?php echo $pages_item->pages_order; ?>"></td>
</tr>
<tr>
<td>View Content</td>
<td><textarea rows="3" cols="20" name="pages_content"><?php echo $pages_item->pages_content; ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" class="edit" name="submit" value="SUBMIT"></td>
</tr>
<?php } ?>
</table>            
</div>
</div>      
</div>
</div>

答案 1 :(得分:0)

希望您已根据此处发布的评论和回复调试并找出问题。

这只是一个提示:

每当你有一个应该返回数组的方法时 - 你应该在该方法中有一个空数组声明。然后再为该数组赋值/值。通过这种方式,您始终可以在foreach循环中使用返回的值,而无需担心返回的值是否实际上是数组。

e.g。

public function call_point_pages($pages_id) {
    $pages = array();

    $this->db->where('pages_id', $pages_id);
    $query = $this->db->get('pages');

    if ($query->num_rows() > 0) {
        foreach($query->result() as $row) {
            $page = object2array($row);
            $pages[] = $page;
        }
        $query->free_result();
    }
    return $pages;
}

这看起来像很多代码,但它在运行时将我从非预期的错误中解救出来。