Welcome Guest, Not a member yet? Register   Sign In
Get query results in model
#1

[eluser]rochellecanale14[/eluser]
Hello guys just want to ask about models.
I created a model called Entry_Model and I have two functions. One is to view all entries and the other one is to count. I did this by using a traditional query. I used this code:
Code:
$sql1 = "SELECT *  FROM entries";
$result1 = $this->db->query($sql1);

My problem is when i tries to get the result from my model and then passing thru the controller I got an error like this:
Code:
Fatal error: Call to a member function result_row() on a non-object in
Here's the sample code:

Entry_Controller:

Code:
public function index(){

                $data['title'] = "Welcome back ".$this->session->userdata('username')." !";
                
                $data['viewEntries'] = $this->entry_model->viewAllEntry();
                $data['countAllEntries'] = $this->entry_model->countAllEntry();
                
                $this->load->view('Admin/header_admin/admin_header',$data);
                $this->load->view('Admin/content_admin/display_blog',$data);
                $this->load->view('Admin/footer_admin/footer_admin',$data);
                
            }
   }
Entry_Model:
Code:
public function viewAllEntry(){
            
            $data[] = array();
            
            $sqlViewAllEntry = "SELECT * FROM tbl_entries";
            $data['viewEntries'] = $this->db->query($sqlViewAllEntry);
            
            return $data;
            
        }
        
        public function countAllEntry(){
            
            $data[] = array();
            
            $sqlCountEntries = "SELECT COUNT(fk_memid) AS all_entries FROM tbl_entries";
            $data['countAllEntries'] = $this->db->query($sqlCountEntries);
            
            return $data;
            
        }
View:

Code:
foreach($countAllEntries->result_row() as $row){
                $allRecord = $row['all_entries'];
}

echo $allRecord; //return error

But when i try not to use a model it works. Here's what i did in my view.
Code:
$sqlCountEntry = "SELECT COUNT(*) AS entry FROM tbl_entries";
            $result = $this->db->query($sqlCountEntry);
            foreach($result->result_array() as $row){
                echo $row['entry'];
            }

What's my error? Or is there another way to perform this? By the way I used tradtional query because as of now I am confused with the use of active query. Specially when I performing a join statement. Please help me guys. Thanks.
#2

[eluser]xeroblast[/eluser]
$data['viewEntries'] = $this->db->query($sqlViewAllEntry)->result();

$data['countAllEntries'] = $this->db->query($sqlCountEntries)->row();




Theme © iAndrew 2016 - Forum software by © MyBB