Welcome Guest, Not a member yet? Register   Sign In
Two table query and then using the two arrays question
#1

[eluser]steve hunter[/eluser]
Hi all,

First post here, really excited to be getting stuck into this framework. Looks great but i have hit a problem on my test project and was wondering if you could help.

I want to echo out the colour next to the name of a widget instead of the FK id.

Quote:WIDGETS TABLE
ID NAME COLOURID
1 WidgetA 1
2 WidgetB 2

------

COLOUR TABLE
ID NAME
1 BLUE
2 RED

MODEL
Code:
class Widgets_model extends CI_Model {

    function __construct()
    
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_colours()
    {
       $queryg = $this->db->query('SELECT * FROM colours');
       return $queryg->result();
    }
    
    function get_widgets()
    {
       $queryr = $this->db->query('SELECT * FROM widgets');
       return $queryr->result();
    }
}

CONTROLLER
Code:
class Widgets extends CI_Controller {

  public function index(
   {
    $this->load->model('Widgets_model');
      
    $data['widgets'] = $this->Widget_model->get_widgets();
    $data['colours'] = $this->Widget_model->get_colours();
    
    $this->load->view('widget_view', $data);        
   }

}

VIEW
Code:
foreach($widgets as $row){
        echo $row->NAME .  " - " . $row->COLOURID;
   }
Which works and outputs 'Widget A - 1' but i want to output 'Widget A - Blue'

I have been playing with trying to output the colour next to the name for a bit with things like this, but am not getting anywhere:
Code:
foreach($widgets as $row){
        echo $row->NAME .  " - " . $colours[$row->COLOURID];
   }

If anyone could help I would really appreciate it.
#2

[eluser]brucebat[/eluser]
I think you would need to use mysql joins for that?
#3

[eluser]steve hunter[/eluser]
Thanks for the quick reply. I am used to just setting up a few SELECT queries to a few smaller tables and then storing that result in some simple array with an index, which is then used as a the key to pull out the value.

I would really like to achieve the same with codeigniter as later on I will use more than two tables and didn't want to write complicated joins with sql.
#4

[eluser]jmadsen[/eluser]
"Complicated joins" is part of being a developer. You should learn to do things correctly, not ask us to teach you how to do things in a bad way.


return $queryr->result(); is an object, not an array. If you echo your $colours variable you will see why your code is not working.

look through this page and you will see some variants on the code you are writing that will help you.
http://ellislab.com/codeigniter/user-gui...sults.html
#5

[eluser]steve hunter[/eluser]
Thanks Jeff, I have spent the time to learn about joins now and its saving me alot of extra work!

I have also started returning some queries as result_array()
#6

[eluser]Mainboard[/eluser]
well, you can try with this

Model
Code:
function get_widgets_colours() {
     $q = $this->db->select('*')->from('widgets')->join('colour', 'widgets.id = colour.id') ;
      $ret['rows'] = $q->get()->result();
     return $ret;
    }
#7

[eluser]Mainboard[/eluser]
it works Wink




Theme © iAndrew 2016 - Forum software by © MyBB