Solved

Arithmetic Operations in PHP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

20 Experts available now in Live!

Get 1:1 Help Now