Column wise sum in PHP from HTML table [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to sum of a column
<table width="240" border="1">
<tr>
<td width="94">Name</td>
<td width="130">Current Amount</td>
</tr>
<?php
do {
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php
}
while ($row_Record = mysql_fetch_assoc($Record));
?>
<tr>
<td colspan="2">Total = </td>
</tr>
</table>
I don't know how to do this kind of sum.
Please any one help me.
Thank you.

By using Jquery, you can do something like that
$(document).ready(function(){
var sum = 0
$(".sum").each(function(){
sum += parseFloat($(this).text());
});
alert(sum);
});
Jsfiddle
Alternatively, you can sum all the values by using PHP when fetching the values.
Something like that:
$sum = 0;
while($read = mysqli_fetch_array()){
$sum += $read['row'];
}
echo $sum; //total

<table width="240" border="1">
<tr>
<td width="94">Nmae</td>
<td width="130">Current Amount</td>
</tr>
<?php
$tmp=0;
do {
$tmp+=$row_Record['current_amount'];
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php } while ($row_Record = mysql_fetch_assoc($Record)); ?>
<tr>
<td colspan="2">Total = <?php echo $tmp; ?></td>
</tr>
</table>

Related

how to get the total of the earnings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
the code is expected to calculate the total amount of the "Net Earning" row. How do i write a code to do that. The $stmt variable is associated to a query from the database
<div class="block-card-body">
<div class="my-table table-responsive">
<table class="table align-items-center table-flush mb-0">
<thead class="thead-light">
<tr>
<th>Order ID</th>
<th>Amount</th>
<th>Fee</th>
<th>Net Earning</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach ($stmt as $val) {
?>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<td class="text-success">$<?php echo $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1) ; ?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div><!-- end block-card-body -->
Create a variable to use as an accumulator, initialise it to zero.
Then in the loop, calulate the net and add it to your accumulator. How you display it is up to you later.
Also note, I moved the <tr> inside the loop so you get a well formed table row
<tbody>
<?php
$netTotal = 0;
foreach ($stmt as $val) {
?>
<tr>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<?php
$t = $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1);
$netTotal += $t;
?>
<td class="text-success">$<?php echo $t;?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>

Ordered list for table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My database data is echoed in a table list but I will like for the list to be ordered for reference purposes. I have tried several approaches but it doesn't seem to work. Any way possible? here is my code
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
while($row = dbFetchAssoc($result)) {
extract($row);
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td></td>
<td><?php echo $Firstame; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>
I can see some mistakes in your code like disorder of some statements.I don't know what is your actual code.How ever here iI am share with my knowledge through the small example.I think it may be helps to solve your issue.You just look the following code and change it with your tables and database connection credential.If any problem in this,you can tell me.I will give the further solution as I can.
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td></td>
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
$conn= mysqli_connect("localhost", "root", "", "test");
if(!$conn)
{
echo 'not';die;
}
$result=mysqli_query($conn,"select * from animals");
$i=0;
$j=1;
while($row = mysqli_fetch_array($result)) {
$Firstname=$row['name'];
$LastName=$row['animal'];
$id=$row['animal_ID'];
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td><?php echo $j;?></td>
<td><?php echo $Firstname; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
$j++;
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>

Add Numbers to MySQL, HTML table in order [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I will try my best to explain what I am trying to do. Hopefully you can help me out. I have a database that holds links, these links are displayed in a table. I have the entries output properly in order from Points, but I am trying to add a rank number to the side so it says 1, 2, 3, 4, etc going down the page per entry.
Here is my attempt.
<table width = "1000" style='table-layout:fixed;'>
<tr>
// These are the numbers I need to add
<th>Rank</th>
// All these work fine.
<th>Host</th>
<th>Location</th>
<th>Points</th>
</tr>
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<tr>
<td><?php echo $row1[0]; ?></td>
<td><?php echo $row1[1]; ?></td>
<td><?php echo $row1[2]; ?></td>
</tr>
<?php endwhile; ?>
</table>
Hopefully you understand what I am trying to do, if you need any more information let me know.
Thanks for your time.
you mean? :
<?php $rankId=0; while($row1 = mysqli_fetch_array($result1)){ ?>
<tr>
<td><?=$rankId++ ?></td>
<td><?=$row1[0] ?></td>
<td><?= $row1[1] ?></td>
<td><?=$row1[2] ?></td>
</tr>
<?php } ?>

html php not properly aligning for each column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I m trying to run two loops the first query in retrieves the column .The second query takes the primary id of the first table and executes the query.
Final result should be like given in the image
Im not able to give the proper alignment
Below is the code
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas= mysql_fetch_assoc($mast))
{
?>
<tr>
<td><?php echo $res_mas['col1'];?></td>
<?php
$room= mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
while($res_room= mysql_fetch_assoc($room))
{
?>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</td>
</tr><tr>
<?php }?>
</tr>
<?php } ?>
</table>
This should do it:
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas = mysql_fetch_assoc($mast)) {
$room = mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
$count = 0;
while($res_room= mysql_fetch_assoc($room)) {
$count += 1;
?>
<tr>
<td><?php echo $count === 1 ? $res_mas['col1'] : "";?></td>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</tr>
<?php } } ?>
</table>
(untested)

Unexpected T_WHILE error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can anybody point out what I am doing wrong in my code below? I'm getting an "UNEXPECTED T_WHILE" error.
echo '
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<tr>
<td>', while ($row = mysql_fetch_assoc($queryresult)) { $title = $row['eventTitle']; echo $title; }, '</td>
<td>', while ($row = mysql_fetch_assoc($queryresult)) { $category = $row['eventTitle']; echo $category; }, '</td>
</tr>
</table> ';
Try this
First off, close your PHP tags.
?>
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<tr>
<td>
<?php
while ($row = mysql_fetch_assoc($queryresult)) {
$title = $row['eventTitle'];
echo $title;
}
?>
</td>
<td>
<?php
while ($row = mysql_fetch_assoc($queryresult)) {
$category = $row['eventTitle'];
echo $category;
}
?>
</td>
</tr>
</table>
PHP -
$array = array();
$i = 0;
while ($row = mysql_fetch_assoc($queryresult))
{
$array[$i]['title'] = $row['eventTitle'];
$array[$i]['category'] = $row['eventCategory'];
$i++;
}
HTML -
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<?php
foreach($array as $arr)
{
?>
<tr>
<td><?php echo $arr['event']; ?></td>
<td><?php echo $arr['category']; ?></td>
</tr>
<?php
}
?>

Categories