Welcome Guest, Not a member yet? Register   Sign In
Why is it only returning 1
#1

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

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB