1

an update from my previous question..

this is my code in the model.

function member_here()
{
$this->db->select('');
$this->db->from('membership');
$this->db->where('username',$this->input->post('username'));

$q=$this->db->get();

if($q->num_rows() > 0) {
$data = array('first_name');
foreach($q->result() as $row) {
    $data=$row;
}
return $data;
}
}

and this is my code in the view form:

<?php


$CI =& get_instance();
$CI->load->model('membership_model');
$result = $CI->membership_model->member_here();
print_r($result);
?>

now. i have a problem.

the output is this:

stdClass Object ( [id] => 10 [first_name] => Marishka [last_name] => Villamin [username] => marishkapv [password] => 01aef487205966f24dd694ca4153ccbb [email_address] => [email protected] )

i dont need that output. instead i want my output to be Marishka which is the value of the first_name field.

help please

2 Answers 2

3
echo $result->first_name;

You just need to echo out the part of the object you want, not the entire object. And for the record you shouldn't be getting any data from the model in the view, that should all be done in the controller.

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

9 Comments

You're quite welcome. I would suggest you check out these tutorials net.tutsplus.com/sessions/codeigniter-from-scratch They were a great help to me when I started CI. They'll help you understand the MVC concept too.
follow up question though. i am getting the results i want. but the problem is, the output is the last row in the database. lets say i have 3 rows and I logged in using the 1st user name, it is echoing the 3rd user's infromation. i have been watching that! you answered my question before and your suggestion did a lot for me, i was able to do what i was doing and it helped me too :)
That makes no sense at all to be honest, not with the code above at least. Are you sure the usernames are different?
Yes. The usernames are different. Sorry. I should make it clear. there are 3 people in my database. johndoe, marishka and dennis. if I log in any of the three, it outputs the information for dennis. is it my foreach thats making the results wrong?
I think theres something wrong with my: $this->db->where('username',$this->input->post('username'));
|
0

I guess your problem has been solved, but next time, you could specify the particular column you wish to fetch from your database.

function member_here()
{
    //This is where your specify the column
    $this->db->select('first_name');
    $this->db->from('membership');
    $this->db->where('username',$this->input->post('username'));

    $q=$this->db->get();

    if($q->num_rows() > 0) {
        $data = array('first_name');
        foreach($q->result() as $row) {
            $data=$row;
        }
        return $data;
    }
}

Like so..

I hope this was helpful

1 Comment

i still have a problem though. its not just the first name i am trying to select thats why i left it blank. but the thing is, instead of echoing the data for that specific username, it echoes the data of a different username. to be exact. lets say i have 3 people in my database. johndoe, marishka and dennis. i logged in john doe, so it means when i go to the home page. john doe's information must appear. instead, the information of dennis is being displayed. i dont know which part went wrong. i am now stuck. please help. thank you so much!

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.