Ciao a tutti, sto creando una pagina in php relativa agli ordini. Ho un database con i prodotti che contiene le colonne id, nome, descrizione, prezzo, abilitato (per visualizzare solo determinati prodotti), e seleziona.
Io vorrei che nella casella totale mi dia il totale tra quantità è prezzo per ogni riga e nella casella totale (generale) la somma di tutti i totali. Come da figura. Come devo fare???
allego il mio codice:
<html> <body>
<div style="border:10px solid black; width:700px; height:750px; margin:0 auto 0 auto;">
<table border="1" width="400" height="100" cellpadding="0" cellspacing="0" align="center" valign="middle" >
<h3 align= center >Seleziona prodotto</h3>
<?php
$server = "localhost";
$username = "root";
$password = "";
$con = mysql_connect ($server, $username, $password) or die (mysql_error());
if (!mysql_select_db("ordini", $con)) {
echo "<p> C’è stato un errore. Questo è un messaggio d’errore:</p>";
echo "<p><b>".mysql_error()."</b></p>";
echo "Per favore, per dettagli contattare gli amministratori di sistema";
}
/* Prepara la Query SQL */
$sql = "SELECT nome, descrizione, prezzo FROM prodotti WHERE abilitato = true";
/* Invia la Query SQL al DB attivo */
$result = mysql_query($sql, $con);
if (!$result) {
echo("<p>Errore nell’esecuzione della query:".mysql_error()."</p>");
exit();
}
?>
<!-- Inizializza la tabella con le intestazioni -->
<tr>
<td> <b>Nome</b></td>
<td> <b>Descrizione</b></td>
<td> <b>Prezzo</b></td>
<td> <b>Quantita</b></td>
<td> <b>Seleziona</b></td>
<td> <b>Totale</b></td>
</tr>
<?php
/* Recupera le tuple dal result set MySQL e le formatta come righe di tabella HTML */
while ($row = mysql_fetch_array($result)) {
echo("<td>".$row["nome"]."</td>");
echo("<td>".$row["descrizione"]."</td>");
echo("<td>".$row["prezzo"]."</td>");
echo("<td><input type=text name=quantita value=0></td>");
echo("<td><input type=checkbox name=seleziona></td>");
echo("<td><input type=text name=totale value='0.00'></td>\n</tr>\n\n");
}
?>
<!-- Chiude la tabella HTML -->
</table>
<tr style="font-family:Verdana, sans-serif;font-size:8pt;">
<td><b>Totale</b></td>
<td></td>
<td>
<input name="tot" type="text" value="0.00" style="text-align:right; font-weight=bold;" readonly size="7" onClick="aggiornadaclick(id);"><b>Euro</b>
</td>
</tr>
<?php
/* Chiude la connessione col server MySQL */
mysql_close ($con);
?>
<script language="javascript">
var prefix=""
var wd
function parseelement(thisone){
if (thisone.value.charAt(0)=="$") return
wd="w"
var tempnum=thisone.value
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
wd="d"
break
}
}
if (wd=="w")
thisone.value=prefix+tempnum+".00"
else{
if (tempnum.charAt(tempnum.length-2)=="."){
thisone.value=prefix+tempnum+"0"
}
else{
tempnum=Math.round(tempnum*100)/100
thisone.value=prefix+tempnum
}
}
}
function aggiornadaclick(id) {
var totale=0;
var quantita=new Array(document.servizi.elements["quantita"].value);
var prezzo=new Array(document.servizi.elements.$row["prezzo"].value);
document.servizi.elements["quantita"].value=0; // assegna zero a un campo testo
quantita=document.servizi.elements["quantita"].value;
for (i=1;i<=prezzi.length;i++) {
// calcolo del totale
totale+=prezzi[i-1]*quantita[i-1];
}
totale=totale+;
document.servizi.elements["totale"].value=totale;
parseelement(document.servizi.elements["totale"]);
}
</script>
<form name="modulo" action="email.php" method="post">
<table width="400" height="400" cellpadding="0" cellspacing="0" align="center" valign="middle">
<tr>
</tr>
<tr>
</tr>
<h3 align= center >Dati Ordine</h3>
<tr>
<td>Nome</td><td><input type="text" name="nome" size="50"></td>
</tr>
<tr>
<td>Cognome</td><td><input type="text" name="cognome" size="50"></td>
</tr>
<tr>
<td>Via:</td><td><input type="text" name="via" size="50"></td>
</tr>
<tr>
<td>Citta:</td><td><input type="text" name="citta" size="50"></td>
</tr>
<tr>
<td>Provincia:</td><td><input type="text" name="provincia" size="50"></td>
</tr>
<tr>
<td>CAP:</td><td><input type="text" name="cap" size="50"></td>
</tr>
<tr>
<td>C.F:</td><td><input type="text" name="cf" size="50"></td>
</tr>
<tr>
<td>Indirizzo Email:</td><td><input type="text" name="email" size="50"></td>
</tr>
<tr>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Invia email"></td>
</tr>
</table>
</form>
</body> </html>