0

编辑:我已经成功地能够计算出我试图获得的值,但不是为每一行计算该值,它只是计算一次并将该值发布到任何地方。如何使用我拥有的代码重新计算每一行?

图片:http: //img515.imageshack.us/img515/9064/example2w.png

新代码:

<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>

我有两个数据库表,并且我成功地将列数据拉入表中。表格的第三个单元格我想计算数据库中某些列的百分比。我不知道如何编写代码...我试图从另一个我发现没有运气的线程中的 SELECT 语句中想出一些东西。

这是我要开始工作的查询的图片: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>
4

2 回答 2

1

如果我理解您的要求,您可能正在尝试计算可以在 MySQL 查询结果中找到的值的百分比。如果是这样,那么我将使用该函数mysql_num_rows来获取记录的总数,然后在 a 中whileif我将有一个计数器来记录我达到该值的次数。

然后你只需做简单的数学运算,例如:

result = (100 * counter) / mysql_num_rows

并有一个百分比。然后,您就echo可以随心所欲地获得结果!:)

我希望我正确理解了你的问题!

于 2012-11-20T03:11:39.197 回答
1
$result = mysql_query("SELECT yourRow FROM yourTable");
$aArray = array();
$cont=0;
while($col = mysql_fetch_assoc($result){
   $aArray[$cont] = $col['yourRow'];
   cont++;
}

这应该给你一个可行的数组,让你用它来做你的数学,然后回显它。干杯

于 2012-11-20T03:13:38.993 回答