Welcome Guest, Not a member yet? Register   Sign In
Display one record, array or object?
#1

[eluser]runemedia[/eluser]
Hi all. I'm a CI newbie coming from using Dreamweaver server behaviors. I usually code using the DW behavior format, but doing it all by hand. That said, as a learning exercise, I'm trying to just pull one specific record from the database and display it. I've gotten the code working where I can pull all the records I need and use a foreach to display them. But now I just want to show one. And that's where I'm lost.

My model:
Code:
function retrieve_member() {
        $query = $this->db->get_where('users', array('u_id' => 1));
        return $query->result();
    }

My controller:
Code:
function index() {
        $this->load->model('member_model');
        $data['record'] = $this->member_model->retrieve_member();
        $this->load->view('member_view',$data);
    }

My view:
Code:
<?php echo $record->u_fname; ?>

My error:
Message: Trying to get property of non-object
Filename: views/member_view.php
(The line # references the view code I've shown above).

I've also tried:
Code:
<?php echo $record['u_fname']; ?>
Which gives me a 500 error.

I thnk I'm missing something about arrays and/or objects, but I've now bumped into my CI knowledge limit. Any guidance would be appreciated. Thanks.
#2

[eluser]nedimtx[/eluser]
Try:

Code:
print_r($record);
and tell me what does it return?

Also, try:

Code:
foreach($record as $r){
print_r($r);
}

N.
#3

[eluser]Wondering Coder[/eluser]
in your model it show that your showing all the data in your table. try doing it like this
model
Code:
function retrieve_member() {
        $query = $this->db->get_where('users', array('u_id' => 1));
        return $query();
    }

controller
Code:
function index() {
        $this->load->model('member_model');
        $data['record'] = $this->member_model->retrieve_member()->row();
        $this->load->view('member_view',$data);
    }
#4

[eluser]runemedia[/eluser]
[quote author="nedimtx" date="1296954741"]Try:

Code:
print_r($record);
and tell me what does it return?

Also, try:

Code:
foreach($record as $r){
print_r($r);
}
[/quote]

@nedmitx
Thanks the
Code:
print_r($record);
shows:
Array ( [0] => stdClass Object ( [u_id] => 1 [u_fname] => Patrick ...<snip>))

The foreach code gave me a 500 error.

@Wondering Coder
That code gives me a 500 error, which doesn't even load the View.
#5

[eluser]runemedia[/eluser]
[quote author="nedimtx" date="1296954741"]Try:

Code:
print_r($record);
and tell me what does it return?

[/quote]

Thanks nedimtx. That hint was enough to point me in the right direction. I don't know if my code is "good", but here's what now works in my view
Code:
echo $record[0]->u_fname;

Thanks again. Although, if I'm doing this "wrong" or making some newbie mistake, please feel free to point it out.
#6

[eluser]nedimtx[/eluser]
Np, runemedia.

I'm new to CodeIgniter too! Your model is fine and just use powerful user guide.
Check out:
http://ellislab.com/codeigniter/user-gui...index.html

I don't think you are doing wrong.
Also check out this:
http://www.php.net/manual/en/control-str...oreach.php

In addition, those video tutorials are great:

http://net.tutsplus.com/sessions/codeign...m-scratch/

Please, be careful if are you using CI 2.0., because as I can see we need to use "extends CI_Controller".

N.
#7

[eluser]d1a8lo24[/eluser]
Try the following this is how i usually get 1 record.

In your model.
Code:
function retrieve_member() {
        $query = $this->db
                      ->where('u_id', 1)
                      ->get('users');
        // Always test
        if ($query->num_rows() == 1)
        {
          return $query->row();
        }
        return FALSE;// return anything you want NULL, FALSE, STRING
    }

Your controller which is fine
Code:
function index() {
        $this->load->model('member_model');
        // If nothing is return make sure to do something.
        // You may want to redirect or test in your view and do an if statement.
        $data['record'] = $this->member_model->retrieve_member();
        $this->load->view('member_view',$data);
    }

Now you can try to echo something.
Code:
// It might return an error if nothing is return.
&lt;?php echo $record->u_fname; ?&gt;
The error message: Trying to get property of non-object is because the object doesn't exist in your return since you're returning an array of objects, so if you do a foreach loop even though is only 1 record you will be able to echo out a result in your loop.

So in my code you're retuning one row as object and you don't have to do a foreach loop.

Your code is not wrong is how you echo things out. is just the type of result you're getting thats all.

Hope this helps.
#8

[eluser]runemedia[/eluser]
@nedimtx: Thanks for the tips.

@d1a8lo24: Thanks for the code and advice (and the code works for me).

Now to continue with my learning.




Theme © iAndrew 2016 - Forum software by © MyBB