Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:04:38+00:00 2026-06-10T23:04:38+00:00

I got this PHP code here… $totalBalance = 0; foreach($resultArray as $row) { foreach($row

  • 0

I got this PHP code here…

$totalBalance = 0;
    foreach($resultArray as $row) {
        foreach($row as $key =>$field) {
            echo $field['remaining'] . "<br>";
        }
    }

$resultArray is what is returned from my SQL query into an array.

The first few records of the balance colunm are..

389.96
6433.66
52.52
107.25

however when I run this php code I get

1
5
1
3

why is it doing this and how can I fix it?

Here is the SQL query if it helps

SELECT c.clientid, c.clientname, c.billingdate, 
case when (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) < 0 then (select ii.total from invoice ii where ii.client = c.clientid and ii.isdeleted=0 order by ii.invoiceid desc limit 1)  else (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) end as remaining,
case c.isactive+0
        when '1' then 'Stop'
        else 'Start' 
        end as Active
FROM client c 
ORDER BY clientname

I did a print_r of $resultArray and got this…

Array ( [0] => Array ( [clientid] => 1 [clientname] => client A [billingdate] => 14 [remaining] => 389.96 [active] => Stop ) [1] => Array ( [clientid] => 178 [clientname] => client B [billingdate] => 23 [remaining] => 6433.66 [active] => Stop ) [2] => Array ( [clientid] => 3 [clientname] => client C [billingdate] => 19 [remaining] => 52.52 [active] => Stop ) [3] => Array ( [clientid] => 105 [clientname] => client D [billingdate] => 23 [remaining] => 107.25 [active] => Start )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:04:39+00:00Added an answer on June 10, 2026 at 11:04 pm

    You are doing 1 foreach to many:

    $totalBalance = 0;
    
    foreach($resultArray as $row) {
      echo $row['remaining'] . "<br>";        
    }
    

    And if you want to calculate the totalbalance:

    $totalBalance = 0;
    
    foreach($resultArray as $row) {
      echo $row['remaining'] . "<br>";        
      $totalBalance += $row['remaining'] ; 
    }
    
    echo $totalBalance . "<br>";        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this code from here http://www.irunmywebsite.com/raphael/drawtool2.php . Runs great in IE when I tested
I've got this code here <?php $COUNT = 0; if ($handle = opendir('.')) {
I got this piece of code here... <form action=InvoiceNotice.php?action=invoicenotice method=post> <label for=fordays>Select Day</label> <select
I got this PHP code: // connect to mysql require_once('includes/connect.php'); // include config include('includes/config.php');
I've got this php code to send the users information from the contact form
I got this code in my submit form <form id=myform action='hello.php' method='GET'> <input type=button
i've got this snazzy python code: import subprocess value = subprocess.Popen([php,./php/php_runner.php],stdout=subprocess.PIPE); the problem is,
I'm a fairly newbie php coder, got this php code in a tutorial to
I´ve got this code inside a php file. It should show and hide a
I'm very new to PHP and I got this error (I use Code Igniter).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.