Siccome ho girato e sto girando tuttora su google in cerca di un'illuminazione ma non sono riuscito a trovare assolutamente niente che mi risolva il problema metto anche direttamente lo script da me fatto che magari può essere d'aiuto.
Oltre ai campi coi prodotti presi file txt ho aggiunto 3 campi vuoti per ogni sezione e 5 alla fine di questi in modo da poter permettere l'aggiunta di altri prodotti non presenti in lista.
form.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento senza titolo</title>
</head>
<body>
<?php
$id=0;
$z=0;
echo "<form method=\"POST\" action=\"send.php\">\n"; /*inizializza il form*/
if (!$fp= fopen("list-food-drink.txt","r")) {
echo "Spiacente, ci sono problemi con il caricamento del file.";
} else {
while($linea!="-------")
{
$linea = fgets($fp, 255);
echo "<fieldset><legend>$linea</legend>"; /*Apre il fieldset*/
while($linea!="\r\n"&&$linea!="-------")
{
$linea = fgets ($fp, 255);
if ($linea!="\r\n"&&$linea!="-------") {
$linea = str_replace(array("\r\n", "\r", "\n"), "", $linea);
echo "<label for=\"$linea\">${linea}</label><input name=\"$linea\" type=\"text\" id=\"$linea\" size=\"4\" maxlength=\"3\" value=\"0\" onFocus=\"if (this.value=='0') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='0'}\">\n<input name=\"$id\" type=\"text\" id=\"$id\" size=\"7\" maxlength=\"10\" value=\"\"><br>\n";$id++;}
else {
for ($x=0;$x<3;$x++) {
$emptyrow_name="n".$z;
$emptyrow_quantity="q".$z;
$emptyrow_unit="u".$z;
echo "<input name=\"$emptyrow_name\" type=\"text\" id=\"$emptyrow_name\" size=\"25\" maxlength=\"25\" value=\"Additional product\" onFocus=\"if (this.value=='Additional product') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='Additional product'}\"><input name=\"$emptyrow_quantity\" type=\"text\" id=\"$emptyrow_quantity\" size=\"7\" maxlength=\"10\" value=\"Quantity\" onFocus=\"if (this.value=='Quantity') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='Quantity'}\"><input name=\"$emptyrow_unit\" type=\"text\" id=\"$emptyrow_unit\" size=\"7\" maxlength=\"10\" value=\"Unit\" onFocus=\"if (this.value=='Unit') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='Unit'}\"><br>\n";
$z++;}
echo "</fieldset>";
}
}
}
fclose($fp);
}
echo "<fieldset><legend>Additional products</legend>";
for ($x=0;$x<5;$x++) {$emptyrow_name="n".$z;
$emptyrow_quantity="q".$z;
$emptyrow_unit="u".$z;
echo "<input name=\"$emptyrow_name\" type=\"text\" id=\"$emptyrow_name\" size=\"25\" maxlength=\"25\" value=\"Additional product\"onFocus=\"if (this.value=='Additional product') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='Additional product'}\"><input name=\"$emptyrow_quantity\" type=\"text\" id=\"$emptyrow_quantity\" size=\"7\" maxlength=\"10\" value=\"Quantity\"onFocus=\"if (this.value=='Quantity') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='Quantity'}\"><input name=\"$emptyrow_unit\" type=\"text\" id=\"$emptyrow_unit\" size=\"7\" maxlength=\"10\" value=\"Unit\"onFocus=\"if (this.value=='Unit') {this.value=''}\" onBlur=\"if ((this.value=='') || (this.value==NULL)) {this.value='Unit'}\"><br>\n";
$z++;}
?>
</fieldset>
<fieldset><legend>Invia richiesta</legend><input name="submit" type="submit" value="Invia" /></fieldset>
</body>
</html>
send.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento senza titolo</title>
</head>
<body>
<?php
$i=0;
$z=0;
$id=0;
$vector = array();
if (!$fp= fopen("list-food-drink.txt","r")) {
echo "Spiacente, ci sono problemi con il caricamento del file.";
} else {
while($linea!="-------")
{
$linea = fgets ($fp, 255);
while($linea!="\r\n"&&$linea!="-------")
{
$linea=fgets ($fp, 255);
if ($linea!="\r\n"&&$linea!="-------") {
$linea = str_replace(array("\r\n", "\r", "\n"), "", $linea);
$quant=$_POST['$linea'];
$altq=$_POST['$id'];$id++;
if ($quant > 0) {$vector*=array("name"=>"$linea","q"=>"$quant","id"=>"$altq");$i++;}
}
}
for ($x=0;$x<3;$x++)
{
$emptyrow_name=$_POST["n".$z];
$emptyrow_quantity=$_POST["q".$z];
$emptyrow_unit=$_POST["u".$z];
$z++;
if ($emptyrow_name!="Additional product")
{
$vector*=array("name"=>"$emptyrow_name","q"=>"$emptyrow_quantity","id"=>"$emptyrow_unit");
$i++;
}
}
}
fclose ($fp);
}
for ($x=0;$x<5;$x++)
{
$emptyrow_name=$_POST["n".$z];
$emptyrow_quantity=$_POST["q".$z];
$emptyrow_unit=$_POST["u".$z];
$z++;
if ($emptyrow_name!="Additional product")
{
$vector*=array("name"=>"$emptyrow_name","q"=>"$emptyrow_quantity","id"=>"$emptyrow_unit");
$i++;
}
}
for ($ii=0; $ii<$i; $ii++)
{
$a=$ii+1;
$message.=$a."\t".$vector[$ii]["name"]."\t".$vector[$ii]["q"]."\t".$vector[$ii]["id"]."\r\n";
}
print_r($vector);
?>
</body>
</html>
struttura file txt:
categoria1
prodotto1
prodotto2
categoria2
prod1
prod2