Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

i need help for sum function


paulchen2005

Recommended Posts

Hi,

 

i need a sum function from php to add the master article with all slave articles

 

i must add all products_quantity from same products_family name

 

see my table on picture

 

 

1cm1.jpg

 

 

the products_id 28 is the Master, the products_id 32 and 33 are the slave article

 

 

you see if i had a slavearticle they have the same name in products_family.

 

now i must sum all products_quantity from same products_family.

 

 

how must look code?

 

can someone help me please ?

 

thank you very much

Link to comment
Share on other sites

if i write

$added= tep_db_query("select sum(products_quantity) as products_family from products");

 

becomes with

echo $added;

 

indicated

 

Resource id #130

 

 

who is my error, what can i do to add all products_quantity from same name in products_family ???

Link to comment
Share on other sites

I don't really know the answer to your problem, but you need to use something like

 

tep_db_fetch_array($added) after you use $added = tep_db_query

 

so you're on the right path, you just need the fetch_array function too.

 

that will put your results in an array, then you can pull out what you need and echo it the way you want

 

There's an explanation of mysql_fetch_array here

 

http://us3.php.net/manual/en/function.mysql-fetch-array.php

Link to comment
Share on other sites

Hmm...yes. That's because all it is is "array"...you need to go through and pull out the pieces of the array.

 

Did you look at the examples at php.net?

 

This one is basically what tep_db_query and tep_db_fetch_array are doing

 

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
  die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?>

 

You have to go through the while loop to pull out the pieces.

 

I'm still not clear on what you're trying to do...but I hope the info helps nonetheless :huh:

Link to comment
Share on other sites

Hi,

 

long time bevore i write this post i start a post to write in database, but that is not poss,

 

now i have a code to show the sum...

 

$add = tep_db_query("select sum(products_quantity) as true_qty from products where products_family='dekorative H'");
$add_result = tep_db_fetch_array($add);
echo $add_result['true_qty'];

 

and the last question is

 

what can i write to sum all products_quantity from the same name in products_family from the product what i look

 

look here:

 

http://www.oscommerce.com/forums/index.php?sho...id=911679

 

 

thank you for help me !!!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...