Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12

Discussione: calcolo somma in php

  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    196

    calcolo somma in php

    ciao ragazzi,mi serve un consiglio e possibilmente un aiuto...
    devo fare una somma di alcuni valori inseriti in alcuni campi(nn so a priori il numero,sono in un'area ripetuta);volevo evitare di utilizzare il javascript (magari su certi pc nn è supportato a livello client) e volevo farlo in php..sapete se si può fare una cosa del genere e se si come?
    la mia area ripetuta è questa
    <?php
    $counter = 0;
    do { $counter++;
    ?>
    <tr>
    <td>
    <?php VisualNomeProdotto ($row_visual_prod_asta['Id_prodotto']); ?>
    </td>
    <td><input name="Prezzo_prod" type="text" id="Prezzo_prod<?php echo $counter; ?>"> euro </td>
    </tr>
    <?php } while ($row_visual_prod_asta = mysql_fetch_assoc($visual_prod_asta)); ?>

    in questo modo conto il numero di righe riferite ai prodotti che mi servono,e il risultato delle righe lo stampo in questo campo
    <input type=\"text\" id=\"numeroRighe\" value="<?php echo $counter;?>">

    il mio problema è riuscire a fare la somma dei prezzi che vengono inseriti per ogni prodotto;mi serve soltanto che mi visualizzi il totale(che devo salvare nel database) nn i calcoli intermedi.
    Qualcuno mi può aiutare?

    ciao
    PHP, ASP, Javascript

  2. #2
    metti come name
    name="Prezzo_prod[]"
    cosicche nel php avrai un array
    poi nn ti resta che scorrerlo e fare la somma
    Il Ticino è biancoblù
    DVDS delegato ufficiale

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    196
    ok,come faccio a scorrerlo runtime per avere il rusltato in un campo?
    potresti farmi vedere come bisogna fare?

    ciao grazie
    PHP, ASP, Javascript

  4. #4
    con quel form ti verrà restituito via post un array Prezzo_prod
    lo scorri cosi:
    Codice PHP:
    $somma 0;
    foreach(
    $_POST['Prezzo_prod'] as $value){
       
    $somma += $value;
    }
    echo 
    $somma
    alla fine $somma ti retituira la somma
    Il Ticino è biancoblù
    DVDS delegato ufficiale

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    196
    ti spiego bene il mio problema. nella mia pagina ci sono due form,nel primo ho una serie di valori che deve inserire l'utente tra i quali il totale dell'offerta che deve fare(nel database mi interessa che venga registrato soltanto questo nn i parziali).
    e il form si chiude con un bottone di inserimento.

    Sotto ho il secondo form in cui ho una tabella con i vari prodotti (pezzo di codice che ho postato prima) per i quali l'utente può inserire il prezzo di ciascuno e poi il totale sarà il prezzo che mette nel primo form,tutto dovrebbe avvenire nella stessa pagina..ho provato a mettere un bottone alla fine del secondo form e far aprire una finestra in cui ho inserito il tuo codice ma mi dà errore(Invalid argument supplied for foreach())

    se vuoi ti posto tutto il codice della pagina così può capire meglio il prob..

    ciao
    PHP, ASP, Javascript

  6. #6
    posta il codice che almeno posso darci un occhiata
    Il Ticino è biancoblù
    DVDS delegato ufficiale

  7. #7
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    196
    <?php require_once('Connections/Connessione.php'); ?>

    if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
    }

    if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO offerte (Id_asta, Prezzo_offerta, Data_dell_offerta, Orario_offerta, Id_referente_offerta) VALUES (%s, %s, %s, %s, %s)",
    GetSQLValueString($HTTP_POST_VARS['Nome_asta'], "int"),
    GetSQLValueString($HTTP_POST_VARS['Prezzo_off'], "int"),
    GetSQLValueString($HTTP_POST_VARS['Data_off'], "date"),
    GetSQLValueString($HTTP_POST_VARS['Orario_off'], "date"),
    GetSQLValueString($HTTP_POST_VARS['Id_ref'], "int"));

    mysql_select_db($database_Connessione, $Connessione);
    $Result1 = mysql_query($insertSQL, $Connessione) or die(mysql_error());
    }
    ?>
    <script language="JavaScript">
    <!--

    function calcola_tot(Link)
    {
    newwindow=window.open(Link,"","top=50,left=200,wid th=550,height=300")
    }


    //-->
    </script>
    <html>
    <head>

    <title>Inserisci offerta</title>
    <META http-equiv="REFRESH" content="30">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="aste.css" rel="stylesheet" type="text/css">
    </head>

    <body onLoad="startclock(); timerONE=window.setTimeout">


    </p>
    <h3 class="gray">Offerta del Sig.<font color="#FF0000">
    <?php VisualCognomeReferente($row_visual_offerente['Id_referente']);?>
    </font></h3>
    <p class="gray"><font color="#FF0000"></font></p>
    <form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
    <table width="75%" border="1" class="tab">
    <tr>
    <td width="34%">Id referente </td>
    <td width="66%"><input name="Id_ref" type="text" class="textbox" id="Id_ref" value="<?php echo $row_visual_offerente['Id_referente'];?>">
    </td>
    </tr>
    <tr>
    <td>Titolo dell'asta</td>
    <td> <input name="Nome_asta" type="hidden" id="Nome_asta" value="<?php echo $row_visual_asta['Id_asta']; ?>">
    <?php VisualNomeAsta ($row_visual_asta['Id_asta']);?>
    </td>
    </tr>
    <tr>
    <td>Stile dell'asta</td>
    <td><input name="Stile_asta" type="hidden" id="Stile_asta" value="<?php echo $row_visual_asta['Id_stile_asta']; ?>">
    <?php VisualNomeStileAsta ($row_visual_asta['Id_stile_asta']);?>
    </td>
    </tr>
    <tr>
    <td>Banditore</td>
    <td><input name="Id_referente" type="hidden" id="Id_referente" value="<?php echo $row_visual_asta['Id_referente']; ?>">
    <?php VisualCognomeReferente ($row_visual_asta['Id_referente']);?>
    </td>
    </tr>
    <tr>
    <td height="26">Tipo di asta</td>
    <td>
    <?php
    if ($row_visual_asta['Rialzo'] == 'Yes')
    {
    echo ("Rialzo");
    }
    else
    {
    print ("Ribasso");
    }
    ?>
    </td>
    </tr>
    <tr>
    <td height="26">Data e ora di apertura dell'asta</td>
    <td>
    <?php ConvDate($row_visual_asta['Data_apertura']); ?>
    <?php echo $row_visual_asta['Orario_apertura']; ?></td>
    </tr>
    <tr>
    <td height="26">Data e ora di chiusura</td>
    <td>
    <?php ConvDate($row_visual_asta['Data_chiusura']); ?>
    <?php echo $row_visual_asta['Orario_chiusura']; ?> </td>
    </tr>
    <tr>
    <td height="26">Prezzo di partenza dell'asta</td>
    <td><input name="Prezzo_part" type="text" disabled class="textbox" id="Prezzo_part" value="<?php echo $row_visual_asta['Prezzo_apertura']; ?>">
    &euro; </td>
    </tr>
    <tr>
    <td height="26">Prezzo di riserva</td>
    <td><input name="Prezzo_riserva" type="text" disabled class="textbox" id="Prezzo_riserva" value="<?php echo $row_visual_asta['Prezzo_riserva']; ?>">
    &euro; </td>
    </tr>
    <tr>
    <td>Numero delle offerte possibili</td>
    <td><input name="Num_off_max" type="text" id="Num_off_max" value="<?php echo $row_visual_asta['Num_off_max']; ?>"></td>
    </tr>
    <tr>
    <td>Numero di possibili offerte rimaste </td>
    <td> <table width="100%" border="0">
    <?php

    if ($row_num_offerte['Id_offerta'] == "")
    {
    $contatore= -1;
    }
    else
    {
    $contatore = 0;
    }
    do
    {
    $contatore++;
    ?>
    <tr>
    <td> <input type="hidden" name="Id_off" value="<?php echo $row_num_offerte['Id_offerta']; ?>">
    </td>
    <td><input name="Offerta" type="hidden" id="Offerta" value="<?php echo $contatore; ?>">
    </td>
    </tr>
    <?php } while ($row_num_offerte = mysql_fetch_assoc($num_offerte)); ?>
    <?php
    $calcola_offerte = $row_visual_asta['Num_off_max'];

    //print "$calcola_offerte";
    //print "$contatore";

    $Offerte_restanti = $calcola_offerte - $contatore;

    if ($Offerte_restanti == 0)
    {
    echo "<input name=\"off_rimanenti\" type=\"text\" class=\"textbox\" disabled id=\"off_rimanenti\" value=\"0\">";
    }
    else
    {
    echo "<input name=\"off_rimanenti\" type=\"text\" class=\"textbox\" disabled id=\"off_rimanenti\" value=\"$Offerte_restanti\">";
    }

    ?>
    </table></td>
    </tr>
    <tr>
    <td height="26">Prezzo offerta</td>
    <td> <input name="prezzo_off" type="text" id="prezzo_off"> </td>
    </tr>
    <tr>
    <td>Data dell'offerta</td>
    <td> <input name="Data_off" type="text" class="textbox" id="Data_off" value="<?php
    //recupera la data attuale
    print (date ("Y-m-d"));
    //ConvDate(date("Y-m-d"));
    //time("H:m:s")?>"></td>
    </tr>
    <tr>
    <td>Orario dell'offerta</td>
    <td><input name="Orario_off" type="text" class="textbox" id="Orario_off" value="<?php
    //recupera l'ora attuale
    print (date ("H:i:s"));
    ?>"></td>
    </tr>
    <tr>
    <td>Scarica il documento relativo all'asta</td>
    <td>
    <?php

    $Id_utente = $row_visual_asta['Id_referente'];
    $titolo_asta = $row_visual_asta['Id_asta'];
    $doc = $row_visual_asta['Documento'];


    $tipo= "../stage/Documenti_utenti/$Id_utente/$titolo_asta/$doc";
    if ($doc <> "")
    {
    echo "<A HREF=\"Documenti_utenti/$Id_utente/$titolo_asta/$doc\">Scarica il file</A>
    ";
    }
    else
    {
    print "Nessun file da scaricare";
    }


    ?>
    </td>
    </tr>
    <tr>
    <td colspan="2"><div align="center">
    <input type="submit" name="Submit" value="Inserisci l'offerta">
    </div></td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form1">
    </form>



    <?php
    if (isset ($insertSQL))
    {
    print "

    <h5><font color=\"#FF0000\" face=\"Verdana\">Inserimento effettuato correttamente</font></h5></p>";
    }
    ?>
    </p>


    <a href="visualizza_offerte_referente_e_asta.php?Id_r eferen=<?php echo $row_visual_offerente['Id_referente'];?> & Id_ast=<?php echo $row_visual_asta['Id_asta'];?>">Visualizza
    le tue offerte per questa asta</a></p>
    <form name="form2" method="POST" action="<?php echo $editFormAction; ?>">
    <table width="75%" border="1">
    <tr>
    <td>Categoria del prodotto</td>
    <td></td>
    </tr>
    <tr>
    <td>Nome prodotto</td>
    <td>Prezzo</td>
    </tr>
    <?php
    $counter = 0;
    do
    {
    $counter++;
    ?>
    <tr>
    <td>
    <?php VisualNomeProdotto ($row_visual_prod_asta['Id_prodotto']); ?>
    </td>
    <td><input name="Prezzo_prod[]" type="text" id="Prezzo_prod<?php echo $counter; ?>">
    euro </td>
    </tr>
    <?php } while ($row_visual_prod_asta = mysql_fetch_assoc($visual_prod_asta)); ?>
    <tr>
    <td colspan="2"><div align="center">
    <input type="submit" name="Submit2" value="Invia">
    </div></td>
    </tr>
    <tr>
    <td colspan="2"> </tr>
    </table>
    </form>



    </p>
    </td>
    </tr>
    </table>
    </body>
    </html>
    <?php
    mysql_free_result($visual_offerente);

    mysql_free_result($visual_asta);

    mysql_free_result($visual_offerta);

    mysql_free_result($visual_prod_asta);

    mysql_free_result($num_offerte);

    ?>

    ho segato alcune parti però quello che serve dovrebbe esserci,grazie mille
    PHP, ASP, Javascript

  8. #8
    riposto il codice in modo un po piu comprensibile

    Codice PHP:
    <?php require_once('Connections/Connessione.php'); 

    if (isset(
    $HTTP_SERVER_VARS['QUERY_STRING']
    )) { 
    $editFormAction .= "?" $HTTP_SERVER_VARS['QUERY_STRING']; 


    if ((isset(
    $HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) { 
    $insertSQL sprintf("INSERT INTO offerte (Id_asta, Prezzo_offerta, Data_dell_offerta, Orario_offerta, Id_referente_offerta) VALUES (%s, %s, %s, %s, %s)"
    GetSQLValueString($HTTP_POST_VARS['Nome_
    asta'
    ], "int"), 
    GetSQLValueString($HTTP_POST_VARS['Prezz
    o_off'
    ], "int"), 
    GetSQLValueString($HTTP_POST_VARS['Data_
    off'
    ], "date"), 
    GetSQLValueString($HTTP_POST_VARS['Orari
    o_off'
    ], "date"), 
    GetSQLValueString($HTTP_POST_VARS['Id_re
    f'
    ], "int")); 

    mysql_select_db($database_Connessione$Connessione); 
    $Result1 mysql_query($insertSQL$Connessione) or die(mysql_error()); 

    ?> 
    <script language="JavaScript"> 
    <!-- 

    function calcola_tot(Link) 

    newwindow=window.open(Link,"","top=50,left=200,width=550,height=300") 



    //--> 
    </script> 
    <html> 
    <head> 

    <title>Inserisci offerta</title> 
    <META http-equiv="REFRESH" content="30"> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <link href="aste.css" rel="stylesheet" type="text/css"> 
    </head> 

    <body onLoad="startclock(); timerONE=window.setTimeout"> 


     </p> 
    <h3 class="gray">Offerta del Sig.<font color="#FF0000"> 
    <?php VisualCognomeReferente($row_visual_offer
    ente
    ['Id_referente']);?> 
    </font></h3> 
    <p class="gray"><font color="#FF0000"></font></p> 
    <form name="form1" method="POST" action="<?php echo $editFormAction?>"> 
    <table width="75%" border="1" class="tab"> 
    <tr> 
    <td width="34%">Id referente </td> 
    <td width="66%"><input name="Id_ref" type="text" class="textbox" id="Id_ref" value="<?php echo $row_visual_offerente['Id_referente'];?>"> 
    </td> 
    </tr> 
    <tr> 
    <td>Titolo dell'asta</td> 
    <td> <input name="Nome_asta" type="hidden" id="Nome_asta" value="<?php echo $row_visual_asta['Id_asta']; ?>"> 
    <?php VisualNomeAsta ($row_visual_asta['Id_asta']);?> 
    </td> 
    </tr> 
    <tr> 
    <td>Stile dell'asta</td> 
    <td><input name="Stile_asta" type="hidden" id="Stile_asta" value="<?php echo $row_visual_asta['Id_stile_asta']; ?>"> 
    <?php VisualNomeStileAsta ($row_visual_asta['Id_stile_asta']);?> 
    </td> 
    </tr> 
    <tr> 
    <td>Banditore</td> 
    <td><input name="Id_referente" type="hidden" id="Id_referente" value="<?php echo $row_visual_asta['Id_referente']; ?>"> 
    <?php VisualCognomeReferente ($row_visual_asta['Id_referente']);?> 
    </td> 
    </tr> 
    <tr> 
    <td height="26">Tipo di asta</td> 
    <td> 
    <?php 
    if ($row_visual_asta['Rialzo'] == 'Yes'

    echo (
    "Rialzo"); 

    else 

    print (
    "Ribasso"); 

    ?> 
    </td> 
    </tr> 
    <tr> 
    <td height="26">Data e ora di apertura dell'asta</td> 
    <td> 
    <?php ConvDate($row_visual_asta['Data_apertura
    '
    ]); ?> 
      <?php echo $row_visual_asta['Orario_apertura']; ?></td> 
    </tr> 
    <tr> 
    <td height="26">Data e ora di chiusura</td> 
    <td> 
    <?php ConvDate($row_visual_asta['Data_chiusura
    '
    ]); ?> 
      <?php echo $row_visual_asta['Orario_chiusura']; ?> </td> 
    </tr> 
    <tr> 
    <td height="26">Prezzo di partenza dell'asta</td> 
    <td><input name="Prezzo_part" type="text" disabled class="textbox" id="Prezzo_part" value="<?php echo $row_visual_asta['Prezzo_apertura']; ?>"> 
     € </td> 
    </tr> 
    <tr> 
    <td height="26">Prezzo di riserva</td> 
    <td><input name="Prezzo_riserva" type="text" disabled class="textbox" id="Prezzo_riserva" value="<?php echo $row_visual_asta['Prezzo_riserva']; ?>"> 
     € </td> 
    </tr> 
    <tr> 
    <td>Numero delle offerte possibili</td> 
    <td><input name="Num_off_max" type="text" id="Num_off_max" value="<?php echo $row_visual_asta['Num_off_max']; ?>"></td> 
    </tr> 
    <tr> 
    <td>Numero di possibili offerte rimaste </td> 
    <td> <table width="100%" border="0"> 
    <?php 

    if ($row_num_offerte['Id_offerta'] == ""

    $contatore= -1

    else 

    $contatore 0

    do 

    $contatore++; 
    ?> 
    <tr> 
    <td> <input type="hidden" name="Id_off" value="<?php echo $row_num_offerte['Id_offerta']; ?>"> 
    </td> 
    <td><input name="Offerta" type="hidden" id="Offerta" value="<?php echo $contatore?>"> 
    </td> 
    </tr> 
    <?php } while ($row_num_offerte mysql_fetch_assoc($num_offerte)); ?> 
    <?php 
    $calcola_offerte 
    $row_visual_asta['Num_off_max']; 

    //print "$calcola_offerte"; 
    //print "$contatore"; 

    $Offerte_restanti $calcola_offerte $contatore

    if (
    $Offerte_restanti == 0

    echo 
    "<input name=\"off_rimanenti\" type=\"text\" class=\"textbox\" disabled id=\"off_rimanenti\" value=\"0\">"

    else 

    echo 
    "<input name=\"off_rimanenti\" type=\"text\" class=\"textbox\" disabled id=\"off_rimanenti\" value=\"$Offerte_restanti\">"


    ?> 
    </table></td> 
    </tr> 
    <tr> 
    <td height="26">Prezzo offerta</td> 
    <td> <input name="prezzo_off" type="text" id="prezzo_off"> </td> 
    </tr> 
    <tr> 
    <td>Data dell'offerta</td> 
    <td> <input name="Data_off" type="text" class="textbox" id="Data_off" value="<?php 
    //recupera la data attuale 
    print (date ("Y-m-d")); 
    //ConvDate(date("Y-m-d")); 
    //time("H:m:s")
    ?>"></td> 
    </tr> 
    <tr> 
    <td>Orario dell'offerta</td> 
    <td><input name="Orario_off" type="text" class="textbox" id="Orario_off" value="<?php 
    //recupera l'ora attuale 
    print (date ("H:i:s")); 
    ?>"></td> 
    </tr> 
    <tr> 
    <td>Scarica il documento relativo all'asta</td> 
    <td> 
    <?php 

    $Id_utente 
    $row_visual_asta['Id_referente']; 
    $titolo_asta $row_visual_asta['Id_asta']; 
    $doc $row_visual_asta['Documento']; 


    $tipo"../stage/Documenti_utenti/$Id_utente/$titolo_asta/$doc"
    if (
    $doc <> ""

    echo 
    "<A HREF=\"Documenti_utenti/$Id_utente/$titolo_asta/$doc\">Scarica il file</A>
    "


    else 

    print 
    "Nessun file da scaricare"



    ?> 
    </td> 
    </tr> 
    <tr> 
    <td colspan="2"><div align="center"> 
    <input type="submit" name="Submit" value="Inserisci l'offerta"> 
    </div></td> 
    </tr> 
    </table> 
    <input type="hidden" name="MM_insert" value="form1"> 
    </form> 


     
    <?php 
    if (isset ($insertSQL)) 

    print 
    "

    <h5><font color=\"#FF0000\" face=\"Verdana\">Inserimento effettuato correttamente</font></h5></p>"


    ?> 
    </p> 


    <a href="visualizza_offerte_referente_e_asta.php?Id_referen=<?php echo $row_visual_offerente['Id_referente'];?> & Id_ast=<?php echo $row_visual_asta['Id_asta'];?>">Visualizza 
    le tue offerte per questa asta</a></p> 
    <form name="form2" method="POST" action="<?php echo $editFormAction?>"> 
    <table width="75%" border="1"> 
    <tr> 
    <td>Categoria del prodotto</td> 
    <td></td> 
    </tr> 
    <tr> 
    <td>Nome prodotto</td> 
    <td>Prezzo</td> 
    </tr> 
    <?php 
    $counter 
    0
    do 

    $counter++; 
    ?> 
    <tr> 
    <td> 
    <?php VisualNomeProdotto ($row_visual_prod_asta['Id_prodotto']); ?> 
    </td> 
    <td><input name="Prezzo_prod[]" type="text" id="Prezzo_prod<?php echo $counter?>"> 
    euro </td> 
    </tr> 
    <?php } while ($row_visual_prod_asta mysql_fetch_assoc($visual_prod_asta)); ?> 
    <tr> 
    <td colspan="2"><div align="center"> 
    <input type="submit" name="Submit2" value="Invia"> 
    </div></td> 
    </tr> 
    <tr> 
    <td colspan="2"> </tr> 
    </table> 
    </form> 



     </p> 
    </td> 
    </tr> 
    </table> 
    </body> 
    </html> 
    <?php 
    mysql_free_result
    ($visual_offerente); 

    mysql_free_result($visual_asta); 

    mysql_free_result($visual_offerta); 

    mysql_free_result($visual_prod_asta); 

    mysql_free_result($num_offerte); 

    ?>
    Il Ticino è biancoblù
    DVDS delegato ufficiale

  9. #9
    prova a cmabiare
    <td><input name="Prezzo_prod[]" type="text" id="Prezzo_prod<?php echo $counter; ?>">

    con
    <td><input name="Prezzo_prod[<?php echo $counter; ?>]" type="text" id="Prezzo_prod[<?php echo $counter; ?>]">


    al massimo metti ad inizio pagina
    print_r($_POST[Prezzo_prod]);

    e vedi che ti da
    Il Ticino è biancoblù
    DVDS delegato ufficiale

  10. #10
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    196
    ho provato a sostituire e mi dà questo risultato

    se inserisco i valori e clicco sul bottone invia mi dà:

    Notice: Use of undefined constant Prezzo_prod - assumed 'Prezzo_prod' in c:\programmi\easyphp1-7\www\stage\inserisci_offerta2.php on line 48
    Array ( [1] => 545 [2] => 5 )

    come faccio adesso a far comparire il risultato della somma in un campo?
    PHP, ASP, Javascript

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.