Jump to content

Adding up records from a SQL Database


nate2687

Recommended Posts

I have 5 check boxes with an item and a price being pulled out of a database, when a user clicks on one or more of the check boxes, it tells them what item(s) they clicked and what the price of the item(s) are.  All I want to do is add up the price of the items they check, how do I got about doing this?  Here is what I got so far:

 

<html>
<body>

<?php

echo "<font size=6><b>Here is My Form</b><br><br></font>";


//open connection
include('C:\wamp\www\class\opencon.php');

//Query
$query="SELECT * FROM pageinfo";
$data = mysql_query($query) or die("Error, could not get info");

echo '<form method=post>';
while($row = mysql_fetch_assoc($data))
{
//$Item = $row['Item'];
//echo "<input type=\"checkbox\" />" . $Item . "<br/>";


echo "<input type='checkbox' value='You ordered a $row[item] for $$row[Price]' name='item[]'>". $row['Item']. "<br/>";
$row=mysql_fetch_assoc($data);

echo "<input type='checkbox' value='You ordered $row[item] for $$row[Price]' name='item[]'>". $row['Item']. "<br/>";
$row=mysql_fetch_assoc($data);

echo "<input type='checkbox' value='You ordered a $row[item] for $$row[Price]' name='item[]'>". $row['Item']. "<br/>";
$row=mysql_fetch_assoc($data);

echo "<input type='checkbox' value='You ordered $row[item] for $$row[Price]' name='item[]'>". $row['Item']. "<br/>";
$row=mysql_fetch_assoc($data);

echo "<input type='checkbox' value='You ordered a $row[item] for $$row[Price]' name='item[]'>". $row['Item']. "<br/>";
$row=mysql_fetch_assoc($data);


}


echo "<br><br>";
echo '<input type=submit value=Submit "change">';
echo "<br><br>";
echo "<font size=6><b>You Ordered the Following:</b><br><br></font>";


$item=$_POST['item'];

if($item == True)
{

foreach ($item as $fooditem)
{
echo "</br>";
echo "$fooditem";
echo "Your total is: (I need to do something here)"



}

}

else
{
echo "You didn't check any items";
}


echo '</form>';


include('C:\wamp\www\class\closeconn.php');


?>
</html>
</body>


 

My table inside my database looks like this: phpmyadmin.png

Link to comment
https://forums.phpfreaks.com/topic/101644-adding-up-records-from-a-sql-database/
Share on other sites


it seems  a little bit hard....can u write seperate query to gfet names....
instead of passing two values in one check ...like this......

echo "<input type='checkbox' value=$row[Price]' name='item[]'>". $row['Item']. "<br/>";
$row=mysql_fetch_assoc($data);


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.