Forum Moderators: coopster

Message Too Old, No Replies

Update value without reloading the browser

         

nimonogi

3:03 pm on Apr 24, 2010 (gmt 0)

10+ Year Member



Hello,

I have the following code which is used in a shopping cart script.


function getBasket(){

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

$query = "SELECT * FROM baskets WHERE basketSession = '" . $sessionID . "' GROUP BY productID ORDER By basketID DESC";
$result = mysql_query($query);
//echo $query;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

$query2 = "SELECT * FROM products WHERE productID = " . $row['productID'];
$result2 = mysql_query($query2);
$row2 = mysql_fetch_array( $result2 );

$productID = $row2['productID'];
$productPrice = $row2['productPrice'];
$productName= $row2['productName'];
$query2 = "SELECT COUNT(*) AS totalItems FROM baskets WHERE basketSession = '" . $sessionID . "' AND productID = " . $productID;
$result2 = mysql_query($query2);
$row2 = mysql_fetch_array( $result2 );
$totalItems = $row2['totalItems'];
$finalPrice = $finalPrice + ($totalItems * $productPrice);
$basketText = $basketText . '<li id="productID_' . $productID . '"><a href=inc/functions.php?action=deleteFromBasket&productID=' . $productID . ' onClick="return false;"><img src="images/delete.png" id="deleteProductID_' . $productID . '"></a> ' . $productName . '(' . $totalItems . ' items) - $' . ($totalItems * $productPrice) . '</li>';
}
echo $basketText;

echo 'TOTAL:' . sprintf("%01.2f",($finalPrice);
}


My problem is with this part of code:

echo sprintf("%01.2f",($finalPrice);


I want the value of $finalPrice to be update each time a new product is added to the basket.
Currently, shows the correct value only when i reload the browser.

Thanks in advance!

rocknbil

8:41 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, you have an error:

echo 'TOTAL:' . sprintf("%01.2f",($finalPrice);

has an extra parenthesis, should be

echo 'TOTAL:' . sprintf("%01.2f",$finalPrice);

Currently, shows the correct value only when i reload the browser


Because you're not compiling $finalPrice until you get results from the query, no results, no price. Add this.

function getBasket(){
var $finalPrice=0;
// etc.
}

Just matters of efficiency and good practice: the extra var shouldn't be necessary, even if $finalPrice has a previous value:

$finalPrice = $finalPrice + ($totalItems * $productPrice);

just needs

$finalPrice += ($totalItems * $productPrice);

Second, I'd re-think the way you're using the function, echoing out in mid stream. This takes away some of the purpose of paring off tasks into functions, to make them portable. Instead of echoing inside the function, do something like this:

list ($basket_text, $total) = getBasket();
echo "$basket_text, $total";


Then in in getBasket,

function getBasket() {

// etc.

return Array($basketText,sprintf("%01.2f",$finalPrice));
}

Then you can separate the actual program from the functions, maybe even moving them into includes.

nimonogi

1:34 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



rocknbil thanks for your help, but this does not solve my problem.

Anyone?

jatar_k

2:41 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this does

ajax

arvind gupta

1:37 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Hi nimonogi,

Use AJAX as told by jatar_k. You might want to have a lokk at xajax libarary for php - very easy to use.