Solved

Arithmetic Operations in PHP

Posted on 2013-02-07
5
202 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
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 400 total points
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
Thank you so much to the both of you for the answers.

@DaveBaldwin, your solution worked perfectly.
0
 
LVL 82

Expert Comment

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

Expert Comment

by:ee_reach
Comment Utility
Glad it all worked out.  Thanks for the assist points.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now