Aiuto per form

ClouDClauS

Nuovo Utente
14 Ago 2012
8
0
0
Ciao ragazzi sto sviluppando un form che dovrebbe in teoria moltiplicare un campo A per campo B
non sembra difficile, ma il campo A è una cifra che viene aggiornata via CSV, quindi devo eseguire un parser.
Con queste cose sono agli inizi ma vi posto quello che sono riuscito a buttar giu... praticamente un minestrone.
Diciamo che quando vado a provarlo mi triplica il form e non capisco il perchè e non è l'unico errore, perfavore aiutatemi voi :crying:

PHP:
<html>
<head>
<script type="text/javascript">
function processForm() {
//get form variables
var theItems = document.getElementsByName( "item" );
var theQty = Number( document.getElementById( "qty" ).value );
var thePrice = Number( document.getElementById( "price" ).value);

//variable for selected item
var selectedItem;

//variable for order total
var orderTotal = 0.00;

//the display box
var outBox = document.getElementById( "total" );

//set selectedItem = checked item
for(var i = 0; i < theItems.length; i++) {
if( theItems[i].checked) {
selectedItem = theItems[i].value;
break;
}
}

//get price for selected item
switch(selectedItem) {
case "A":
total = (thePrice * 0.916);
break;
case "B":
total = (thePrice * 0.999);
break;
case "C":
total = 0.00;
break;
default:
total = 0.00;
}

//figure final total
total = (selectedItem * theQty);

//output to text box
outBox.value = "€" + total.toString();
}
</script>
</head>
<body>

<?php

$remoteFileAddr = "http://dodygold1.altervista.org/csv/oro.csv";
$content = file_get_contents($remoteFileAddr) or die ("error");
$seperator = ";";

$nPos = strpos($content,"\n");
$firstLine = substr($content, 0, $nPos);
// subtracting first line(column titles) from main content
$content = substr($content, $nPos, strlen($content));
$colNum = substr_count($firstLine, $seperator);

// Exploding First Line for Column titles
$tree = explode($seperator, $firstLine);

// Parsing Data
// Exploding content line by line
$lbl = explode("\n", $content);

$conCount = count($lbl);

echo "\n";
foreach ($lbl as $line) {
$i = 0;
// Exploding content with seperator
$cols = explode($seperator, $line);
foreach ($cols as $col) {

echo <<< FORM
<form id="form1" action="" method="get">
22KT <input type="radio" name="item" value="A" /> 24KT <input type="radio" name="item" value="B" />
<input type="text" id="price" size="20" maxlength="20" readonly="readonly" value="{$col}" />
Peso in Grammi: <input type="text" id="qty" size="10" maxlength="10" value="0" />

Totale: <input type="text" id="total" size="20" maxlength="20" readonly="readonly" value="0.00" />
<input type="button" id="submit" value="Calcola il Totale" onclick="processForm()" />
</form>
FORM;

}
}
?>
</body>
</html>
 
Ciao,
diciamo che ti sei complicato un po la vita leggendo quel file

da una stringa disposta su tre righe hai crato un array di conseguenza l'array ha preso 3 elementi 2 vuoti e uno con il dato che ti serve
ciclandolo e inserendo nel ciclo il form te lo stampa giustamente 3 volte

prova ad usare fopen() piuttosto

PHP:
 <?php
        $remoteFileAddr = "http://dodygold1.altervista.org/csv/oro.csv";
        // apri il file in lettura
        $fr = fopen($remoteFileAddr, 'r');
        // crei un arrary che conterra le righe
        $righe = array();
        // cicli il file
        while (!feof($fr)) {
            // riempi l'array
            $righe[] = fgets($fr);
        }

        // a te interessa il secondo elemento che contiene il numero quindi $righe[1]
        echo <<< FORM
<form id="form1" action="" method="get">
22KT <input type="radio" name="item" value="A" /> 24KT <input type="radio" name="item" value="B" />
<input type="text" id="price" size="20" maxlength="20" readonly="readonly" value="{$righe[1]}" />
Peso in Grammi: <input type="text" id="qty" size="10" maxlength="10" value="0" />

Totale: <input type="text" id="total" size="20" maxlength="20" readonly="readonly" value="0.00" />
<input type="button" id="submit" value="Calcola il Totale" onclick="processForm()" />
</form>
FORM;
        ?>
 
Criric, Scusami ma se invece il file CSV fosse impostato in questo modo : http://95.240.176.113/csv/camp.csv e io dovrei prendere solo il secondo valore omettendo le virgolette come posso fare?

Ciao,
diciamo che ti sei complicato un po la vita leggendo quel file

da una stringa disposta su tre righe hai crato un array di conseguenza l'array ha preso 3 elementi 2 vuoti e uno con il dato che ti serve
ciclandolo e inserendo nel ciclo il form te lo stampa giustamente 3 volte

prova ad usare fopen() piuttosto

PHP:
 <?php
        $remoteFileAddr = "http://dodygold1.altervista.org/csv/oro.csv";
        // apri il file in lettura
        $fr = fopen($remoteFileAddr, 'r');
        // crei un arrary che conterra le righe
        $righe = array();
        // cicli il file
        while (!feof($fr)) {
            // riempi l'array
            $righe[] = fgets($fr);
        }

        // a te interessa il secondo elemento che contiene il numero quindi $righe[1]
        echo <<< FORM
<form id="form1" action="" method="get">
22KT <input type="radio" name="item" value="A" /> 24KT <input type="radio" name="item" value="B" />
<input type="text" id="price" size="20" maxlength="20" readonly="readonly" value="{$righe[1]}" />
Peso in Grammi: <input type="text" id="qty" size="10" maxlength="10" value="0" />

Totale: <input type="text" id="total" size="20" maxlength="20" readonly="readonly" value="0.00" />
<input type="button" id="submit" value="Calcola il Totale" onclick="processForm()" />
</form>
FORM;
        ?>
 
ciao, ho commentato e usato i var_dump() per farti capire come ho fatto, una volta che hai capito puoi toglierli
PHP:
<?php

$remoteFileAddr = "http://95.240.176.113/csv/camp.csv";
// apri il file in lettura
$fr = fopen($remoteFileAddr, 'r');
// crei un arrary che conterra le righe
$righe = array();
// cicli il file
while (!feof($fr)) {
    
    // riempi l'array
    $righe[] = fgets($fr);
}
// vediamo cosa c'è dentro
var_dump($righe);
// creiamo un array con la riga che ci interessa 
$array_dati = explode(",", $righe[2]);
// vediamo cosa c'è dentro
var_dump($array_dati);
// gli elementi che ci interessano sono 1 e 2
// uniamoli con il punto cosi è piu semplice fare calcoli o se vuoi la virgola metti la virgola
$valore = $array_dati[1] . "." . $array_dati[2];
// ripuliamolo degli apici doppi
$valore = str_replace("\"", "", $valore);
// vediamo cosa ne è uscito
var_dump($valore);

// direi che si puo usare nel form
echo <<< FORM
<form id="form1" action="" method="get">
22KT <input type="radio" name="item" value="A" /> 24KT <input type="radio" name="item" value="B" />
<input type="text" id="price" size="20" maxlength="20" readonly="readonly" value="{$valore}" />
Peso in Grammi: <input type="text" id="qty" size="10" maxlength="10" value="0" />

Totale: <input type="text" id="total" size="20" maxlength="20" readonly="readonly" value="0.00" />
<input type="button" id="submit" value="Calcola il Totale" onclick="processForm()" />
</form>
FORM;
?>
?>
 
Sei semplicemente mitico! Ultima cosa e ho completato la mia task! Devo far si che il totale mi appaia con due decimali arrotondando per difetto o per eccesso... è possibile fare una cosa del genere?
Cmq mi hai aperto un mondo con pochi semplici suggerimenti non saprò mai come ringraziarti!

ciao, ho commentato e usato i var_dump() per farti capire come ho fatto, una volta che hai capito puoi toglierli
PHP:
<?php

$remoteFileAddr = "http://95.240.176.113/csv/camp.csv";
// apri il file in lettura
$fr = fopen($remoteFileAddr, 'r');
// crei un arrary che conterra le righe
$righe = array();
// cicli il file
while (!feof($fr)) {
    
    // riempi l'array
    $righe[] = fgets($fr);
}
// vediamo cosa c'è dentro
var_dump($righe);
// creiamo un array con la riga che ci interessa 
$array_dati = explode(",", $righe[2]);
// vediamo cosa c'è dentro
var_dump($array_dati);
// gli elementi che ci interessano sono 1 e 2
// uniamoli con il punto cosi è piu semplice fare calcoli o se vuoi la virgola metti la virgola
$valore = $array_dati[1] . "." . $array_dati[2];
// ripuliamolo degli apici doppi
$valore = str_replace("\"", "", $valore);
// vediamo cosa ne è uscito
var_dump($valore);

// direi che si puo usare nel form
echo <<< FORM
<form id="form1" action="" method="get">
22KT <input type="radio" name="item" value="A" /> 24KT <input type="radio" name="item" value="B" />
<input type="text" id="price" size="20" maxlength="20" readonly="readonly" value="{$valore}" />
Peso in Grammi: <input type="text" id="qty" size="10" maxlength="10" value="0" />

Totale: <input type="text" id="total" size="20" maxlength="20" readonly="readonly" value="0.00" />
<input type="button" id="submit" value="Calcola il Totale" onclick="processForm()" />
</form>
FORM;
?>
?>
 

Discussioni simili