CodeIgniter Forums
Why is it only returning 1 - 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: Why is it only returning 1 (/showthread.php?tid=40773)



Why is it only returning 1 - El Forum - 04-18-2011

[eluser]Unknown[/eluser]
Hi,
I'm completely new to codeigniter but trying to get it to work.
Currently I'm trying to pass results from a table into a dropdown menu. And I've got to the point where it is passing the results, but only 1 (not the whole array)
in my controller I have this
Code:
function members_area()
    {
        $username = $this->session->userdata('username');
        $queryG = $this->db->query("SELECT name FROM games WHERE author = '$username' ");
        
        if ($queryG->num_rows() > 0)
        {
            $data['games'] = array();
            foreach ($queryG->result_array() as $row)
            {
                $row['name'];
            }
        }
        $data['games']=$row;
        
        $this->load->view('logged_in_area', $data);
    }

in my view I got a simple dropbox like this
Code:
----html---
    <?php
    echo form_open('site/gamebuilder');
    echo form_dropdown('game', $games);
    echo form_submit('submit', 'Load Game');
    ?>
----html---

Can anybody tell me where I went wrong in this and how to fix it?
oh and if you're wondering it displays only the last row resulting from the query

Thx a lot in advance


Why is it only returning 1 - El Forum - 04-18-2011

[eluser]JHackamack[/eluser]
Try this:
Code:
$data['games'] = array();
        if ($queryG->num_rows() > 0)
        {
            foreach ($queryG->result_array() as $row)
            {
                $data['games'][] = $row['name'];
            }
        }



Why is it only returning 1 - El Forum - 04-18-2011

[eluser]bubbafoley[/eluser]
edit: JHackamack beat me to it Big Grin


Why is it only returning 1 - El Forum - 04-18-2011

[eluser]Unknown[/eluser]
thx that got it working


Why is it only returning 1 - El Forum - 04-18-2011

[eluser]TWP Marketing[/eluser]
Code:
function members_area()
    {
        $username = $this->session->userdata('username');
        $queryG = $this->db->query("SELECT name FROM games WHERE author = '$username' ");
        
        if ($queryG->num_rows() > 0)
        {
            $data['games'] = array();
            foreach ($queryG->result_array() as $row)
            {
                $row['name'];// <-- this doesn`t do anything
            }
        }
        $data['games']=$row; //<-- this is outside the loop and will only receive the LAST $row data
        
        $this->load->view('logged_in_area', $data);
    }
Try this instead:
Code:
function members_area()
    {
        $username = $this->session->userdata('username');
        $queryG = $this->db->query("SELECT name FROM games WHERE author = '$username' ");
        
        if ($queryG->num_rows() > 0)
        {
            $data['games'] = array();
            $i = 1; // initialize counter
            foreach ($queryG->result_array() as $row)
            {
                $i++;
                $data['games'][$i] = $row;
            }
        }
        $this->load->view('logged_in_area', $data);
Now loop through the $games array in your view and it should contain more than one entry
[edit] ok your way is simpler than mine...