简体   繁体   中英

Find the difference between two count columns from two different database queries and display result in PHP

I asked a question last week and had assumed that the final part of it would be the straight forward part. However that is not proving to be the case. Here is a link to the original question and background.

I have two databases so have done 2 SQL queries and have merged the results into a table with four columns that displays

Name | Number| db1 Count | db2 Count |

That displays all the information perfectly. However, I need to get it to display, in a 5th column, the difference between db1 Count and db2 Count. This is the code I have:

$results = array();
while($row = $result1->fetch_assoc()) {
 //Adding all the 1st query results 
    $results[$row['number']] = $row;
}

while($row = $result2->fetch_assoc()) {
    if(! isset($results[$row['number']]) {
          $results[$row['number']] = $row;
    }else {
      $results[$row['number']]['db2 Count'] = $row['db2 Count'];
    }         
}
$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);


if ($results) {
    echo"<TABLE><caption>Total Call Count Overview</caption><TR>
      <TH>Number</TH>
      <TH>Company</TH>
      <TH>db1 Count</TH>
      <TH>db2 Count</TH>
      <TH>Difference</TH></TR>";

    foreach($results as $row) {
         echo"<TR><TD>". $row["number"]. "</TD>";
         echo"<TD>". $row["company"]. "</TD>";
         echo"<TD>". $row["db1 Count"]. "</TD>";
         echo"<TD>". $row["db2 Count"]. "</TD>";
         echo"<TD>". $difference . "</TD></TR>";
    }
    echo"</TABLE>";
 } else {
 echo"0 Results";
}

I had thought that putting the counts as variables

$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);

Would allow me to subtract them and echo the $difference variable, but I am just getting a column filled with zeros. I have also tried

echo"<TD>". $row['db1 Count'] - $row['db2 Count']. "</TD>";

But only to get the all the db2 Count figures across the top of the page.

Is there something I am missing, because I have tried answers to similar questions online but to no avail. I'm really quite new to this so any tips in the right direction would be much appreciated.

Move these lines:

$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);

inside the : foreach($results as $row)

so

foreach($results as $row) {
    $db1_count = $row['db1 Count'];
    $db2_count = $row['db2 Count'];
    $difference = ($db1_count - $db2_count);
    echo"<TR><TD>". $row["number"]. "</TD>";
    echo"<TD>". $row["company"]. "</TD>";
    echo"<TD>". $row["db1 Count"]. "</TD>";
    echo"<TD>". $row["db2 Count"]. "</TD>";
    echo"<TD>". $difference . "</TD></TR>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM