1

I am accessing an array object being transferred from model to view. When I print that array it shows complete info what I want, but when I implement foreach on that array and try to access an index it gives me an error of undefined index.

the model code is

public function fetch()
        {

$queryy=$this->db->select('*')
            ->from('selleritem')
            ->get();
            $data=$queryy->result();

            foreach($data as $datas)
            {
                $pid=$datas->pid ;
$query= $this->db->limit('1')
->select('*')
     ->where('pid', $pid)             
  ->get('selleritemimages'); 

    if($query->num_rows() > 0){
         foreach ($query->result() as $row)
         {
            $data[] = $row;
         }
        return $data;
        //print_r($data);
    }
}

the controller code is:

public function index()
    {

        $this->load->model('fetchindex');

        $fetch= array($this->fetchindex->fetch());  

        $this->load->view('welcome_message',['fetch'=>$fetch]);

    }
}

the view code is printing the array in view shows .

print_r($fetch);

    foreach ( $fetch as $ff) {

    echo $ff["pid"].'hh<br>';

    }   

printing the array in view shows .

Array ( 
    [0] => Array 
    ( 
        [0] => stdClass Object 
        ( 
            [id] => 2 
            [name] => fashionable kurta for men 
            [gender] => groom 
            [price] => 11213 
            [description] => Item Description 
            [rent_sale] => Rent 
            [features] => Key Features 
            [pid] => 4 
            [product] => kurta 
            [available] => 9 
            [uid] => 2 
            [ucid] => 7 
            [rating] => 0 
            [rating_users] => 0 
        ) 
        [1] => stdClass Object 
        ( 
            [id] => 1 
            [name] => suit fro groom 
            [gender] => groom 
            [price] => 12345 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 3 
            [product] => suits 
            [available] => 6 
            [uid] => 2 
            [ucid] => 11 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [2] => stdClass Object 
        ( 
            [id] => 3 
            [name] => sherwani 
            [gender] => groom 
            [price] => 12133 
            [description] => Item Description 
            [rent_sale] => Rent 
            [features] => Key Features 
            [pid] => 5 
            [product] => sherwani 
            [available] => 10 
            [uid] => 4 
            [ucid] => 10 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [3] => stdClass Object 
        ( 
            [id] => 4 
            [name] => branded shoes for groom 
            [gender] => groom 
            [price] => 3232 
            [description] => Item Description 
            [rent_sale] => sale 
            [features] => Key Features 
            [pid] => 6 
            [product] => foot wear 
            [available] => 9 
            [uid] => 4 
            [ucid] => 12 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [4] => stdClass Object 
        ( 
            [id] => 5 
            [name] => watches for groom 
            [gender] => groom 
            [price] => 33232 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 7 
            [product] => watches 
            [available] => 10 
            [uid] => 2 
            [ucid] => 18 
            [rating] => 24 
            [rating_users] => 6 
        ) 
        [5] => stdClass Object 
        ( 
            [id] => 6 
            [name] => watches for groom 
            [gender] => groom 
            [price] => 12133 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 8 
            [product] => watches 
            [available] => 10 
            [uid] => 1 
            [ucid] => 18 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [6] => stdClass Object 
        ( 
            [id] => 9 
            [name] => Branded bags 
            [gender] => bride 
            [price] => 22353 
            [description] => branded bags for sale 
            [rent_sale] => Sale 
            [features] => branded bags for sale 
            [pid] => 9 
            [product] => bags 
            [available] => 7 
            [uid] => 1 
            [ucid] => 3 
            [rating] => 53 
            [rating_users] => 20 
        ) 
        [7] => stdClass Object 
        ( 
            [id] => 10 
            [name] => Branded bags 
            [gender] => bride 
            [price] => 22353 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 10 
            [product] => bags 
            [available] => 8 
            [uid] => 1 
            [ucid] => 3 
            [rating] => 39 
            [rating_users] => 16 
        ) 
        [8] => stdClass Object 
        ( 
            [id] => 11 
            [image] => 1582863614kurta.jpg 
            [pid] => 4 
            [uid] => 2 
        ) 
    ) 
)

Below is the error what I'm getting:

Severity: Notice Message: Undefined index: pid Filename: views/welcome_message.php Line Number: 33

3
  • Severity: Notice Message: Undefined index: pid Filename: views/welcome_message.php Line Number: 33? where is that line? can we see your view page code? Commented Jul 25, 2017 at 7:17
  • try like this : echo $ff->pid.'hh<br>'; Commented Jul 25, 2017 at 7:20
  • I have posted a answer for you should help Commented Jul 25, 2017 at 8:24

5 Answers 5

2

Because it is Codeigniter Object, not an Array. And also see that your result object array is inside 0 index. So, try fetching and getting data this way.

<?php foreach ( $fetch[0] as $ff) { 
     echo $ff->pid;
} ?>

Thank You.

Sign up to request clarification or add additional context in comments.

Comments

0

Try your foreach like this:

foreach ($fetch[0] as $ff)
{
     echo $ff["pid"].'hh<br>';
}  

Comments

0

Try this:

$objects = json_decode($fetch);
$finalResult = (Array)$objects[0];
foreach($finalResult as $result){
    echo $result->pid.'<br />';
}

Comments

0
foreach ($fetch[0] as $ff)
{
 echo $ff["pid"];
}  

1 Comment

Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
0

Try $query->result_array() instead of $query->result() If you read through the entire user guide, it should answer a few of your questions.

Better way

$this->load->model('fetchindex');

$data['fetch'] = $this->fetchindex->fetch();  

// You can load views like so instead of using includes on view.
$this->load->view('header');
$this->load->view('welcome_message',  $data);
$this->load->view('footer');

Model

Filename: Fetchindex.php

<?php 

class Fetchindex extends CI_Model {

public function fetch()
{
    $data = array();

    $query = $this->db->get('selleritem');

    foreach ($query->result() as $result)
    {
        $this->db->where('pid', $result->pid);
        $this->db->limit(1);
        $sub_query = $this-db->get('selleritemimages');

        if ($sub_query->num_rows() > 0) 
        {

            foreach ($sub_query->result_array() as $row)
            {
                $data[] = array(
                   'somecolumn' => $row['somecolumn']
                   // Add other items you need
                );

            }
        }
    }

    return $data;
}

}

View

<?php foreach ($fetch as $item) {?>
     <?php echo $item["somecolumn"];?><br/>
<?php }?>  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.