Welcome Guest, Not a member yet? Register   Sign In
array frustration : ripping hair out
#1

[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

class Games extends Controller {

    function Games()
    {
        parent::Controller();
        $this->load->library('games_library');    
    }
    
    function index()
    {
        $this->load->view('games/games_home.php');
    }
    
    function play($game_id, $game_name = '')
    {
        $data['data'] = $this->games_library->renderGameData($game_id);
        $this->load->view('games/games_play.php', $data);
    }
}
?>

here is my library

Code:
<?php

class Games_library
{

    function Games_library()
    {
        $this->CI =& get_instance();
        $this->CI->load->model('games_model', '', TRUE);    // Start up the trusty database shit
    }
    
    public function renderGameData($game)
    {
        
        if(is_numeric($game))
        {
            $result = $this->CI->games_model->getGameData($game);
            return $result;
        }
        else
        {
            redirect("errors/404");
        }
    }

}

?>

here is my model

Code:
<?php

class Games_model extends Model {

    function Games_model()
    {
        parent::Model();
    }
    
    function getGameData($game)
    {
        $query = $this->db->query("SELECT * FROM `game_games` WHERE ID = '$game' LIMIT 1");
        return $query->result();
    }
}

?>

and here is my view

Code:
<?php

print( $data['name']);
?>

please please please help me lol im gonna throw my laptop out the window soon!
#2

[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
#3

[eluser]Tom Schlick[/eluser]
i still get this error when i try to do that "Trying to get property of non-object"
#4

[eluser]sophistry[/eluser]
edited previous... bump.
#5

[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
#6

[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.
#7

[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

class Games extends Controller {

    function Games()
    {
        parent::Controller();
        $this->load->library('games_library');    
    }
    
    function index()
    {
        $this->load->view('games/games_home.php');
    }
    
    function play($game_id, $game_name = '')
    {
        $data['games_result'] = $this->games_library->renderGameData($game_id);
        $this->load->view('games/games_play.php', $data);
    }
}
?>

and here is the view

Code:
<?php

print($games_result->name);
?>

nothing changed in these but here is the library...

Code:
<?php

class Games_library
{

    function Games_library()
    {
        $this->CI =& get_instance();
        $this->CI->load->model('games_model', '', TRUE);    // Start up the trusty database shit
    }
    
    public function renderGameData($game)
    {
        
        if(is_numeric($game))
        {
            $result = $this->CI->games_model->getGameData($game);
            return $result;
        }
        else
        {
            redirect("errors/404");
        }
    }

}

?>

and here is the model
Code:
<?php

class Games_model extends Model {

    function Games_model()
    {
        parent::Model();
    }
    
    function getGameData($game)
    {
        $query = $this->db->query("SELECT * FROM `game_games` WHERE ID = '$game' LIMIT 1");
        return $query->result();
    }
}

?>
#8

[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));
return $query->row();

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)
#9

[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! :-)
#10

[eluser]sophistry[/eluser]
darn it, RJ beat me to the punch.




Theme © iAndrew 2016 - Forum software by © MyBB