Welcome Guest, Not a member yet? Register   Sign In
Retrieving from database
#1

[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
#2

[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
#3

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

yes it works, very simple lol

Thanks a lot




Theme © iAndrew 2016 - Forum software by © MyBB