SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding 20% within mysql

    Hey

    I have a table of products with a price excluding tax. The client now wants to display the price with tax (however the net/tax needs to be kept separtely for processing reasons)
    The products are displayed 50 per page, hence i was wondering if its possible to add to the msql call rather than changing the echo in php.

    ..is it possible?

    thanks in advance

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,909
    Mentioned
    95 Post(s)
    Tagged
    0 Thread(s)
    Code SQL:
    SELECT
          price AS price_without_tax
        , price + ((price/100) *20) AS price_with_tax
    FROM
        the_products_table

    That example would get the price without the tax and and the price with tax (assumed to be 20% for the example).
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers, ive got it working...

    Just one little thing about the decimal places that i dont if can be overcome.

    i.e. orginal price (without tax) was 492.00 euros, now it comes in at 590.4, which is of course correct, but could it include another '0'?

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    London
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you're displaying directly from mysql you can use the format function:
    http://dev.mysql.com/doc/refman/5.0/...unction_format

    assuming you want 2 decimals:
    SELECT
    format(price,2) AS price_without_tax
    , format(price + ((price/100) *20),2) AS price_with_tax
    FROM
    the_products_table

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Staffordshire, UK & Florida, USA
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or the less complex: "price * 1.2 as price_with_tax"

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all, got it working...theres over 3k products, so its saved quite a bit of time!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •