在這段代碼中,我使用PHP從mysql數據庫中獲取信息。它在一個循環中,以便數據庫中的所有行將顯示在頁面上。每一行都有一個價格,我將這些(循環中)加起來並顯示爲一個總數。我想添加一些功能,這些功能允許爲了將價格添加到總量中而需要檢查的複選框,但我無法弄清楚在什麼地方以及我應該使用什麼技術來執行此操作。將變量通過php傳遞到JavaScript,同時處於從數據庫獲取信息的循環中
這裏是代碼段:
$sql = "SELECT CourseNum, CourseName, CreditNum, InstructorName, BookName, NewPrice, UsedPrice, Section_Name, BookLink FROM BookInfo"; // echo"<a href='$row["BookLink"].'>$row["BookName"].</a>";
$test = "test";
$result = $conn->query($sql);
echo"<h2>CIT Course requirements <hr></h2>";
echo"<h2><small>Math & Science</small></h2>";
if ($result->num_rows > 0) {
echo "$tableHeaderInfo";
// output data of each row
while($row = $result->fetch_assoc()) {
if($row["Section_Name"] == mth_sci){
echo "<tr><td class='col-xs-.3'><input type='checkbox' ID='checbox123' name='checkbox123' value='1' onClick='apply(this);' /> </td><td class='col-xs-1'>" . $row["CourseNum"]. "</td><td class='col-xs-3'>" . $row["CourseName"]. " </td><td class='col-xs-2'>" . $row["InstructorName"]." </td><td><a href='" . $row["BookLink"] . "'>" . $row["BookName"] . "</a></td><td class='col-xs-1'>$" . money_format('%(#10n', $row["NewPrice"]). " </td><td class='col-xs-1'>$" . money_format('%(#10n', $row["UsedPrice"]). " </td></tr>";
$test = "test";
echo "
<script>
apply = function (obj)
{
if(obj.checked){
obj.parentNode.parentNode.style.backgroundColor='lightgreen'
}
else
obj.parentNode.parentNode.style.backgroundColor=''
}
</script>
";
$sum_total1 += $row["NewPrice"]; //This totals the prices right now
$sum_total2 += $row["UsedPrice"];
}
}
echo "</table>";
} else {
echo "0 results";
}
所以不是$ sum_total2 + = $行[ 「UsedPrice」];被用於總計價格,在名爲'apply'的echo'd javascript函數中將會出現類似的行。
這似乎是一個好的方式來總數?我如何從循環中獲得增加,這樣我可以拉動價格變量?
感謝您的幫助:)如果需要,我可以發佈完整的代碼頁。
編輯:
我我的主要目標是使用,而內環路,但只有當複選框被選中,以總價格一路上揚。
例如
if(obj.checked){
obj.parentNode.parentNode.style.backgroundColor='lightgreen'
//does the total here ie total += $NewPrice
}
,但不能工作,可以像這樣,因爲在循環按時間checkbutton事件發生已經完成..
編輯3:
的代碼片斷是基本顯示下面的圖像(減去一些自舉)
http://pastie.org/10790551(鏈接到上面圖片中的代碼)
當按下複選框時,總數將被更新。這似乎是我的技術不會爲此工作,因爲我需要在循環內部捕獲bookprice變量?
[獲得足夠的代表發表評論並不難。](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can -i-do-instead) –
好嗎?我實際上認爲它是。但是我從你的鏈接中學到的唯一東西是,看起來我錯了,我的答案有資格作爲答案。那是你的觀點嗎? –
我只是想幫助。 –