Here's what I threw together. Uses Mike's slightly-famed Number.toCurrency() function.
Code:
<?php
// test.php
function toCurrency($amount) {
$amount *= 100;
$amount = round($amount);
$amount /= 100;
if(strpos($amount, ".") === false)
return $amount . ".00";
while(strlen(substr($amount, ".")) < 2)
$amount .= "0";
return $amount;
}
function getSelectedItems($items) {
$si = array();
foreach($_POST as $k => $v)
if(isset($items[$v])) array_push($si, $items[$v]);
return $si;
}
function formatItemList($items) {
$si = getSelectedItems($items);
$rt = 0;
$html = '<table style="text-align:center;"><tr><th>Item</th><th>Price</th><th>Running Total</th></tr>';
for($i = 0; $i < count($si); ++$i)
$html .= "<tr><td>" . $si[$i]['name'] . "</td><td>\$" . toCurrency($si[$i]['price']) . "</td><td>\$" . toCurrency($rt += $si[$i]['price']) . "</tr>";
$html .= "<tr style=\"font-weight:bold;\"><td>Total</td><td>\$" . toCurrency($rt) . "</td><td>\$" . toCurrency($rt) . "</td></tr></table>";
return $html;
}
$items = array(
"os0" => array(
"name" => "Microsoft Windows XP Professional Edition SP2",
"price" => 135.0
),
"os1" => array(
"name" => "Microsoft Windows XP Home Edition SP2",
"price" => 90.0
),
"os2" => array(
"name" => "Gentoo Linux",
"price" => 10.0
),
"os3" => array(
"name" => "Fedora Core 5",
"price" => 5.0
),
"os4" => array(
"name" =>"No OS",
"price" => 0.0
),
"cpu0" => array(
"name" => "AMD Athlon 64 FX55 San Diego 2000MHz HT Socket 939",
"price" => 639.0
),
"cpu1" => array(
"name" => "AMD Athlon 64 FX57 San Diego 2000MHz HT Socket 939",
"price" => 811.0
),
"cpu2" => array(
"name" => "AMD Athlon 64 FX60 Toledo 2000MHz HT Socket 939 Dual Core",
"price" => 853.0
)
// Add more. Doesn't matter what the names are, so long as they match the values on the form.
);
if(!isset($_POST['calculate'])) { ?>
<html>
<head>
<title>F</title>
<script type="text/javascript">
Number.prototype.toCurrency = function(c, t, d) {
// Mike Winter (mwinter)'s Number.toCurrency() function.
var n = +this,
s = (0 > n) ? '-' : '',
m = String(Math.round(Math.abs(n))),
i = '',
j, f;
c = c || '';
t = t || '';
d = d || '.';
while (m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while (j > 3) {i = t + m.substring(j - 3, j) + i; j -= 3;}
i = m.substring(0, j) + i;
return s + c + i + d + f;
};
var items = new Object();
<?php foreach($items as $k => $v) { ?>
items['<?php echo($k); ?>'] = <?php echo(toCurrency($v['price']) * 100); ?>;
<?php } ?>
function calcTotal(frm) {
var total = 0;
for(var i=0; i<frm.elements.length; i++) {
if(typeof items[frm.elements[i].value] != "undefined")
total += items[frm.elements[i].value];
}
return total;
}
window.onload = function() {
for(var i=0;i<document.forms[0].elements.length;i++)
if(document.forms[0].elements[i].tagName.toLowerCase() == "select")
document.forms[0].elements[i].onchange = function() {
document.getElementById("ttl").innerHTML = calcTotal(this.form).toCurrency('$');
};
document.getElementById("ttl").innerHTML = calcTotal(document.forms[0]).toCurrency('$');
};
</script>
</head>
<body>
<form action="<?php echo($PHP_SELF); ?>" method="post">
<input type="hidden" name="calculate" value="true">
<select name="os">
<?php for($i = 0; isset($items["os$i"]); $i++) { ?>
<option value="os<?php echo($i); ?>"><?php echo($items["os$i"]['name']); ?> ($<?php echo(toCurrency($items["os$i"]['price'])); ?>)</option>
<?php } ?>
</select>
<br>
<select name="cpu">
<?php for($i = 0; isset($items["cpu$i"]); $i++) { ?>
<option value="cpu<?php echo($i); ?>"><?php echo($items["cpu$i"]['name']); ?> ($<?php echo(toCurrency($items["cpu$i"]['price'])); ?>)</option>
<?php } ?>
</select>
<br>
<p style="font-weight:bold;">
Total: <span id="ttl"></span>
<input type="submit">
</p>
</form>
</body>
</html>
<?php } else { ?>
<html>
<head>
<title>Confirmation Page</title>
</head>
<body>
<?php echo(formatItemList($items)); ?>
</body>
</html>
<?php } ?>
It lacks your formatting and data, since I was merely experimenting. I'm sure you should have no trouble integrating it.
Marginally tested.
Bookmarks