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>

2 つのデータベース テーブルがあり、列データをテーブルに正常に取り込んでいます。テーブルの 3 番目のセルでは、データベースのいくつかの列からパーセンテージを計算したいと考えています。これをコーディングする方法がわかりません...別のスレッドから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を使用してレコードの合計を取得し、その値に何回遭遇したかを示すカウンターを取得します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 に答える