dsb238100 2012-11-20 02:57
浏览 22
已采纳

从数据库计算列然后拉入表格单元格

EDIT: I have successfully been able to calculate the value I was trying to get, but instead of calculating that value for each row, it is just calculating it once and posting that value everywhere. How do I make it recalculate for each row using the code I have?

Picture: http://img515.imageshack.us/img515/9064/example2w.png

New Code:

<html>
<head>
<title>PHP-MySQL Project 4</title>

    <div align="center">
        <p>
            PHP-MySQL Project 4
        <br/>
            By: Ryan Strouse
        </p>
    </div>
</head>
<body bgcolor="#99FFFF">

<?php

$DBName = "surveys";
$DBConnect = @mysqli_connect("localhost", "students", "password")
    Or die("<p>Unable to connect to the database server.</p>"
    . "<p>Error code " . mysqli_connect_errno()
    . ": " . mysqli_connect_error()) . "</p>";

if (!$DBConnect)
    {
    echo "<p> The database server is not available.</p>";
    }
else
    {
    echo "<p> Successfully connected to the database $DBName</p>";
    }

mysqli_select_db($DBConnect, $DBName); 
echo "<p>Database -'$DBName'- found</p>";

$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);

$count_surveys = $row['surveyResponses'];

echo "<p>Total Responses: $count_surveys</p>";


$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = @mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);


$SQLstring3 = "SELECT * FROM surveys, results";
$QueryResult3 = @mysqli_query($DBConnect, $SQLstring3);
$fetchrow = mysqli_fetch_assoc($QueryResult3);
$result_amount = (($fetchrow['resultResponses'] / $fetchrow['surveyResponses']) * 100);

echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
do {
    echo "<tr><td>{$Row['resultDescription']}</td>";
    echo "<td>{$Row['resultResponses']}</td>";
    echo "<td>$result_amount</td></tr>";
    $Row = mysqli_fetch_assoc($QueryResult3);
} while ($Row);
echo "</table>";


?>



<center>
<h3><a href="Survey1.html">Return To Main Page</a></h3>
<h3><a href="../Menu.html">Return to Menu</a></h3>
</center>
</body>

<footer>
<div align="center">
&copy; Copyright Ryan Strouse &copy;

</div>
</footer>
</html>

I have two database tables and I am successfully pulling in column data into a table. The third cell of the table I would like to calculate a percentage out of some of the columns from the database. I'm not sure how to code this... I've tried to come up with something in the SELECT statement from another thread I found with no luck.

Here is a picture of the query I'm trying to get to work: http://img696.imageshack.us/img696/3862/examplegw.png

<html>
<head>
<title>PHP-MySQL Project 4</title>


</head>
<body bgcolor="#99FFFF">

<?php

$DBName = "surveys";
$DBConnect = @mysqli_connect("localhost", "students", "password")
    Or die("<p>Unable to connect to the database server.</p>"
    . "<p>Error code " . mysqli_connect_errno()
    . ": " . mysqli_connect_error()) . "</p>";

if (!$DBConnect)
    {
    echo "<p> The database server is not available.</p>";
    }
else
    {
    echo "<p> Successfully connected to the database $DBName</p>";
    }

mysqli_select_db($DBConnect, $DBName); 
echo "<p>Database -'$DBName'- found</p>";

$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);

$count_surveys = $row['surveyResponses'];

echo "<p>Total Responses: $count_surveys</p>";


$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = @mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);


//this is where I am trying to calculate the value and then below it display in table 
//cell # 3
$SQLstring3 = "SELECT *,((resultResponses/surveyResponses)*100) AS AMOUNT FROM surveys, results";
$QueryResult3 = @mysqli_query($DBConnect, $SQLstring3);

do {
    echo "<table>";
    echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
    echo "<tr><td>{$Row['resultDescription']}</td>";
    echo "<td>{$Row['resultResponses']}</td>";
    echo "<td>$QueryResult3</td></tr>";
    $Row = mysqli_fetch_assoc($QueryResult);
} while ($Row);
echo "</table>";


?>



<center>
<h3><a href="Survey1.html">Return To Main Page</a></h3>
<h3><a href="../Menu.html">Return to Menu</a></h3>
</center>
</body>

<footer>

</footer>
</html>
  • 写回答

2条回答 默认 最新

  • dongpao1905 2012-11-20 03:11
    关注

    IF I understand what you are asking, you're trying probably to calculate the percentage of a value that you can find in a MySQL query's results. If that is so, then I'd use the function mysql_num_rows to get the total of the records and then in a while and if I'd have a counter of how many times I meet that value.

    Then you just do simple math, for example:

    result = (100 * counter) / mysql_num_rows
    

    and have a percentage. Then you just echo the result wherever you want! :)

    I hope I have understood your question correctly!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)