Solved

Arithmetic Operations in PHP

Posted on 2013-02-07
5
213 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 83

Accepted Solution

by:
Dave Baldwin earned 400 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 100 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 83

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

837 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