I'm using CodeIgniter and I'm struggling with arrays, I need to do a mysql db lookup in the controller and then pass an array to the view so I can loop through the array in the view to display a list of results. I need the array to contain two fields per row and ID and Name so eg
row1 ID = 1 , Name = Hello row2 ID = 2 , Name = World
i've been looking al over the net and I can do this by just passing either ID or Name but i cant figure out how to pass them both in the array.
//get open cases
$this->db->where('UserID',$this->session->userdata('ID'));
$query = $this->db->get('cases');
if ($query->num_rows() > 0)
{
$cases = array();
foreach ($query->result() as $row)
{
// ive tried this
$cases[]=$row->ID,$row->CaseName;
// and this
$cases[]=($row->ID,$row->CaseName);
//but they both dont work :(
}
I can understand basic arrays but have never really had to use them before and now im using CI and i cant simply put the sql lookup in the view and loop through it there. I know the sql lookup should be in model but im not fussed about that at the moment, just want to get the array filled out and passed to the view, any help would be appreciated.
//EDIT this below function works fine but is it how yours suppposed to use CI
function viewCase()
{
$caseID = $this->uri->segment(3);
$this->db->where('ID',$caseID);
$this->db->where('UserID', $this->session->userdata('ID'));
$query = $this->db->get('cases');
if ($query->num_rows() == 1)
{
foreach ($query->result() as $row)
{
$data['caseID']= $row->ID;
$data['caseName'] = $row->CaseName;
$data['fileName'] = $row->FileName;
$data['statusID'] = $row->Status;
$data['statusName']= "not set yet";
//we need the status friendly name
$this->db->where('ID', $data['statusID']);
$query2 = $this->db->get('case_status');
if ($query->num_rows() == 1)
{
foreach ($query2->result() as $row2)
{
$data['statusName']=$row2->Name;
}
}
}
$data['fName']= $this->session->userdata('fName');
$data['lName']= $this->session->userdata('lName');
$data['email']= $this->session->userdata('email');
$data['fullName']= $this->session->userdata('fName')." ".$this->session->userdata('lName');
$this->load->view('head',$data);
$this->load->view('header_logged_in',$data);
$this->load->view('nav');
$this->load->view('view_case_view',$data);
$this->load->view('footer');
}
else
{
$data['fName']= $this->session->userdata('fName');
$data['lName']= $this->session->userdata('lName');
$data['email']= $this->session->userdata('email');
$data['fullName']= $this->session->userdata('fName')." ".$this->session->userdata('lName');
$this->load->view('head',$data);
$this->load->view('header_logged_in',$data);
$this->load->view('nav');
$this->load->view('view_case_error_view');
$this->load->view('footer');
}
}