我喜欢用脚本对所有值求和,但得到的结果是: total:NaN (需要对除第一列以外的所有列求和)
代码PHP和脚本位于同一文件中,这是我的代码:
对于php:
<table id="table">
<thead class="thead-dark">
<tr class="titlerow">
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>col5</th>
</tr>
</thead>
<tbody>
<tr>
<?php
include("conn.php");
$result = mysql_query("SELECT id,name,col1,col2 FROM table GROUP BY name");
while($test = mysql_fetch_array($result))
{
$id = $test['id'];
echo"<td class='rowDataSd'>".$test['col1']."</td>";
echo"<td class='rowDataSd'>".$test['col1']."</td>";
echo"<td class='rowDataSd'>".$test['col2']."</td>";
echo"<td class='rowDataSd'>".$test['col2']."</td>";
echo"<td class='rowDataSd'>".$test['col2']."</td>";
echo "</tr>";
}
mysql_close($conn);
echo '<tfoot>
<tr class="totalColumn">
<td>.</td>
<td class="totalCol">Total:</td>
<td class="totalCol">Total:</td>
<td class="totalCol">Total:</td>
<td class="totalCol">Total:</td>
<td class="totalCol">Total:</td>
</tr>
</tfoot>';
?>
</table>
对于脚本:
var totals=[0,0,0];
$(document).ready(function(){
var $dataRows=$("#table tr:not('.totalColumn, .titlerow')");
$dataRows.each(function() {
$(this).find('.rowDataSd').each(function(i){
totals[i]+=parseInt( $(this).html());
});
});
$("#table td.totalCol").each(function(i){
$(this).html("total:"+totals[i]);
});
});
确切的问题在哪里?
答案 0 :(得分:0)
我不确定,但是据我所知,也许我是错的(很乐意就此提供建议)=> mysql_fetch_array和mysql_query有点“死”,而今天,您应该使用:mysqli_fetch_array和mysqli_result(请参阅附加的i),这取决于您正在服务器上运行的版本。该查询对您有用吗?如果没有,我会对此进行调查。
在此处查看示例:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
答案 1 :(得分:0)
我完全不会用jQuery来总结它们。我会用PHP做到这一点:
...
<tr> <!--- this tr is out of place, needs to be in the loop -->
<?php
//include("conn.php"); this should be require as it's necessary
//include will ignore missing files, require with throw an error
//this code will not work without that file, so let PHP tell you
//when it goes missing, instead of wondering why your DB calls don't work
require "conn.php"; // inlude/require are not functions they will work with (...) but it's not best practice.
$result = mysql_query("SELECT id,name,col1,col2 FROM table GROUP BY name");
//define default values for increment without errors
$totals = ['col1'=>0,'col2'=>0,'col3'=>0,'col4'=>0,'col5'=>0];
while($test = mysql_fetch_array($result))
{
$id = $test['id'];
echo "<tr>"; //-- this is the tr from above --
//use a loop if the HTML is all the same
for($i=1;$i<=5;++$i){
$key = 'col'.$i;
echo"<td class='rowDataSd'>".$test[$key]."</td>";
//simply add the values on each iteration (sum)
$totals[$key] += $test[$key];
}
echo "</tr>"; //-- without fixing the open tag as mentioned above, your HTML would be messed up --
}
mysql_close($conn);
echo '<tfoot>';
echo '<tr class="totalColumn">';
echo '<td>.</td>';
for($i=1;$i<=5;++$i){
echo '<td class="totalCol">total:'.$totals['col'.$i].'</td>';
}
echo '</tr>';
echo '</tfoot>';
...
如果这些东西没有动态变化(似乎没有变化),则无需使用Javascript。
您还可以通过使用简单的for循环来减少代码。
干杯!