Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
try to do sum.., but resulting error Database_Result_Cached could not be converted to string
  • I'm having a problem implementing sum in FuelPHP, and then i try to search on the internet as a solution. I found the code to do sum. 

    here's a query in my controller:

     $data['total'] = DB::select(DB::expr('SUM(jumlah) as count'))->from('itempengadaans')->where('paketpengadaan_id', $pengadaan)->execute();

    $this->template->content=View::forge('view',$data);



    and in view I just want to echo the result 
    <?php echo $total ?>

    but the result Database_Result_Cached could not be converted to string.

    can somebody help me please.., thax for your help.., 


  • itcanitcan
    Accepted Answer
    query should be:

     $data['total'] = DB::select(DB::expr('SUM(jumlah) as count'))->from('itempengadaans')->where('paketpengadaan_id', $pengadaan)->execute()->current();
  • @itcan when I add current() to query it shows array to string conversion error.. how to fix that... thx
  • Finally.., FOund it after hours of trying.
    Hope this would useful to others..., eventhough I'm not so sure it's best solution 
     in COntroller
    $data['total'] = DB::select(DB::expr('SUM(jumlah) as count'))->from('itempengadaans')->where('paketpengadaan_id', $pengadaan)->execute()->as_array();
    $this->template->content=View::forge('view',$data);

    in view
    <?php echo array_sum($total[0]); ?>

    it will give you the correct result..., but round and round process :P

  • If you run any query to the database using DB::select()-&gt;execute() you will end up with an array no matter how many data you requested. Your query (the one you wrote first) thus returns an array which contains only one key (total) and only one value for that key (the sum of that column).
    Therefore, if you want to echo it, you have to do &lt;?php echo $total['total']; ?&gt; or you'll do some pre-processing in the model to only return that value and 0 (zero) otherwise
  • @philipptempel
    hehehe..., That's why I ended up using array_sum() because I don't know any other way to access the result. Thanks for your help.., I'll try it. deeply thanks

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion