Sum values from a column in database [closed]
Good morning,
I 've been struggling with the simple task of creating a sum of numerical values from the database. What I want to do is to create a loop within another loop.
The first loop is fetching 'project' from database and report the values in a table on my page and the bracket closes after the last column of the page. Now the part that i want to do is to use the $id
to fetch the values from 'budget' in the database, and the ones that have the project_id='$id'
to sum and echo them in another cell of the same table.
My code so far is the following:
<?php
include 'dbcon.php';
$query1=mysqli_query($con,"select * from project ORDER BY project_id ASC")or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($query1)){
$id=$row['project_id'];
$total=0;
$query = mysqli_query($con,"SELECT * FROM budget WHERE project_id='$id'");
while ($row=mysqli_fetch_array($query)){
$total=$total+$row['amount'];}
?>
<tr>
<td colspan="2"><?php echo $row1['project_name'];?></td>
<td><?php echo $total;?></td>
<td><?php echo $row1['project_status'];?></td>
</tr>
<?php }?>
Now what I am getting is 0 in the table and not the summary. I have also tryid some solution I found in the site like:
$query = mysqli_query($con,"SELECT sum(amount) as total FROM budget WHERE project_id='$id'");
No luck with that one either. Now i have two explanations. Either the loop within a loop is not build correctly by me (i believe that setting $id=$row1['project_id']
will push the project_id to the next loop and will not change until the second loop finishes) or i missing something really silly here...
php loops sum
closed as off-topic by Rob♦ Nov 30 '18 at 3:25
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Rob
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 4 more comments
Good morning,
I 've been struggling with the simple task of creating a sum of numerical values from the database. What I want to do is to create a loop within another loop.
The first loop is fetching 'project' from database and report the values in a table on my page and the bracket closes after the last column of the page. Now the part that i want to do is to use the $id
to fetch the values from 'budget' in the database, and the ones that have the project_id='$id'
to sum and echo them in another cell of the same table.
My code so far is the following:
<?php
include 'dbcon.php';
$query1=mysqli_query($con,"select * from project ORDER BY project_id ASC")or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($query1)){
$id=$row['project_id'];
$total=0;
$query = mysqli_query($con,"SELECT * FROM budget WHERE project_id='$id'");
while ($row=mysqli_fetch_array($query)){
$total=$total+$row['amount'];}
?>
<tr>
<td colspan="2"><?php echo $row1['project_name'];?></td>
<td><?php echo $total;?></td>
<td><?php echo $row1['project_status'];?></td>
</tr>
<?php }?>
Now what I am getting is 0 in the table and not the summary. I have also tryid some solution I found in the site like:
$query = mysqli_query($con,"SELECT sum(amount) as total FROM budget WHERE project_id='$id'");
No luck with that one either. Now i have two explanations. Either the loop within a loop is not build correctly by me (i believe that setting $id=$row1['project_id']
will push the project_id to the next loop and will not change until the second loop finishes) or i missing something really silly here...
php loops sum
closed as off-topic by Rob♦ Nov 30 '18 at 3:25
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Rob
If this question can be reworded to fit the rules in the help center, please edit the question.
1
You know MySQL has a SUM() function, yes?
– CD001
Nov 20 '18 at 9:04
Hello CD001, I have tryed it like: '$total = new PDO; foreach($total->query('SELECT SUM(amount) FROM budget where project_id='$id'') as $row)' without success. I think i am missing something really simple here..
– juamx214
Nov 20 '18 at 9:13
@juamx214 why are you using two queries ? In your current code, you will get only the TOTAL of last project that you get from query one.
– Sajjad Ali
Nov 20 '18 at 9:59
If your aim is to get the total of a specific project then you should have to get SUM of only that particular subject (by getting the id of subject) rather than getting all the projects, which is surely not a good practice.
– Sajjad Ali
Nov 20 '18 at 10:00
I question the edit where they attempted to answer. I also question it being approved. I rolled it back.
– Funk Forty Niner
Nov 20 '18 at 13:58
|
show 4 more comments
Good morning,
I 've been struggling with the simple task of creating a sum of numerical values from the database. What I want to do is to create a loop within another loop.
The first loop is fetching 'project' from database and report the values in a table on my page and the bracket closes after the last column of the page. Now the part that i want to do is to use the $id
to fetch the values from 'budget' in the database, and the ones that have the project_id='$id'
to sum and echo them in another cell of the same table.
My code so far is the following:
<?php
include 'dbcon.php';
$query1=mysqli_query($con,"select * from project ORDER BY project_id ASC")or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($query1)){
$id=$row['project_id'];
$total=0;
$query = mysqli_query($con,"SELECT * FROM budget WHERE project_id='$id'");
while ($row=mysqli_fetch_array($query)){
$total=$total+$row['amount'];}
?>
<tr>
<td colspan="2"><?php echo $row1['project_name'];?></td>
<td><?php echo $total;?></td>
<td><?php echo $row1['project_status'];?></td>
</tr>
<?php }?>
Now what I am getting is 0 in the table and not the summary. I have also tryid some solution I found in the site like:
$query = mysqli_query($con,"SELECT sum(amount) as total FROM budget WHERE project_id='$id'");
No luck with that one either. Now i have two explanations. Either the loop within a loop is not build correctly by me (i believe that setting $id=$row1['project_id']
will push the project_id to the next loop and will not change until the second loop finishes) or i missing something really silly here...
php loops sum
Good morning,
I 've been struggling with the simple task of creating a sum of numerical values from the database. What I want to do is to create a loop within another loop.
The first loop is fetching 'project' from database and report the values in a table on my page and the bracket closes after the last column of the page. Now the part that i want to do is to use the $id
to fetch the values from 'budget' in the database, and the ones that have the project_id='$id'
to sum and echo them in another cell of the same table.
My code so far is the following:
<?php
include 'dbcon.php';
$query1=mysqli_query($con,"select * from project ORDER BY project_id ASC")or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($query1)){
$id=$row['project_id'];
$total=0;
$query = mysqli_query($con,"SELECT * FROM budget WHERE project_id='$id'");
while ($row=mysqli_fetch_array($query)){
$total=$total+$row['amount'];}
?>
<tr>
<td colspan="2"><?php echo $row1['project_name'];?></td>
<td><?php echo $total;?></td>
<td><?php echo $row1['project_status'];?></td>
</tr>
<?php }?>
Now what I am getting is 0 in the table and not the summary. I have also tryid some solution I found in the site like:
$query = mysqli_query($con,"SELECT sum(amount) as total FROM budget WHERE project_id='$id'");
No luck with that one either. Now i have two explanations. Either the loop within a loop is not build correctly by me (i believe that setting $id=$row1['project_id']
will push the project_id to the next loop and will not change until the second loop finishes) or i missing something really silly here...
php loops sum
php loops sum
edited Nov 30 '18 at 3:25
Rob♦
23.6k115274
23.6k115274
asked Nov 20 '18 at 9:02
juamx214
24
24
closed as off-topic by Rob♦ Nov 30 '18 at 3:25
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Rob
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Rob♦ Nov 30 '18 at 3:25
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Rob
If this question can be reworded to fit the rules in the help center, please edit the question.
1
You know MySQL has a SUM() function, yes?
– CD001
Nov 20 '18 at 9:04
Hello CD001, I have tryed it like: '$total = new PDO; foreach($total->query('SELECT SUM(amount) FROM budget where project_id='$id'') as $row)' without success. I think i am missing something really simple here..
– juamx214
Nov 20 '18 at 9:13
@juamx214 why are you using two queries ? In your current code, you will get only the TOTAL of last project that you get from query one.
– Sajjad Ali
Nov 20 '18 at 9:59
If your aim is to get the total of a specific project then you should have to get SUM of only that particular subject (by getting the id of subject) rather than getting all the projects, which is surely not a good practice.
– Sajjad Ali
Nov 20 '18 at 10:00
I question the edit where they attempted to answer. I also question it being approved. I rolled it back.
– Funk Forty Niner
Nov 20 '18 at 13:58
|
show 4 more comments
1
You know MySQL has a SUM() function, yes?
– CD001
Nov 20 '18 at 9:04
Hello CD001, I have tryed it like: '$total = new PDO; foreach($total->query('SELECT SUM(amount) FROM budget where project_id='$id'') as $row)' without success. I think i am missing something really simple here..
– juamx214
Nov 20 '18 at 9:13
@juamx214 why are you using two queries ? In your current code, you will get only the TOTAL of last project that you get from query one.
– Sajjad Ali
Nov 20 '18 at 9:59
If your aim is to get the total of a specific project then you should have to get SUM of only that particular subject (by getting the id of subject) rather than getting all the projects, which is surely not a good practice.
– Sajjad Ali
Nov 20 '18 at 10:00
I question the edit where they attempted to answer. I also question it being approved. I rolled it back.
– Funk Forty Niner
Nov 20 '18 at 13:58
1
1
You know MySQL has a SUM() function, yes?
– CD001
Nov 20 '18 at 9:04
You know MySQL has a SUM() function, yes?
– CD001
Nov 20 '18 at 9:04
Hello CD001, I have tryed it like: '$total = new PDO; foreach($total->query('SELECT SUM(amount) FROM budget where project_id='$id'') as $row)' without success. I think i am missing something really simple here..
– juamx214
Nov 20 '18 at 9:13
Hello CD001, I have tryed it like: '$total = new PDO; foreach($total->query('SELECT SUM(amount) FROM budget where project_id='$id'') as $row)' without success. I think i am missing something really simple here..
– juamx214
Nov 20 '18 at 9:13
@juamx214 why are you using two queries ? In your current code, you will get only the TOTAL of last project that you get from query one.
– Sajjad Ali
Nov 20 '18 at 9:59
@juamx214 why are you using two queries ? In your current code, you will get only the TOTAL of last project that you get from query one.
– Sajjad Ali
Nov 20 '18 at 9:59
If your aim is to get the total of a specific project then you should have to get SUM of only that particular subject (by getting the id of subject) rather than getting all the projects, which is surely not a good practice.
– Sajjad Ali
Nov 20 '18 at 10:00
If your aim is to get the total of a specific project then you should have to get SUM of only that particular subject (by getting the id of subject) rather than getting all the projects, which is surely not a good practice.
– Sajjad Ali
Nov 20 '18 at 10:00
I question the edit where they attempted to answer. I also question it being approved. I rolled it back.
– Funk Forty Niner
Nov 20 '18 at 13:58
I question the edit where they attempted to answer. I also question it being approved. I rolled it back.
– Funk Forty Niner
Nov 20 '18 at 13:58
|
show 4 more comments
1 Answer
1
active
oldest
votes
You might need to change this. I think you are doing something wrong here:
$id=$row['project_id'];
To this:
$id=$row1['project_id'];
See edit code
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might need to change this. I think you are doing something wrong here:
$id=$row['project_id'];
To this:
$id=$row1['project_id'];
See edit code
add a comment |
You might need to change this. I think you are doing something wrong here:
$id=$row['project_id'];
To this:
$id=$row1['project_id'];
See edit code
add a comment |
You might need to change this. I think you are doing something wrong here:
$id=$row['project_id'];
To this:
$id=$row1['project_id'];
See edit code
You might need to change this. I think you are doing something wrong here:
$id=$row['project_id'];
To this:
$id=$row1['project_id'];
See edit code
edited Nov 21 '18 at 13:52
Funk Forty Niner
80.5k1247101
80.5k1247101
answered Nov 20 '18 at 9:18
Akhtar Munir
6811
6811
add a comment |
add a comment |
1
You know MySQL has a SUM() function, yes?
– CD001
Nov 20 '18 at 9:04
Hello CD001, I have tryed it like: '$total = new PDO; foreach($total->query('SELECT SUM(amount) FROM budget where project_id='$id'') as $row)' without success. I think i am missing something really simple here..
– juamx214
Nov 20 '18 at 9:13
@juamx214 why are you using two queries ? In your current code, you will get only the TOTAL of last project that you get from query one.
– Sajjad Ali
Nov 20 '18 at 9:59
If your aim is to get the total of a specific project then you should have to get SUM of only that particular subject (by getting the id of subject) rather than getting all the projects, which is surely not a good practice.
– Sajjad Ali
Nov 20 '18 at 10:00
I question the edit where they attempted to answer. I also question it being approved. I rolled it back.
– Funk Forty Niner
Nov 20 '18 at 13:58