CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Add Columns SQL AND PHP (http://www.codingforums.com/showthread.php?t=284158)

logepoge1 12-14-2012 09:40 PM

Add Columns SQL AND PHP
 
Code:

<?php
        require('includes/header.php');
       
?>

<h4> <ul id="menu">
<li><p><a href="customer_details.php">Customer Details</a></p></li>
<li><p><a href="order_customer_details.php">Details by order</a></p></li>
<li><p><a href="country_list.php">Customers by Country</a></p></li>
<li><p><a href="categories.php">Products by category</a></p></li>
<li><p><a href="low_stock.php">Low Stock Items</a></p></li>
<li><p><a href="index.php">Home</a></p></li>
</ul></h4>


<?php
        $sql = "SELECT customers.CompanyName, orders.CustomerID, orders.OrderID, order_details.OrderID, order_details.ProductID, products.ProductID, products.ProductName, order_details.UnitPrice, order_details.Quantity, orders.shipVia, shippers.shipperId, shippers.CompanyName, orders.Freight
        FROM customers, orders, order_details, products, shippers
        WHERE customers.CustomerID = orders.CustomerID
        AND orders.OrderID = '" . $_GET['order'] . "%'
        AND orders.OrderID = order_details.OrderID
        AND order_details.ProductID = products.productID
        AND orders.ShipVia = shippers.ShipperID
        ORDER BY products.productName, products.UnitsInStock, products.UnitPrice, shippers.CompanyName, orders.Freight";
       
        $result = mysql_query($sql) or die(mysql_error());

       
echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Shipper</th>
<th>Shipping Cost</th>
<th>Total Cost</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ProductName'] . "</td>";
  echo "<td>" . $row['Quantity'] . "</td>";
  echo "<td>" . $row['UnitPrice'] . "</td>";
  echo "<td>" . $row['CompanyName'] . "</td>";
  echo "<td>" . $row['Freight'] . "</td>";
  //echo "<td>""</td>";
  }
echo "</table>";
               
?>



<?php
require('includes/footer.php');

?>

I want to add the number from the Total Cost column into something below that says: The total cost for this order is: and echo that amount.

Redcoder 12-14-2012 11:06 PM

The SUM of the column should do it. Notice it is the first thing in the query -not because it has to be done like that but because I didn't want it to get lost in the sea of columns and tables in the query.

PHP Code:

$sql "SELECT SUM(order_details.UnitPrice) as TotalPrice, customers.CompanyName, orders.CustomerID, orders.OrderID, order_details.OrderID, order_details.ProductID, products.ProductID, products.ProductName, order_details.UnitPrice, order_details.Quantity, orders.shipVia, shippers.shipperId, shippers.CompanyName, orders.Freight
    FROM customers, orders, order_details, products, shippers
    WHERE customers.CustomerID = orders.CustomerID
    AND orders.OrderID = '" 
$_GET['order'] . "%'
    AND orders.OrderID = order_details.OrderID
    AND order_details.ProductID = products.productID
    AND orders.ShipVia = shippers.ShipperID
    ORDER BY products.productName, products.UnitsInStock, products.UnitPrice, shippers.CompanyName, orders.Freight"




All times are GMT +1. The time now is 06:14 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.