0

I want echo my list of trip, when I try print_r the value can show but when I echo the result always

Message: Undefined variable: adventure

Filename: views/profile.php

Line Number: 121

Backtrace:

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/profile.php

Line Number: 121

this is my controller :

 public function getListTrip(){         
    $this->load->model('userModel');        
    $data['adventure'] = $this->userModel->getTrip()->result_array();
    //echo ($data);
    $this->load->view('profile', $data);        
 }

and this is my model :

function getTrip(){
    $userId = $this->session->userdata('user_id');    
    return $this->db->get_where('adventure',['user_id' => $userId]);
}

this is my view

                        <table>
                           <?php
                            foreach ($adventure as $b){
                                echo "<tr>
                                <td>$b->name</td>
                                <td>$b->place</td>
                                <td>$b->category</td>

                                </tr>";
                            }

                        ?>
                        </table>

so how should I edit my code to make the value show in my page whit echo or foreach not in print_r... thanks a lot

1

4 Answers 4

1

Change your model

function getTrip(){
    $userId = $this->session->userdata('user_id');    
    $query= $this->db->get_where('adventure',['user_id' => $userId]);
    return $query->result();
}

Also chnage in your controller

$data['adventure'] = $this->userModel->getTrip()->result_array();

To

$data['adventure'] = $this->userModel->getTrip();
Sign up to request clarification or add additional context in comments.

1 Comment

its work dude, thanks.. but I should see this in view of my controller /getListTrip cannot see in my /profile
1

echo is used to output one or more strings, since your $data is an array you see the error, you need to pass data to view and iterate it in the view itself, like:

public function getListTrip(){
    $this->load->model('userModel');
    $data['adventure'] = $this->userModel->getTrip()->result_array();
    //pass data to your view
    $this->load->view('yourviewname', $data);
}

For more information check Codeigniter Views

Update

Check if your array is not empty before trying to iterate thru it, like in your view::

<?php
if( !empty($adventure) ) {
    foreach($adventure as $b):
?>
        <tr>
            <td><?php echo $b['name']; ?></td>
            <td><?php echo $b['place']; ?></td>
            <td><?php echo $b['category']; ?></td>
        </tr>
<?php
    endforeach;
}
?>

2 Comments

I try what your suggest, and still got some error in my view page, please help me where is my error??
@FahmiSalmin check the added code. Btw, since the reult is array, you cannot access the fields with '->' operator.
0

You cannot echo the content of an Array.

So whenever you want to view the Content of an array use print_r($arrayName);

And When you just want to print any variable just use echo $variableName;

Comments

0

I don't see any issue with your code. If you're not using autoloader to load the session library that might be your issue:

$this->load->library('session');

You can check the documentation Codeigniter sessions

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.