Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Arithmetic Operations in PHP

Posted on 2013-02-07
5
Medium Priority
?
234 Views
Last Modified: 2013-02-07
Hi,

I'm trying to do some simple adding in php, but having a few issues.

Here is the function code:
    function getUsedCredits() {
        
        $f_response = 0;
        $total = 0;
        $uid = 21;
        $sql = "SELECT cost FROM tbl_clientdomains WHERE uid = ?";
        $response = $this->db->query($sql, array($uid));
        
        if ($response->num_rows() > 0) {
            foreach ($response->result() as $row) {
                
                $f_response = $total + (int)$row->cost;
                
            }
        }
        var_dump($response);
        return $f_response;
    }

Open in new window


When I echo $f_response, the result is: 1
However, the result should be 9 but I'm not sure why it's showing 1.

the var_dump of $response is below:
object(CI_DB_mysql_result)[22]
  public 'conn_id' => resource(49, mysql link persistent)
  public 'result_id' => resource(69, mysql result)
  public 'result_array' => 
    array (size=0)
      empty
  public 'result_object' => 
    array (size=9)
      0 => 
        object(stdClass)[21]
          public 'cost' => string '1' (length=1)
      1 => 
        object(stdClass)[23]
          public 'cost' => string '1' (length=1)
      2 => 
        object(stdClass)[24]
          public 'cost' => string '1' (length=1)
      3 => 
        object(stdClass)[25]
          public 'cost' => string '1' (length=1)
      4 => 
        object(stdClass)[26]
          public 'cost' => string '1' (length=1)
      5 => 
        object(stdClass)[27]
          public 'cost' => string '1' (length=1)
      6 => 
        object(stdClass)[28]
          public 'cost' => string '1' (length=1)
      7 => 
        object(stdClass)[29]
          public 'cost' => string '1' (length=1)
      8 => 
        object(stdClass)[30]
          public 'cost' => string '1' (length=1)
  public 'custom_result_object' => 
    array (size=0)
      empty
  public 'current_row' => int 0
  public 'num_rows' => int 9
  public 'row_data' => null

Open in new window


I'm using codeigniter as the framework.

Even though from the above we can see there are 9 items in the array that should be returning 1, the total value should therefore be 9 but the output is always 1 for some reason..

Any help is appreciated.
0
Comment
Question by:rvcw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 1600 total points
ID: 38865359
You are continuously resetting the value.  Try this.

    function getUsedCredits() {
        
        $f_response = 0;
        $total = 0;
        $uid = 21;
        $sql = "SELECT cost FROM tbl_clientdomains WHERE uid = ?";
        $response = $this->db->query($sql, array($uid));
        
        if ($response->num_rows() > 0) {
            $f_response = $total;
            foreach ($response->result() as $row) {
                
                $f_response += (int)$row->cost;
                
            }
        }
        var_dump($response);
        return $f_response;
    }

Open in new window

0
 
LVL 8

Assisted Solution

by:ee_reach
ee_reach earned 400 total points
ID: 38865722
In order to accumulate a value and increment it each time, change this line:
$f_response = $total + (int)$row->cost;

to this:

$f_response = $f_response + (int)$row->cost;

This allows $f_response to add a new value to the existing value, such that it takes the current value and adds a new value to it.


This is the same calculation as what the previous poster wrote, so you could also say the following.  

$f_response +=  (int)$row->cost;

The first is easier to remember and see what is happening.  The second is more concise.'
0
 

Author Closing Comment

by:rvcw
ID: 38866302
Thank you so much to the both of you for the answers.

@DaveBaldwin, your solution worked perfectly.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38866373
You're welcome, glad to help.
0
 
LVL 8

Expert Comment

by:ee_reach
ID: 38866803
Glad it all worked out.  Thanks for the assist points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question