Noob! Accessing a variable from an array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Noob! Accessing a variable from an array (/showthread.php?tid=29452) Pages:
1
2
|
Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]trickymh[/eluser] Hello, I apologise if this has been covered before. I have searched, but only found one similar question, and the fix didn't work for me. All I want to do is retrieve the value of a field, and use it in a view. My Controller: Code: function index() My model code: Code: function get_resource() And my view code (well, the important bit) Code: <div><?php echo $results->name;?></div> where 'name' is the name of the field. For testing's sake there is only one row in the 'resources' table. I get the following errors: undefined variable: results trying to get property of non-object I have tried so many different syntaxes and ways of doing this. I'm going mental hehe. Thanks for any help Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]Adam K[/eluser] You have to notice, that you're returning 'results' into your $data array under index 'resources'. So in view, rather than Code: echo $results->name; Code: echo $resources->name; Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]trickymh[/eluser] [quote author="Adam K" date="1270918560"]You have to notice, that you're returning 'results' into your $data array under index 'resources'. So in view, rather than Code: echo $results->name; Code: echo $resources->name; Thanks Adam, when I do this I get a 'Trying to get property of non-object' error on the view code. Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]John_Betong[/eluser] Try this: Code: // model edit: changed view $result-> to $resources->name Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]Adam K[/eluser] @trickymh - that probably means, that what you get as return from your model isn't what you think it is - this usually happens, when you 'think' you're returning db array, but somehow, something else is returned (i.e. -1 for not found in database). Try following in your view: Code: <?php Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]John_Betong[/eluser] If you are using Windows try this - the output will be formatted and easier to read: Code: echo '<pre>'; Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]trickymh[/eluser] Hi Adam, I tried your code and got the original errors. I have tried the var_dump thing before, and it shows the data I'm after, which is why it's so frustrating i can't access it. This is the output: object(stdClass(2) { ["id"]=>string(1)"1"["name"]=>string(9)"Villa One" } Thanks for your time! Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]trickymh[/eluser] [quote author="John_Betong" date="1270919884"]If you are using Windows try this - the output will be formatted and easier to read: Code: echo '<pre>'; Thanks John, on a mac Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]Adam K[/eluser] I may be obvious, but it works like this: everything you assign to $data as (i.e.) Code: $data['myIndex'] Code: $myIndex Code: $this->load->view($view,$data); so, it comes down to fact, that you're referencing to different values. Noob! Accessing a variable from an array - El Forum - 04-10-2010 [eluser]trickymh[/eluser] [quote author="John_Betong" date="1270919030"]Try this: Code: // model edit: changed view $result-> to $resources->name [/quote] John, I love you, it worked! Sorry, i didnt see you'd posted the orignal code suggestion, and i didnt see the edit you made till a minute ago. I'm just going to figure out why it worked now... Many thanks, Matt |