0

How to fix this error messages:

A PHP Error was encountered
Severity: Warning Message: Invalid
argument supplied for foreach()
Filename: views/editpages.php
Line Number: 48


Line 48: <?php foreach ($pages as $pages_item): ?>


views/editpages.php

<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>

controllers/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); 

}

models/Mpages.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();

}
Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
  • according to your foreach you get n number of forms and tables. But all the form action goes to one method. Check your code with is this archive your purpose. ex: if you had 15 array in foreach you get 15 table and 15 forms. But one form action URL. – Abdulla Nilam Oct 23 '16 at 05:29
  • Possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Abdulla Nilam Oct 23 '16 at 05:30
  • 1
    @Davy .try to print before foreach . print_r($pages); exit; check what data you get – Vision Coderz Oct 23 '16 at 06:17
  • @Davy i think $pages has no array data – Vision Coderz Oct 23 '16 at 06:18
  • 1
    Well a few things to checkout... Do you have a segment(3) value and what happens when you don't? Are you checking that a valid result comes back from your query, ie is what is in $data['pages'] an array or something else? I'm hinting on being able to debug these things via inspecting the result of each step and being able to handle exceptions to what you expect. – TimBrownlaw Oct 23 '16 at 06:25
  • Try adding `$data['pages'] = array();` above here `$data['pages'] = $this->Mpages->call_point_pages($pages_id);` –  Oct 23 '16 at 07:08

2 Answers2

0

Controller/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); 
}

models/Mpages.php

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

Only one result will be get because there is a where condition and that seems like a primary key

so your View must like

<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>
BIBIN JOHN
  • 354
  • 7
  • 13
0

Hope you have already debugged and figured out the issue based on the comments and responses posted here..

this is just a tip:

Whenever, you have a method that is supposed to return array - you should have an empty array declaration inside that method. And then later on assign value/values to that array. This way you will always be able to use the returned value inside a foreach loop without worrying about whether the returned value is actually an array or not.

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;
}

This looks like a lot of code, but it has saved me from un expected errors at run time.

pankaj
  • 1,316
  • 3
  • 16
  • 27