Jump to content

[SOLVED] Calculating an array


DarkPrince2005

Recommended Posts

I am trying to calculate the total of values that are obtained from a calculation of fields retrieved from a database, i've searched the web and havent come up with any sollution that works. can someone pls help, as my knowledge is limited when it comes to arrays.

 

<?php
if (!session_id()) {
  session_start();
}//connect to the database
$connect = mysql_connect("localhost", "root", "") 
  or die ("Hey loser, check your server connection.");
mysql_select_db("pc_lan_it") or die (mysql_error());
$sessid = session_id();


$query = "select tbl_cart.product_id as 'id', sum(tbl_cart.cart_qty) as 'a', tbl_cart.cart_qty, tbl_cart.cart_session_id, tbl_cart.cart_id,
tbl_product.product_name as 'name',tbl_product.product_price as ' price',(tbl_product.product_price * tbl_cart.cart_qty) as 'total' from tbl_cart,tbl_product where cart_session_id like '$sessid' and tbl_product.product_id = tbl_cart.product_id group by tbl_cart.product_id";

$results = mysql_query($query);
$results2 = array($query['total']);

echo "<html>
<head>
	<title>Pc,LAN & IT Consulting Web Template</title>

	<style>
	h1{font-size:'13pt';
	}
	  
	.scrollable { 
	width: 650px; 
	height: 95%; 
	overflow: auto;
	background-repeat:no-repeat;
	} 
	 </style>";

	echo "
</head>
<body topmargin='0' bottommargin='0' leftmargin='0' rightmargin='0' bgcolor='#E4E5E7'>
<center><table cellpadding='0' border='0' cellspacing='0' height='100%'>
<tr>
<td colspan='2' height='110' width='893' background='images/top4.gif' align='right' valign='top'>
				<form name='myform1' action='' method='post'>
				<table height='84' cellpadding='0' cellpadding='0' border='0'>
					<tr>
						<td height='36' valign='top' align='right'>
														   <a href='index.php'><img src='images/home.gif' border='0' class='domroll images/home1.gif' alt='Home'></a>
														   <a href='contact.html'><img src='images/contact.gif' border='0' class='domroll images/contact1.gif' alt='Contact Us'></a>
														   <a href='newsletter.html'><img src='images/news.gif' border='0' class='domroll images/news1.gif' alt='Newsletter'></a>
														   <a href='login.html'><img src='images/login.gif'  border='0' class='domroll images/login1.gif' alt='Login'></a>
														   <a href='order.html'><img src='images/order.gif' border='0' class='domroll images/order1.gif' alt='Order'></a>
														   <a href='cart.php'><img src='images/cart.gif' border='0' class='domroll images/cart1.gif' alt='Shopping Cart'></a>     </td>
					</tr>
					<tr>
					<td valign='center' align='right'><input type='text' size='15' name='search'>  <a href=\"javascript:document.myform1.submit()\" onmouseover='document.myform1.sub_but.src='images/search11.gif'' onmouseout='document.myform1.sub_but.src='images/search1.gif''><img src='images/search1.gif' border='0' name='sub_but' alt='Search'></a>   </td>
					</tr>
				</table></form>
</td>
</tr>
<tr>
<td width='192' height='408' background='images/categories_nav.gif' valign='top'><center><font size='1'><br><br><br></font>
	<a href='notebooks.html'><img src='images/notebooks.gif' height='36' border='0' class='domroll images/notebooks1.gif' alt='Notebooks'></a><br>
	<a href='desktops.html'><img src='images/desktops.gif' height='36' border='0' class='domroll images/desktops1.gif' alt='Desktops'></a><br>
	<a href='hardware.html'><img src='images/hardware.gif' height='36' border='0' class='domroll images/hardware1.gif' alt='Hardware'></a><br>
	<a href='software.html'><img src='images/software.gif' height='36' border='0' class='domroll images/software1.gif' alt='Software'></a><br>
	<a href='accessories.html'><img src='images/accessories.gif' height='36' border='0' class='domroll images/accessories1.gif' alt='Accessories'></a><br>
	<a href='handhelds.html'><img src='images/handhelds.gif' height='36' border='0' class='domroll images/handhelds1.gif' alt='Handhelds'></a><br>
	<a href='plasmas.html'><img src='images/plasmas.gif' height='36' border='0' class='domroll images/plasmas1.gif' alt='Plasmas'></a><br>
	<a href='projectors.html'><img src='images/projectors.gif' height='36' border='0' class='domroll images/projectors1.gif' alt='Projectors'></a><br>
	<a href='hometheatersystems.html'><img src='images/homet.gif' height='36' border='0' class='domroll images/homet1.gif' alt='Home Theater Systems'></a><br>
	<a href='printersandscanners.html'><img src='images/printers.gif' height='36' border='0' class='domroll images/printers1.gif' alt='Printers & Scanners'></a><br></td>
<td rowspan='2' width='701' valign='top' align='left' background='images/body.gif'><div class='scrollable'>
	<table border='0' cellpadding='0' cellspacing='0' width='650' height='100%'><tr><td align='center' valign='top'><br><br>

<center><table border='0' width='625' align='center' cellpadding='5'>
  <tr>
    <td>Quantity</td>
    <td>Item Image</td>
    <td>Item Name</td>
    <td>Price Each</td>
    <td>Extended Price</td>
    <td></td>
    <td></td>";

//$total = 0;
while ($row = mysql_fetch_array($results)) {
  echo "<tr>
		<td align='center' valign='center'>
			<form method='POST' action=''>
				<input type='hidden' name='modified_hidden' value='$row[cart_id]'>
				<input type='text' name='modified_quan' size='2' value='$row[a]'>
		</td>
		<td align='center' valign='center'>$row[id]</td>
		<td align='center' valign='center'>$row[name]</td>
		<td align='center' valign='center'>$row[price]</td>
		<td align='center' valign='center'>";
  //get extended price
  $extprice = number_format($row['price'] * $row['a'], 2);
  echo $extprice;
  echo "</td>
		<td align='center' valign='center'><input type='image' src='images/updateqty.gif' name='change' value=\"Change Qty\"  onclick=\"this.form.action='modcart.php?action=change';\"><br>
	  <input type='image' src='images/deleteitem.gif' name='delete' value=\"Delete Item\"  onclick=\"this.form.action='modcart.php?action=delete';\">
        </form></td>";
  echo "</tr>";
  //add extended price to total
  
}
echo "
  <tr>
    <td colspan='4' align='left'>
         Your total before shipping is:</td>
    <td align='center'>"; if (mysql_num_rows($results) > 0) 
{  
print '<ul>';  $sum = array(); // initialize  
while ($myrow = mysql_fetch_array($results)) {   
$sum[] = $myrow['total']; // sum   
$salestotal= $myrow['total'];   
print "<li>$salestotal</li>";  
}  
$sum = array_sum($sum);  
print "</ul>$sum\n"; 
} 
else 
{  
print "<h1>No results</h1>\n"; 
}
   echo "</td>
    
    <td>";

echo "<form method=\"POST\" action=\"modcart.php?action=empty\">
        <input type=\"hidden\" name=\"carttemp_hidden\"
          value=\"";
if (isset($carttemp_hidden)) {
  echo $carttemp_hidden;
}
echo "\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Empty Cart\">
      </form>";
?>

</td>
</tr>
</table>
<form method="POST" action="checkout.php">
<input type="submit" name="Submit" value="Proceed to Checkout">
</form>

<?php
echo "</td></tr></table></div>
</td>
</tr>
<tr>
<td background='images/search.gif' valign='top' align='center'><center><font size='1'><br><br><br></font>
		<form name='myform' action='' method='post'>
			<table cellpadding='0' cellspacing='0' border='0' width='160'>
				<tr>
					<td valign='center'><input type='text' size='15' name='search'>  <a href=\"javascript:document.myform.submit()\" onmouseover=\"document.myform.sub_but.src='images/search11.gif'\" onmouseout=\"document.myform.sub_but.src='images/search1.gif'\"><img src='images/search1.gif' border='0' name='sub_but' alt='Search'></a></td>
				</tr>
			</table>
		</form>
</td>
</tr>
<tr>
<td colspan='2' height='24' background='images/bottom.gif'></td>
</tr>
</table>
<script src='js/chrisdomroll.js'>
</script>";
?>
</body>
</html>

Link to comment
Share on other sites

Here is the code that i am using to calculate the array.

 

if (mysql_num_rows($results) > 0) 
{  
print '<ul>';  $sum = array(); // initialize  
while ($myrow = mysql_fetch_array($results)) {   
$sum[] = $myrow['total']; // sum   
$salestotal= $myrow['total'];   
print "<li>$salestotal</li>";  
}  
$sum = array_sum($sum);  
print "</ul>$sum\n"; 
} 
else 
{  
print "<h1>No results</h1>\n"; 
}

Link to comment
Share on other sites

I havent come up with any sollution that works. can someone pls help, as my knowledge is limited when it comes to arrays.

 

When you say it's not working, how do you mean? Is it returning the wrong thing? Returning nothing at all? Giving a PHP error?

Link to comment
Share on other sites

mysql_fetch_array returns one row at a time.  Because your query returns several rows, the statement "$results2 = array($query['total']);" initializes an array with only one element whose single value is the total of the first row only.

 

Instead, try

 

$totalInt = 0;
while ($row = mysql_fetch_array($results)) {
  $totalInt += $row['total'];
}

 

This should return the sum of all of the 'total' values involved in the query.

 

Link to comment
Share on other sites

well in reality since the data is coming from a database table, you're probably gonna wanna use sum() in your query for example:

 

info

id col1

1  10

2  15

3  8

2  13

2  50

 

$sql = "select sum(col1) as total from info where id = 2";

$result = mysql_query($sql);

$total = mysql_result($result, 0, 0);

echo $total; // output: 78

 

Link to comment
Share on other sites

  • 2 weeks later...

Got it to work here is the code...

 

echo "
  <tr>
    <td colspan='4' align='left'>
         Your total before shipping is:</td>
    <td align='center'>"; 
$E=0;
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)){
$E +=$row['total'];
$total = number_format($E,2);
}
echo $total; // output: 78
   echo "</td>
    
    <td>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.