Hoi, ik ben bezig met een compacte winkelwagen waarin staat hoeveel producten je hebt, en wat de totaalprijs is.

Om één of andere reden wordt de prijs verdubbeld, en ik kan nergens vinden waarom dit gebeurd.

Als iemand me kan vertellen wat ik fout doe, of wat ik nog mis, dan zal ik heel erg blij zijn :).

<?php
if(count($articles) > 0){
	if (count($articles) == 1) {
		echo '<span class="wsaantal">Totaal '.count($articles).' product.</span>';
	}else{
		echo '<span class="wsaantal">Totaal '.count($articles).' producten.</span>';
	}
	$arraysize = count($articles);
	$price = '';
	$count = 0;
	while ($count <= $arraysize) {
		foreach($articles as $line) {
			$price = $price + ($line['price']*$line['amount']);
			$count++;
		}
	}
	echo '<span class="wsprijs">Prijs: &euro; '.number_format($price + (($price / 100) * $settings['tax']),2, ',', '').'</span><br />';
	unset($price);
	//echo '<span class="wsprijs">Prijs: &euro; '.number_format(($line['price']*$line['amount']) + ($line['price'] * $line['amount']) / 100 * $settings['tax'],2, ',', '').'</span><br />';
?>
	<div id="afrekenen">
		<a href="/winkelwagen/">Naar product overzicht &raquo;</a>
	</div>
 <?php 
}else{
	echo '<li>Geen producten.</li>';
}
?>
Waarom doe je dat niet in de query, een voorbeeldje uit een eigen project(in CI)
[code]<?php
function getOverview($id = 0) {
if($id !== 0) {
$result=$this->db->query("SELECT COALESCE(SUM(si.amount), 0) total_amount,
SUM(si.amount * IF(si.has_buy_out = 0, p.product_new_price, ROUND(p.product_new_price * 0.9, 2))) total_price
FROM shoppingbags s
JOIN shopbag_items si ON s.bag_id = si.bag_id
JOIN products p ON si.product_id = p.product_id
WHERE s.session_id = '" . $id . "'");
return $result->row();
}
return false;
}
volgens mij kan
<?php
while ($count <= $arraysize) {
foreach($articles as $line) {
$price = $price + ($line['price']*$line['amount']);
$count++;
}
}
?>
gewoon worden:
<?php
foreach($articles as $line) {
$price += $line['price']*$line['amount'];
}
?>

en dan kan lijn 8 ook weg
@Frank: Die while is ook het probleem.

Stel $arraysize = count($articles); geeft 1.
$count = 0;
Dan doet while ($count <= $arraysize) alles dus 2 keer.
1 Keer als $count = 0 en 1 keer als $count = 1.


Toevoeging op 15/09/2013 02:21:45:

Je wilt toch rekenen?
Dan gebruik je geen $price = ''; maar $price = 0;
Precies San, die while moet er uit.
<?php

// ga met functies werken.
function formatPrice($price)
{
return number_format($price, 2, ',', '');
}

function getTaxAmount($price, $tax)
{
return ($price / 100) * $tax;
}

function renderCart($articles)
{
$html = '';
$price = 0;

if(!count($articles))
return '<li>Geen producten.</li>';

if (count($articles) == 1)
$html .= '<span class="wsaantal">Totaal 1 product.</span>';
else
$html .= '<span class="wsaantal">Totaal '.count($articles).' producten.</span>';

foreach($articles as $line)
$price = $price + ($line['price'] * $line['amount']);

$tax = getTaxAmount($price, $settings['tax']);
$total = $price + $tax;

$html .= '<span class="wsprijs">Prijs: &euro; '.formatPrice($total).'</span><br />';

$html .= '<div id="afrekenen"><a href="winkelwagen">Naar product overzicht &raquo;</a></div>';

return $html;
}

echo renderCart($articles);

?>
Bedankt voor jullie hulp Frank en San! De code werkt nu gewoon zoals het hoort :).

Reageren