CodeIgniter Forums
Retrieving from database - 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: Retrieving from database (/showthread.php?tid=52166)



Retrieving from database - El Forum - 05-31-2012

[eluser]Coder Death[/eluser]
Hello to all,

I have my model with this function :
Code:
function getReportByDate(){
        
        $result = Array();
        
        $i = 0;
        
        $q = "SELECT * FROM _my_report";
        
        $q = $this->db->query($q);
        
        if($q->num_rows() > 0)
        {
            foreach($q->result() as $q2){
                
                $result[$i] = $q2;
                $i++;
            }
        }
            
        return $result;
    }

in my controller I do this :
Code:
$this->load->model('getreport');

            $totalreport = $this->getreport->getReportByDate();

            if(count($totalreport) != 0 ){
                $final = Array();
                $final['a'] = $totalreport;
            }

And in the view I get the data like this :
Code:
<?php
        foreach($a as $list){
            
            echo $list[$i];
$i++
}

But I have this error :
Code:
Object of class stdClass could not be converted to string

Please how can I solve this problem ?
Thanks



Retrieving from database - El Forum - 05-31-2012

[eluser]weboap[/eluser]
1 - in the model don't loop thru result
use
Code:
...
if($q->num_rows() > 0)
        {
        return $q->result_array();
         }
return false
....



2- in controller

Code:
//will be only you are returninf a false if no data.

$this->load->model('getreport');

$final['totalreport'] = $this->getreport->getReportByDate();

this->load-view('your_report_view', $final);

in view


Code:
//remimber the report we are getting in the view is either false or an array.
//choose yoru test.
<?php

if(!$totalreport){

echo 'no data';
}else{
        foreach($totalreport as $item){
            
            echo $item['table_filed1'];
            echo $item['table_filed1'];
            echo $item['table_filed1'];
             ...

          }
}

good luck



Retrieving from database - El Forum - 05-31-2012

[eluser]Coder Death[/eluser]
@Weboap,

yes it works, very simple lol

Thanks a lot