Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am newbie in CodeIgniter. When I try to data passing with controller and view and model, I have got this:

Syntax error: Unexpected '$data'(T_VARIABLE) in c:\xampp\htdocs\ci\application\controllers\users.php on line 10

Here is my controller users.php code:

<?php
class Users extends CI_Controller
    {
        public function show()
            {
                $data['results']=$this->user_model->get_users();
                $this->load->view('user_view',$data);
             }
    }
?>

At views folder user_view.php code:

<body>
<?php
foreach($results as $object){
    echo $object->username;
}
?>
</body>

At model folder user_model.php code

<?php
class User_model extends CI_Model
    {
        public function get_users()
            {
                $query=$this->db->get('users');
                return $query->result();
            }
    }
?>

How can I solve this error?

share|improve this question
up vote 1 down vote accepted

Why do your error says c:\xampp\htdocs\ci\application\users.php shouldn't it have been c:\xampp\htdocs\ci\application\controller\users.php`

share|improve this answer
    
well good catch – Linus Sep 4 '15 at 5:32
    
Thanks for reply. I corrected it. still getting same error. – NIL SAGOR Sep 4 '15 at 8:51

Use $results in foreach loop and check

share|improve this answer
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Gerald Versluis Sep 4 '15 at 6:58
    
Thanks for your comment. I changed it and tried but still got same error. – NIL SAGOR Sep 4 '15 at 8:54

In your controller

public function show()
{
    $data['results']=$this->user_model->get_users();
    $this->load->view('user_view',$data);
}

In your model

public function get_users(){
    $query=$this->db->get('users');
    $result = $query->result_array(); //added
    return $result; //added
}

In view

<body>
<?php
    foreach($results as $new_results) //changed
    {
        echo $new_results['username']; //changed
    }
?>
</body>
share|improve this answer
    
Thanks for your comment. I checked it. But got same error. – NIL SAGOR Sep 4 '15 at 9:13
    
whats the error?? – Abdulla Nilam Sep 4 '15 at 9:13
    
the error is: severity: Parsing Error; Message: Syntax error, unexpected '$data' (T_VARIABLE); Filename: controllers/users.php – NIL SAGOR Sep 4 '15 at 9:25
    
no. check the line. this code is correct – Abdulla Nilam Sep 4 '15 at 9:27
    
sorry I bother you again. after correcting line I got two message: first one is Severity:notice; Message: use of undefined constant result-'result'; Filename:models/user_model.php and second one is Severity:Warning; Message:Invalid argument supplied for foreach(); filename:views/user_view.php – NIL SAGOR Sep 4 '15 at 9:57

You are trying to access wrong ARRAY in your view. In your controller array is $data['results'] not $data['result']. So you need to access $results in foreach loop.

In your view Change from

foreach($result as $object){

To

foreach($results as $object){
share|improve this answer
    
Thanks for your comments. I changed it but still got same error. – NIL SAGOR Sep 4 '15 at 8:53

First of all, you have to load your model as:

$this->load->model('User_model','user_model');

Then you can use the function defined in that model as :

$this->user_model->get_users();
share|improve this answer

You have one more error in your code i.e. you have not loaded the model in the function

update your code to add the following line in first line of show() function

$this->load->model('user_model');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.