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.


Messages In This Thread
Two table query and then using the two arrays question - by El Forum - 07-17-2011, 01:33 PM
Two table query and then using the two arrays question - by El Forum - 07-17-2011, 01:44 PM
Two table query and then using the two arrays question - by El Forum - 07-17-2011, 02:15 PM
Two table query and then using the two arrays question - by El Forum - 07-17-2011, 05:44 PM
Two table query and then using the two arrays question - by El Forum - 07-19-2011, 02:23 PM
Two table query and then using the two arrays question - by El Forum - 07-19-2011, 04:14 PM
Two table query and then using the two arrays question - by El Forum - 07-19-2011, 04:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB