Solved

Arithmetic Operations in PHP

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

623 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