Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Cash Register Troubles


PHP Cash Register Troubles

Reply
  #1 (permalink)  
Old 12-04-07, 10:30 AM
tophat's Avatar
tophat tophat is offline
Newbie Coder
 
Join Date: Dec 2005
Location: Michigan
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb PHP Cash Register Troubles

I am writing a php powered cash register that will be used for my school store. They scan the barcode information, it's sent to multiple scripts using AJAX, and the output is displayed on the screen (items bought, tax, subtotal, total, etc...). One of the files it sends data to is price.php, which takes in a barcode ($id) and the price of the product ($x - which isn't really necessary, it was just an old variable i planned on using but i think i took it out). the problem is it isn't displaying things 'up-to-date'. lets say i added the first product, which cost 15.99. it would return 0 for tax, subtotal, and total. then if you scanned that same product again, it would then display subtotal 15.99, tax .95, and total 16.94, even though it is really subtotal 31.98, tax 1.9, and total 33.88, which is the correct data that is held in the cookie. I am wondering why, even though it's pulling the data from the cookie, the output on the screen is different than is what is in the cookie. Maybe it takes time for the cookie to register/update? If anyone can help me with this that would be great


PHP Code:

<?php


// Price.php

define("DB_SERVER""localhost"); // Server
define("DB_USER""root"); // Username
define("DB_PASS"""); // User pass
define("DB_NAME""products"); // Database

mysql_connect(DB_SERVERDB_USERDB_PASS) or die(mysql_error()); // Connection
mysql_select_db(DB_NAME) or die(mysql_error()); // Selection of database

function add_cookie($cookie_total$sub$tax) {

    
$cookie $_COOKIE["total"];
    
$cookie2 $_COOKIE["sub_total"];
    
$cookie3 $_COOKIE["tax"];

    
setcookie("total"$cookie_totaltime()+3600);

    
setcookie("sub_total"$cookie2 $subtime()+3600);

    
setcookie("tax"$cookie3 $taxtime()+3600);

}

function 
display($total$sub$tax) {

    echo 
"<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td>Sub:</td><td>$" $sub "</td></tr></table><hr><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td>Tax:</td><td>$" $tax "</td></tr></table><hr><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr valign=\"middle\"><td><br>Total:</td><td><b>$" $total "</b></tr></td></table>";

}

$x $_POST['x'];
$id $_POST['id'];

$query mysql_query("SELECT * FROM products WHERE bar_id='$id'");
$f mysql_fetch_array($query);
$n mysql_num_rows($query);

if(
$x && $id) { // Pre-sets all cookies

    
$cookie $_COOKIE["total"];
    
$cookie2 $_COOKIE["sub_total"];
    
$cookie3 $_COOKIE["tax"];

    if(!
$cookie) {

        
setcookie("total"$f['price'], time()+3600); // Add tax to this!

    
}

    if(!
$cookie2) { // Tax

         
if($f['tax'] == "yes") {

            
$tax $f['price'] * .06;
            
$total $f['price'] + $tax;

            
setcookie("total"$totaltime()+3600);

        } else {

            
setcookie("total"0.00time()+3600);

        }

    }

    if(!
$cookie3) { // Subtotal

        
setcookie("sub_total"$f['price'], time()+3600);

    }

} else {

    echo 
"Missing barcode or other essential data.  All queries closed.";

    exit();

}

if(
$n == 1) {

    if(
$f['price']) {

        if(
$f['tax'] == "yes") {

            
$tax $f['price'] * .06;
            
$total $f['price'] + $tax;

            
$cookie $_COOKIE["total"];
            
$cookie2 $_COOKIE["sub_total"];
            
$cookie3 $_COOKIE["tax"];

            if(
$cookie) { // Cookie, Tax

                
$cookie_total $cookie $total;

                
add_cookie($cookie_total$f['price'], $tax); // Function above

                
$round round($cookie_total2);

                
display($round$cookie2$cookie3); // add $f[price] and $tax because cookies take long to register ?



            
} else { // No Cookie, Tax

                
add_cookie($f['price'] + $tax$f['price'], $tax); //

                
$round round($total2);

                
display($round$f['price'], $tax);

            }

        } else { 
// No Tax

            
$cookie $_COOKIE["total"];

            if(
$cookie) { // Cookie, No Tax


                
$cookie_total $cookie;

                
add_cookie($cookie_total$f['price'], 0); // Function above

                
$round round($cookie_total $f['price'], 2);

                
display($round$cookie2$cookie3);

            } else { 
// No Cookie, No Tax

                
add_cookie($f['price'], $f['price'], 0); //

                
$round round($f['price'], 2);

                
display($round$f['price'], 0);

            }

        }

    } else {

        echo 
"No price";

    }

} else {

    echo 
"NO SUCH";

}

?>
__________________
tophatcomedy.com
Reply With Quote
  #2 (permalink)  
Old 12-04-07, 03:40 PM
robertark's Avatar
robertark robertark is offline
Newbie Coder
 
Join Date: Dec 2007
Location: Springfield, MO
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
It is possible that the cookies are not being overwritten (or are) - Try using a session array if possible with a unique identifier (such as $_SESSION['something1']['something2'] = 'value' - or similar)

If you do, don't forget to use session_start();
__________________
Knowledge Is Power And Without Power, You're Nobody. -Self
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
how to use onclick in php coolcoder HTML/XHTML/XML 2 05-31-06 12:40 PM
How to register system user in linux (php) shellter PHP 2 01-02-05 09:58 AM
PHP Runner , Preview PHP files ! moslehi@gmail.com General Advertisements 3 12-08-04 03:01 PM


All times are GMT -5. The time now is 07:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.