Within this section of code I am using PHP to pull information from a mysql database. Its in a loop so that all of the rows in the database will be presented on the page. There is a price in each row, and I total those (in the loop) up and display them as one total. I want to add functionality that allows for a checkbox that needs to be checked in order for the price to be added to the total, but I cant figure out where and with what technology I should be using to do this.
Here is the snippet of code:
$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";
}
So instead of $sum_total2 += $row["UsedPrice"]; being used to total the price, there will be a similar line within the echo'd javascript function named 'apply'.
Does this seem like an okay way to total the numbers? How do I get that addition to happen from within the loop so I can pull the price variables?
Thanks for the help :) I can post the full page of code if needed.
Edit:
I my main goal is to use to total up the prices while within the loop, but only if the checkbox is checked.
for example
if(obj.checked){
obj.parentNode.parentNode.style.backgroundColor='lightgreen'
//does the total here ie total += $NewPrice
}
But can cant work it like this since the loop has already finished by time the checkbutton event happens..
edit 3:
The snippet of code is basically displaying the image below (minus some bootstrap)
http://pastie.org/10790551 (link to the code from image above)
When the checkbox is pressed, the total will be updated. It seems like my technique wont work for this since I need to catch the bookprice variables while inside of the loop?