Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
arlene

Chapter 9 - Subtotal Not Executing Proper Dollar Amount

Recommended Posts

Hello,
I want to be able to list an item for at least $1000.00 dollars. But, in the subtotal column, the decimal point
is not displayed in the correct place. 
For example:
 

 Your Shopping Cart
  
I am able to start out with the decimal point in the hundreds column. I then add a lot of quantities and update. Everything works just fine; the subtotal is correctly updated.   
 
                       ITEM                                      QTY          PRICE          SUBTOTAL           OPTIONS
 
cappuccino machine :: 4 cup capacity             9            $150.00          $1350.00        Move to Wish List
                                                                                                                                    Remove from Cart
 
Your Shopping Cart
 
But, when I start out with a decimal point in the thousands column. The subtotal seems to not grab the correct dollar amount. Instead it displays the decimal point in the ones column. How do I fix this?

 

                       ITEM                                      QTY             PRICE        SUBTOTAL          OPTIONS

 

cappuccino espresso machine ::
Pure cappuccino delight!                                 1            $1,000.00           $1.00           Move to Wish List
                                                                                                                                     Remove from Cart

Share this post


Link to post
Share on other sites
Hello Larry, This is a really goooooood book! I'm still a bit green in PHP, but working through your book, php is starting to click. 

My error must be somewhere in my code but I can't seem to figure it out. I've tried everything I could possibly think of including loading and reloading all the code, but still no answers. 

I've tried reformatting the number_format function to: number_format($subtotal, 2, '.',' '), FORMAT(sc.price/100, 2, '.', ' ') and many other variations with all the number_format functions, but obviously I don't know what I am doing. 

 

So, I have decided to go ahead and add the wishlist.html, wishlist.php, and add the delimiter procedures into the database to see if I could maybe get some answers. I did get something. I got an error message saying: 

 

wishlist.php

 

 

[0] => Array

 

        (

 

            [function] => my_error_handler

 

            [args] => Array

 

                (

 

                    [0] => 8

 

                    [1] => A non well formed numeric value encountered

 

                    [2] => C:\xampp\htdocs\arlene\views\wishlist.html

 

                    [3] => 22

 

                    [4] => Array 

 

.

.

.

[row] => Array

 

                                (

 

                                    [sku] => C1

 

                                    [quantity] => 1

 

                                    [category] => Landscape Paintings

 

                                    [name] => 30X40 - oils - original - exibition canvas

 

                                    [price] => 150000

 

                                    [stock] => 1

 

                                    [sale_price] => 100000

 

                                )

 

 

 

                            [price] => 1,000.00

 

                            [subtotal] => 1

 

                        )

 

 

 

                )

 

 

 

        )

.

.

.

 

 

 

And further down, I got another error message:

 

.

.

.

 

[error] => Commands out of sync; you can't run this command now

 

            [error_list] => Array

 

                (

 

                    [0] => Array

 

                        (

 

                            [errno] => 2014

 

                            [sqlstate] => HY000

 

                            [error] => Commands out of sync; you can't run this command now

 

                        )

.

.

.

 

 

My project I'm trying to build is actually an artist website, that is why I want to be able to list prices at $1000.00 dollars and up.

Any help would greatly be appreciated! Thanks in advance.

Share this post


Link to post
Share on other sites

Sorry for the delay on this; busy summer, and on vacation. Do you still need help here? (And thanks for the nice words on the book!)

Share this post


Link to post
Share on other sites

Yes, yes I still need help with this. And no worries, I understand summer is a crazy busy time.

You are welcome about your book; it is quality (exactly what I was looking for) and I can hardly wait to get my project up and running using your examples.

Share this post


Link to post
Share on other sites

This is strange. I'd start by determining if the subtotal is not being calculated properly or not being formatted properly or not being displayed properly. So I'd echo out the unformatted base price and subtotal to see where the problem may be.

Share this post


Link to post
Share on other sites
Hi Larry, 

I was not exactly sure how to go about echoing back the unformatted base price and subtotal. Instead, I have made sure everything was coded exactly as it is presented in your book. But no success with my ongoing issue.

 

So I have decided to recreate the Coffee Website using the books downloadable files, along with creating the database ecommerce2 (on my local server using xammp).

I then ran the Coffee Site seeing it in action. It works beautifully. 

 

Out of curiosity I then checked to see if the Coffee Site might possibly have the same issue I'm experiencing with my project. To check, I changed the price of an item in the non_products_category 

(to over a thousand dollars) and also in the sales category(one thousand dollars). 

To my surprise the Coffee Site is experiencing the same problem. 

 

I now wonder if there could be an undetected error or glitch in your program for the Coffee Site? Could you check into this? Thank you so much in advance.

 

The Coffee Site:

 

Your Wish List

Please use this form to update your wish list. You may change the quantities, move items to your cart for purchasing, or remove items entirely.

 

An error occurred in script 'C:\xampp\htdocs\ecommerce2\views\wishlist.html' on line 22:

A non well formed numeric value encountered

Array

 

(

 

    [0] => Array

 

        (

 

            [function] => my_error_handler

 

            [args] => Array

 

                (

 

                    [0] => 8

 

                    [1] => A non well formed numeric value encountered

 

                    [2] => C:\xampp\htdocs\ecommerce2\views\wishlist.html

 

                    [3] => 22

 

                    [4] => Array

 

                        (

 

                            [_GET] => Array

 

                                (

 

                                )

 

 

 

                            [_POST] => Array

 

                                (

 

                                )

 

 

 

                            [_COOKIE] => Array

 

                                (

 

                                    [sESSION] => bfea748e21018bbbb8eccc8be4e6165a

 

                                )

 

 

 

                            [_FILES] => Array

 

                                (

 

                                )

 

 

 

                            [uid] => bfea748e21018bbbb8eccc8be4e6165a

 

                            [page_title] => Coffee - Your Wish List

 

                            [dbc] => mysqli Object

 

                                (

 

                                    [affected_rows] => 2

 

                                    [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $

 

                                    [client_version] => 50011

 

                                    [connect_errno] => 0

 

                                    [connect_error] => 

 

                                    [errno] => 0

 

                                    [error] => 

 

                                    [error_list] => Array

 

                                        (

 

                                        )

 

 

 

                                    [field_count] => 7

 

                                    [host_info] => localhost via TCP/IP

 

                                    [info] => 

 

                                    [insert_id] => 0

 

                                    [server_info] => 5.6.16

 

                                    [server_version] => 50616

 

                                    [stat] => 

 

                                    [sqlstate] => HY000

 

                                    [protocol_version] => 10

 

                                    [thread_id] => 118

 

                                    [warning_count] => 0

 

                                )

 

 

 

                            [r] => mysqli_result Object

 

                                (

 

                                    [current_field] => 0

 

                                    [field_count] => 7

 

                                    [lengths] => Array

 

                                        (

 

                                            [0] => 2

 

                                            [1] => 1

 

                                            [2] => 4

 

                                            [3] => 14

 

                                            [4] => 6

 

                                            [5] => 1

 

                                            [6] => 6

 

                                        )

 

 

 

                                    [num_rows] => 2

 

                                    [type] => 0

 

                                )

 

 

 

                            [row] => Array

 

                                (

 

                                    [sku] => G2

 

                                    [quantity] => 1

 

                                    [category] => Mugs

 

                                    [name] => Red Dragon Mug

 

                                    [price] => 150000

 

                                    [stock] => 4

 

                                    [sale_price] => 100000

 

                                )

 

 

 

                            [price] => 1,000.00

 

                            [subtotal] => 1

 

                        )

 

 

 

                )

 

 

 

        )

 

 

 

    [1] => Array

 

        (

 

            [file] => C:\xampp\htdocs\ecommerce2\views\wishlist.html

 

            [line] => 22

 

            [function] => number_format

 

            [args] => Array

 

                (

 

                    [0] => 1,000.00

 

                    [1] => 2

 

                )

 

 

 

        )

 

 

 

    [2] => Array

 

        (

 

            [file] => C:\xampp\htdocs\ecommerce2\wishlist.php

 

            [line] => 82

 

            [args] => Array

 

                (

 

                    [0] => C:\xampp\htdocs\ecommerce2\views\wishlist.html

 

                )

 

 

 

            [function] => include

 

        )

 

 

 

)

 

 

Item                                    Quantity          Price        Subtotal           Options

Mugs::Red Dragon Mug          1              $1.00       $1.00            Move to Cart

                                                                                                        Remove from Wish List

                          There are only 4 left in stock of the Red Dragon Mug.

Kona::1 lb. - decaf - whole      1              $7.00       $7.00             Move to Cart

                                                                                                        Remove from Wish List

-------------------------------------------------------------------------------------------------------------------

 

Your help would be greatly appreciated! Thanks again.

Share this post


Link to post
Share on other sites

Hi all,

 

I had the same problem. It seems 2 problems:

 

Fisrt, qty can get truncated to 127 or 255 in the database. Had to change the type to mediumint(8) to reliably enter semi-reasonable quantities into MYSQL. Couldn't find a mismatch between collation, character sets, it all seemed consistent at UTF8-general, but maybe I missed something there. Had to change to mediumint(8) in the database tables AND stored procedures (qty parameter definition).

 

This first problem affects both the cart and the wishlist, have to fix both places.

 

Second problem, calculating the subtotal fails when in thousands because the number format function returns a string with the comma.

 

try:

echo gettype($price);

echo '<br>' . 'price is ' .  $price;

 

The inherent typecasting to INT apparently is truncating to just the thousands digit. Found this fix online, just insert in cart.php (and wishlist.php) after the get_just_price function but before the subtotal calculation:

 

    $price = htmlentities($price); //important part
    $price = str_replace(array('&#36; ', ','), '', $price); //remove $ and comma

 

Maybe (probably!) Larry has a better work-around... Still not sure why tinyint(3) doesn't seem to cut cut it...

 

  • Upvote 1

Share this post


Link to post
Share on other sites
Wow what a quick fix! Thanks so much daddytiger for sharing your find. So grateful--it's hard to find words to express how much!
 
For me, the second method you have mentioned was all I needed to rectify the problem: 
 

 

$price = htmlentities($price); //important part

$price = str_replace(array('&#36; ', ','), '', $price); //remove $ and comma
 

 

And now my subtotal works perfectly!
 
I can't thank you enough. You have made my day! Cheers.

Share this post


Link to post
Share on other sites

I think this points to a bigger problem. One should really distinguish storing prices from displaying prices. Only display the results of calculations (i.e dollars) to customers, newer work with dollars directly.

  • Upvote 1

Share this post


Link to post
Share on other sites

I'll double-check the code and get back. I'm pretty sure that the code stores prices in integers and handles formatting on the display side. But I'll double-check.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...