Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6703083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:07:47+00:00 2026-05-26T07:07:47+00:00

The following code is a simple select statement that should use the array_sum function

  • 0

The following code is a simple select statement that should use the array_sum function to return a value. The result should be getting stored in an array and then getting added up in the SUM function:

$this->db->select('period')
     ->from('calcdata');

$query = $this->db->get()->result_array();
$query = array_sum($query);

echo "SUM " . $query . "\n" ;
return $query;

the result of this is “SUM 0” but it should be 147 after adding all values up in the period column.

The following code works so i don’t understand why the array would be any different from this:

$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";

I am using codeigniter to create the arrays, does anyone know what is wrong here?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:07:48+00:00Added an answer on May 26, 2026 at 7:07 am

    Try calling the content of the field instead, not of the whole result array:

    $this->db->select('period')->from('calcdata');
    
    $query = $this->db->get();
    
    $period_array = array();
    foreach ($query->result_array() as $row)
    {
      $period_array[] = intval($row['period']); //can it be float also?
    }
    $total = array_sum($period_array);
    

    UPDATE:

    @uzsolt is right, I almost forgot there’s a dedicated function in the Active Record class, select_sum(); you might want to try that out also, something like

    $this->db->select_sum('period')->get('calcdata');
    

    Quoting from the docs:

    $this->db->select_sum();

    Writes a "SELECT SUM(field)" portion for your query. As with
    select_max(), You can optionally include a second parameter to rename
    the resulting field.

    $this->db->select_sum(‘age’); $query = $this->db->get(‘members’);

    //> Produces: SELECT SUM(age) as age FROM members

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP code doing a very simple select into a table.
The following code is a simple thread game, that switches between threads causing the
I have got the following very simple code: function init() { var articleTabs =
The following code has a simple binding which binds the Text of the TextBlock
The following code executes a simple insert command. If it is called 2,000 times
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
In emacs, I've read the following code snippet in simple.el : (frame-parameter frame 'buried-buffer-list)
I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();
I have the following code (correct for my simple tests) for a linked list
I have used following code to create a simple PDF file. It executes fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.