array frustration : ripping hair out |
[eluser]Tom Schlick[/eluser]
ok i have my controller going to my database and pulling the information i need and passing the array of that row back down to my view. the only problem is that i cant figure out how to get the single feilds instead of the whole array. i know the data has passed down because i can do print_r() and get the array printed with all of the correct information but when i try to echo() or print() $data['name'] it says "Undefined index: name" when i try to use the variable name for it ($name instead of $data['name']) it says "Undefined variable" ahhhh! here is my controller Code: <?php here is my library Code: <?php here is my model Code: <?php and here is my view Code: <?php please please please help me lol im gonna throw my laptop out the window soon!
[eluser]sophistry[/eluser]
EDIT: oops, sorry, i thought you were still in the controller. i see you've already assigned the data to data. (that's not a good idea to use "data" as a variable name as well as a key - you confuse yourself as well as possibly overwriting your variables - just give them good old descriptive variable names like $data['games_result']). anyhow, in the view (as you'll see when you read the manual ;-) ) the value is just the name you assigned into the $data variable in the controller. in your case it looks like it is a db object you assigned into the view so, you'll need to do... Code: $games_result->name
[eluser]Tom Schlick[/eluser]
i still get this error when i try to do that "Trying to get property of non-object"
[eluser]Tom Schlick[/eluser]
still getting the same error when i try to print it i changed $data['data'] to $data['games_result'] and changed echo $data['data']; to echo $games_result->name; and i still get that and i did read the manual like 40 times to try to get this but im confused as hell lol
[eluser]sophistry[/eluser]
well, now we're getting somewhere... :-) are you sure that the db result that you assign to the view in the controller has a real result in it? are you sure that you are using the properly capitalized names of your field? name and not Name for instance. BTW, post your new code.
[eluser]Tom Schlick[/eluser]
yes i am sure when i did print_r($data) before it would print out the entire array of data the feild title is 'name' the only thing in the field that is capitalized is the ID feild. here is my controller again Code: <?php and here is the view Code: <?php nothing changed in these but here is the library... Code: <?php and here is the model Code: <?php
[eluser]Rick Jolly[/eluser]
To get a single result use: Code: $query = $this->db->query("SELECT * FROM `game_games` WHERE ID = ? LIMIT 1", array($game)); Also, you may have reduced your code for clarity, but you should check for a result before trying to print it in your view. Also, you may be aware of this as well, but watch out for sql injection. Verify that game_id is an integer and use query bindings. (Edited the code for query bindings)
[eluser]sophistry[/eluser]
ok, cool. i believe that even though you are LIMITing the query your result object returns as an array of nested record objects with just one record object: http://ellislab.com/codeigniter/user-gui...sults.html i may be mistaken but, perhaps you need to foreach the array? i usually use the $query->row() or $query->row_array() methods to avoid that. sorry if i overlooked that in previous posts - sometimes there are so many ways to go about doing something in CI that when you get your rhythm in one way it seems strange to do it another! :-)
|
Welcome Guest, Not a member yet? Register Sign In |