Display one record, array or object? |
[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() { My controller: Code: function index() { 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']; ?> 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.
[eluser]nedimtx[/eluser]
Try: Code: print_r($record); Also, try: Code: foreach($record as $r){ N.
[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() { controller Code: function index() {
[eluser]runemedia[/eluser]
[quote author="nedimtx" date="1296954741"]Try: Code: print_r($record); Also, try: Code: foreach($record as $r){ @nedmitx Thanks the Code: print_r($record); 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.
[eluser]runemedia[/eluser]
[quote author="nedimtx" date="1296954741"]Try: Code: print_r($record); [/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.
[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.
[eluser]d1a8lo24[/eluser]
Try the following this is how i usually get 1 record. In your model. Code: function retrieve_member() { Your controller which is fine Code: function index() { Now you can try to echo something. Code: // It might return an error if nothing is return. 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.
[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. |
Welcome Guest, Not a member yet? Register Sign In |