Solved

SUM values from two tables and echo in PHP

Posted on 2013-01-18
4
669 Views
Last Modified: 2013-01-18
Hello Experts.

I have one database with two tables. Need to SUM the values from those two tables and echo it in an <td>.

My current code:

//QUERY SUM RESULT FROM FIRST TABLE (empresas);

$query_emp = "SELECT Employees_n, SUM(Employees_n) FROM empresas";
$result_emp = mysql_query($query_emp);

//ECHO SUM IN A <TD>

while($row = mysql_fetch_array($result_emp)){
	echo "<td>" . $row['SUM(Employees_n)'] . "</td>";
}

////////////////////////////////////////////////////////////////////////////////////////////

//QUERY SUM RESULT FROM SECOND TABLE (anon_empresas);

$query_emp_an = "SELECT Employees_n, SUM(Employees_n) FROM anon_empresas";
$result_emp_an = mysql_query($query_emp_an);

//ECHO SUM IN A <TD>

while($row = mysql_fetch_array($result_emp_an)){
	echo "<td>" . $row['SUM(Employees_n)'] . "</td>";
}



//HOW TO ECHO IN A <TD> THE SUM OF BOTH $row['SUM(Employees_n)']??

Open in new window


What I need is to echo a <td> with the SUM of "Employees_n" from empresas and "Employees_n" from anon_empresas;

Just add the two SUMS and echo in a td;


Thanks a lot
0
Comment
Question by:joao_c
[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
4 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38793798
Does it matter if you do one query or two?  Untested code, but probably close.

//QUERY SUM RESULT FROM FIRST TABLE (empresas);
$query_emp = "SELECT SUM(Employees_n) AS sum_e FROM empresas";
$result_emp = mysql_query($query_emp);
$row = mysql_fetch_assoc($result_emp);
$sum_e = $row['sum_e'];

//QUERY SUM RESULT FROM SECOND TABLE (anon_empresas);
$query_emp_an = "SELECT SUM(Employees_n) AS sum_a FROM anon_empresas";
$result_emp_an = mysql_query($query_emp_an);
$row = mysql_fetch_assoc($result_emp_an);
$sum_a = $row['sum_a'];

// ADD THE NUMBERS
$total = $sum_e + $sum_a;

// PUT THE VARIABLE INTO A TD-TAG
echo '<td>' . $total . '</td>';

Open in new window

0
 

Author Closing Comment

by:joao_c
ID: 38793887
Can't thank you enough. :)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38793958
Thanks for the points and thanks for using EE.  You might want to grab a copy of this book.  It will help you get some structured learning in the art.
http://www.sitepoint.com/books/phpmysql5/

Yank's writing is very readable with great examples, and a code library you can download and modify for your own use.  Now in its fifth printing, it's been a permanent part of my professional library since Edition One.

Best regards, ~Ray
0
 

Author Comment

by:joao_c
ID: 38794897
I will buy that book, looks great. EE was really helpfull, and I already earn some points too. :D
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

687 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