select sum of product

Posted on 2006-05-12
Last Modified: 2013-12-12
For some reason this isnt working and its most likley because im rough around the edges, any help please?

Trying to get the sum of a specific product and how many times its been ordered. I get incorrect values fromt the echo.

<?php $presum = $db->Execute("select sum(products_quantity) as products_ordered, products_id
                                                                        from ".TABLE_ORDERS_PRODUCTS . "
                                                                        where products_id = '" . $products->fields['products_id']. "'
                                                                        group by products_id ");
                                                                        echo $presum->fields['products_ordered'];

thanks much
Question by:redcable
    LVL 20

    Expert Comment

    by:Muhammad Wasif
    Your sql is correct.

    >>>I get incorrect values fromt the echo.
    can you explain this?
    LVL 16

    Accepted Solution

    if $products->fields['products_id'] = a single id, say 15, then you query will show sum of products for that specific ID

    products_ordered= 200
    products_id = 15

    this will get u sum of products but not how many times it has been ordered. is that what u mean by not working ??

    maybe u can use
    Execute("select sum(products_quantity) as products_ordered, count(products_id) as q_ordered
                                                                 from ".TABLE_ORDERS_PRODUCTS . "
                                                                where products_id = '" . $products->fields['products_id']. "'
                                                                group by products_id ");

    it should show
    products_ordered= 200
    q_ordered =10 (times)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now