6 Replies - 526 Views - Last Post: 22 January 2013 - 05:42 AM Rate Topic: -----

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

getting sum of records fetched from backend via PHP

Posted 21 January 2013 - 05:32 AM

I am fetching records from database and want to get the total sum of salary paid to each user via PHP.
I want to get the sum of salary using PHP but not via backend. How can I do it?
<?php
		include('conn.php');
		$result =mysql_query("select * from tbl_test")
			or die(mysql_error());
		
		echo "<table cellpadding = '10'>";
		echo "<tr>
				<th>id:</th>
				<th>name:</th>
				<th>salary:</th>		
			</tr>";
			
		while($row = mysql_fetch_array($result))
		{
			echo "<tr>";
			echo '<td>'.$row['userid'].'</td>';
			echo '<td>'.$row['username'].'</td>';
			echo '<td>'.$row['salary'].'</td>';
		}
	?>


Is This A Good Question/Topic? 0
  • +

Replies To: getting sum of records fetched from backend via PHP

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,174
  • Joined: 08-June 10

Re: getting sum of records fetched from backend via PHP

Posted 21 January 2013 - 05:39 AM

View Postjone kim, on 21 January 2013 - 01:32 PM, said:

I want to get the sum of salary using PHP but not via backend.

er, PHP *is* your backend.

other than that I’d use SQL to calculate that.

edit: besides that I see you DB violates Database Normalisation Rules.

This post has been edited by Dormilich: 21 January 2013 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

Re: getting sum of records fetched from backend via PHP

Posted 21 January 2013 - 09:34 PM

Thank You Dormilich.
I've to calculate the salary via PHP script. How can I do it? Please can you suggest me.
As per the database normalisation, I've only shown the table which I needed.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,174
  • Joined: 08-June 10

Re: getting sum of records fetched from backend via PHP

Posted 22 January 2013 - 12:07 AM

View Postjone kim, on 22 January 2013 - 05:34 AM, said:

I've to calculate the salary via PHP script.

then, what have you tried?


View Postjone kim, on 22 January 2013 - 05:34 AM, said:

As per the database normalisation, I've only shown the table which I needed.

and that part is sufficient to tell that you DB is not normalised.
Was This Post Helpful? 0
  • +
  • -

#5 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

Re: getting sum of records fetched from backend via PHP

Posted 22 January 2013 - 01:51 AM

Quote

then, what have you tried?

I can not figure how to do it?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,174
  • Joined: 08-June 10

Re: getting sum of records fetched from backend via PHP

Posted 22 January 2013 - 02:33 AM

how would you do it, if you had the data incoming from a HTML form?
Was This Post Helpful? 0
  • +
  • -

#7 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: getting sum of records fetched from backend via PHP

Posted 22 January 2013 - 05:42 AM

If this is a larger database which i am guessing it isn't i would avoid using php to do this as the script would take a large amount of time to execute and would more than likely involve an array of some class that defines an object reflecting the structure of your table.

Let SQL do the work, for instance say you have a column within your test_table called salary, then you could quickly get the summation of all the salary values using an SQL statement such as

SELECT SUM(salary) FROM test_table



If you had a column defining some pay bracket group indicated by an integer called pay_group, say for instance entry level payments were denoted by level one '1'. Then you could adjust accordingly and execute the following query to return that specific value.
SELECT sum(salary)
FROM test_table
WHERE pay_group=1;



I hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1