I have a question regarding arrays in php.
I have two array's, one contains product prices and the other array is empty but get's filled with a dollar discount value calculated from a discount function which is included.
I want to display the discount for just one specific product for which I picked the discount.
The solution I thought up of was to compare the two array's and displaying the discount for just the product where the discount array element changed (please correct me if this is the wrong way of doing it)
My problem is that when I select a discount, I have no way of updating the correct element in the discount array.
Instead the entire array gets filled with the dollar discount for all the products (the price array).
How would I go about doing this.
This is the entire function with the while loop, the code btw is open source. The project is called Supercart (sourceforge)
Code: Select all
function displayorderitems( $itemText, $orderid = 0 )
{
global $config;
$line_no = 1;
$numitems = 0;
if ( $orderid == 0 )
{
$query = "SELECT * FROM cartitems WHERE cartid = '".$_SESSION['cartid']."' ORDER BY itemid";
if ( $_REQUEST['action'] == "confirm" )
{
$hidezeros = TRUE;
}
}
else
{
$query = "SELECT *, orderdetail.orderid AS orderid, orderdetail.description AS description, UNIX_TIMESTAMP(downloaddate) AS downloaddatets FROM orderdetail \r\n\t\t\tLEFT JOIN orders ON orderdetail.orderid = orders.orderid \r\n\t\t\tWHERE orderdetail.orderid = '{$orderid}' ORDER BY itemid";
}
$result = mysql_query( "SELECT stateid FROM orders WHERE orderid = '{$orderid}'" );
$row = mysql_query( $result );
$stateid = $row['stateid'];
$result = mysql_query( $query );
$taxrate1 = gettaxrate1( $stateid );
$taxrate2 = gettaxrate2( $stateid );
$startpos = strpos( $itemText, "<!-- BEGIN OPTIONS -->" );
if ( 0 < $startpos )
{
$endpos = strpos( $itemText, "<!-- END OPTIONS -->", $startpos ) + 20;
$optiontemplate = substr( $itemText, $startpos, $endpos - $startpos );
$optiontemplate = str_replace( "<!-- BEGIN OPTIONS -->", "", $optiontemplate );
$optiontemplate = str_replace( "<!-- END OPTIONS -->", "", $optiontemplate );
$itemText = substr( $itemText, 0, $startpos )."%%OPTIONS%%".substr( $itemText, $endpos );
}
$numproducts = 0;
$pricesarray = array();
$discountsarray = array();
while ( $record = mysql_fetch_assoc( $result ) )
{
$totaltax = 0;
$totalshipping = 0;
$tax = calculatetax( $record, $taxrate1, $taxrate2 );
if ( 0 < $record['quantity'] )
{
$totalshipping = $record['shipping'] + $record['shipping1'];
if ( 2 <= $record['quantity'] )
{
$totalshipping += $record['shipping2'] * ( $record['quantity'] - 1 );
}
$numitems += $record['quantity'];
$totaltax = $tax * $record['quantity'];
++$numproducts;
}
if ( $optiontemplate != "" )
{
$optiontext = getoptiontext( $record, $optiontemplate );
}
$optionvalue = getoptionstotal( $record );
$productprice = $record['price'] + $optionvalue;
$totalproductprice = $productprice * $record['quantity'];
if ( $record['quantity'] == "0.000" && $hidezeros == TRUE )
{
continue;
}
if ( $_SESSION['CouponIsValid'] )
{
require_once( $config['filepath']."modules/coupon.product.inc.php" );
$discountProduct = CalculateCouponProduct( getcouponproduct( ), $totalproductprice, $totalshipping, $totaltax );
}
else
{
$discountProduct = 0;
}
if ( $config['pricewithtax'] == "Y" )
{
$optionvalue = getpricewithtax( $optionvalue, $record['taxid'] );
$productprice = getpricewithtax( $record['price'], $record['taxid'] ) + $optionvalue;
$totalproductprice = $productprice * $record['quantity'] ; //- $discountProduct;
$pricesarray[]= $totalproductprice ;
$discountsarray[]= $discountProduct;
$grandtotal = $totalproductprice + $totalshipping;
}
else
{
$grandtotal = $totalproductprice + $totalshipping + $totaltax;
}
if ( $record['shipped'] == "Y" )
{
$shipped = $config['langshipped'];
}
else if ( $record['shipped'] == "D" )
{
if ( mktime( ) - $config['downloadhours'] * 3600 < $record['downloaddatets'] )
{
$shipped = $config['langdownloadable'];
}
else
{
$shipped = $config['langdownloadexpired'];
}
}
else
{
$shipped = $config['langnotshipped'];
}
if ( 0 < $record['downloads'] )
{
$shipped = "<a href=\"%%CARTURL%%?action=download&key=xxx\">".$config['langdownload']."</a>";
}
$itemTextDisplay = str_replace( "%%ID%%", $record['itemid'], $itemText );
if ( $record['hidequantityfield'] == "Y" )
{
$startpos = strpos( $itemTextDisplay, "<!-- BEGIN QUANTITY -->" );
if ( 0 < $startpos )
{
$endpos = strpos( $itemTextDisplay, "<!-- END QUANTITY -->", $startpos ) + 22;
$itemTextDisplay = substr( $itemTextDisplay, 0, $startpos ).quantityformat( $record['quantity'] ).substr( $itemTextDisplay, $endpos );
}
}
if ( $record['image'] == "" )
{
$startpos = strpos( $itemTextDisplay, "<!-- BEGIN THUMBIMAGE -->" );
if ( 0 < $startpos )
{
$endpos = strpos( $itemTextDisplay, "<!-- END THUMBIMAGE -->", $startpos ) + 23;
$itemTextDisplay = substr( $itemTextDisplay, 0, $startpos ).substr( $itemTextDisplay, $endpos );
}
}
if ( $record['url'] == "" )
{
$record['url'] = "#";
}
$itemTextDisplay = str_replace( "%%THUMBIMAGE%%", $record['image'], $itemTextDisplay );
$itemTextDisplay = str_replace( "%%PRODUCTURL%%", $record['url'], $itemTextDisplay );
$itemTextDisplay = str_replace( "%%PRODUCTCODE%%", $record['productcode'], $itemTextDisplay );
$itemTextDisplay = str_replace( "%%NAME%%", $record['description'], $itemTextDisplay );
$itemTextDisplay = str_replace( "%%SHORTTEXT%%", $record['shorttext'], $itemTextDisplay );
$itemTextDisplay = str_replace( "%%SHORTTEXTSTRIPPED%%", strip_tags( $record['shorttext'] ), $itemTextDisplay );
if ( $productprice == 0 )
{
$itemTextDisplay = str_replace( "%%BASEPRICE%%", "", $itemTextDisplay );
}
else
{
$itemTextDisplay = str_replace( "%%BASEPRICE%%", currencyformat( $productprice - $optionvalue ), $itemTextDisplay );
}
$itemTextDisplay = str_replace( "%%OPTIONS%%", $optiontext, $itemTextDisplay );
$itemTextDisplay = str_replace( "%%QUANTITY%%", quantityformat( $record['quantity'] ), $itemTextDisplay );
$itemTextDisplay = str_replace( "%%PRICE%%", currencyformat( $productprice ), $itemTextDisplay );
$itemTextDisplay = str_replace( "%%TAX%%", currencyformat( $totaltax ), $itemTextDisplay );
$itemTextDisplay = str_replace( "%%SHIPPING%%", currencyformat( $totalshipping ), $itemTextDisplay );
$itemTextDisplay = str_replace( "%%FULLTOTAL%%", currencyformat( $grandtotal ), $itemTextDisplay );
$itemTextDisplay = str_replace( "%%TOTAL%%", currencyformat( $totalproductprice ), $itemTextDisplay );
$itemTextDisplay = str_replace( "%%ROW%%", $line_no, $itemTextDisplay );
$itemTextDisplay = str_replace( "%%SHIPPED%%", $shipped, $itemTextDisplay );
if ( $line_no % 2 == 1 )
{
$itemTextDisplay = str_replace( "%%ROWCOLOR%%", $config['rowcolor1'], $itemTextDisplay );
}
else
{
$itemTextDisplay = str_replace( "%%ROWCOLOR%%", $config['rowcolor2'], $itemTextDisplay );
}
if ( $record['extra1'] != "" )
{
$itemTextDisplay = str_replace( "%%EXTRA1%%", $record['extra1'], $itemTextDisplay );
}
else
{
$startpos = strpos( $itemTextDisplay, "<!-- BEGIN EXTRA1 -->", 0 );
if ( 0 < $startpos )
{
$endpos = strpos( $itemTextDisplay, "<!-- END EXTRA1 -->", $startpos ) + 19;
$itemTextDisplay = substr( $itemTextDisplay, 0, $startpos ).substr( $itemTextDisplay, $endpos );
}
}
if ( $record['extra2'] != "" )
{
$itemTextDisplay = str_replace( "%%EXTRA2%%", $record['extra2'], $itemTextDisplay );
}
else
{
$startpos = strpos( $itemTextDisplay, "<!-- BEGIN EXTRA2 -->", 0 );
if ( 0 < $startpos )
{
$endpos = strpos( $itemTextDisplay, "<!-- END EXTRA2 -->", $startpos ) + 19;
$itemTextDisplay = substr( $itemTextDisplay, $startpos, $endpos - $startpos );
}
}
if ( $record['extra3'] != "" )
{
$itemTextDisplay = str_replace( "%%EXTRA3%%", $record['extra3'], $itemTextDisplay );
}
else
{
$startpos = strpos( $itemTextDisplay, "<!-- BEGIN EXTRA3 -->", 0 );
if ( 0 < $startpos )
{
$endpos = strpos( $itemTextDisplay, "<!-- END EXTRA3 -->", $startpos ) + 19;
$itemTextDisplay = substr( $itemTextDisplay, $startpos, $endpos - $startpos );
}
}
++$line_no;
$itemDisplay .= $itemTextDisplay."\n";
}
if ( $line_no == 1 && $config['tag']['MESSAGE'] == "" || $numproducts == 0 && $config['tag']['MESSAGE'] == "" )
{
$config['tag']['MESSAGE'] = $config['langemptycart'];
}
print_r ($pricesarray);
print_r ($discountsarray);
return $itemDisplay;
}