CodeIgniter Forums
single ds with MVC? - 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: single ds with MVC? (/showthread.php?tid=2810)



single ds with MVC? - El Forum - 08-25-2007

[eluser]Matthew90[/eluser]
Hello,

today I have started to work with CI and my first impression is really good :-)
As this is my first job with a framework, the MVC-Model and strict OOP-Programming is new for me.
Now I have an problem with the MVC-Model.
It concerns the following...
I want to return a single data set... without oop no problem:
Code:
$this->db->where(array('id' => $this->uri->segment(3)));
        $query = $this->db->get('projects');
        
        
        if ($query->num_rows() > 0) {
            $row = $query->row();
        
               echo $row->id;
               echo $row->description;
                    
           //...
        }
But how can I do it, when I want to work with the model class?
I tried a lot of iddeas but without a result...
At the moment my code is as followed:

project_model.php
Code:
function get_requested_project($where)
    {
        $this->db->where($where);
        $query = $this->db->get('projects');
        
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return false;
        }
    }

admin.php
Code:
function insert_project()
    {
        $this->load->model('project_model');
        
        $row=$this->project_model->get_project(array('id' => $this->uri->segment(3)));
        if ($row != false) {
               echo $row->id;
               echo $row->description;
                    
            $this->load->view('project_insert',$data);
        }

I hope you know what I mean in spite of my bad English and I look forward to get an answer ;-)

Greetings

Matthew


single ds with MVC? - El Forum - 08-28-2007

[eluser]Matthew90[/eluser]
Does nobody have an idea?


single ds with MVC? - El Forum - 08-28-2007

[eluser]obiron2[/eluser]
if you replace

Code:
return $query->row();

with
Code:
return $query->result();

You will get an object back which is an array of objects, each one being a row of data.

if you have returned the object to $resultSet you can then manipulate it by using the array offset and the row field names

Code:
print $resultSet[3]->fieldname;

or
Code:
foreach ($resultSet as $resultRow)
{
  print $resultRow->fieldname1 . "<BR>" $resultRow->fieldname2;

}

you can even take each resultRow and convert that to a Key->Value array

Code:
foreach ($resultRow as $resultKey => $resultValue)
{
  print $resultKey . ":" . $resultValue . "<BR>";
}



single ds with MVC? - El Forum - 08-28-2007

[eluser]Matthew90[/eluser]
thx for your help obiron2, now it works fine :lol: