3
I’m making a form where I basically access a file .txt
in the CSV format containing certain articles and their prices (e.g., cheese,2.10).
The program presents the list in a table, and below appear two fields that ask for "Article" and "Quantity", now what is the best way to make the program look for the article in the file, and tell if it exists or not, if the product exists, show the total multiplication of the quantity with the value of the product, which is in the second column of the file .txt
.
For example, Product: Cheese, Quantity: 10, present 10x the price of total cheese below.
PS: Forms with the "Article" and "Quantity" fields disappear after being filled in.
So far my code is like this:
<?php
$br="</br>";
$file=fopen("produtos.txt","r");
echo "<table border='1'><tr><th>Artigos</th><th>Preco</th>";
while(!feof($file)) {
$registo=fgetcsv($file);
echo"<tr>";
echo"<td>".$registo[0]."</td>";
echo"<td>".$registo[1]."</td>";
echo"</tr>";
}
echo "</table>";
fclose($file);
?>
<?php
if ($_POST) {
echo "Produto: ".$_POST["produto"]."<br>";
echo "Quantidade ".$_POST["quantidade"]."<br>";
}
else {
?>
<form action="eta15.php" method="post">
Produto <input type="text" name="produto"><br>
Quantidade <input type="text" name="quantidade"><br>
<input type="Submit" name="enviar" value="Enviar">
</form>
<?php
}
?>
I leave two printscreens, I hope they help in my question of the result in which I am:
Search form:
Result form:
This is the right answer to the question that has been asked, it has a clean and professional code! Thanks for your time and help!
– Mario Caeiro
In this case, if I wanted the table to disappear after Submit, where I had to put it ?
– Mario Caeiro
@Mariocaeiro inside an if, so:
if( ! $_POST ){ //tabela }
– Jorge B.
I didn’t notice, I have the code like this: http://pastebin.com/k32byUWD ?
– Mario Caeiro
@Mariocaeiro put the code that foils the table within a condition, if it was not done POST then print:
if ( ! isset( $_POST ) ) { echo "<table border='1'> ........ echo "</table>"; }
– Jorge B.
I tried that, but when it presents values it ceases to calculate and accuses that there is no product, but the table disappears
– Mario Caeiro
@Mariocaeiro I forgot that you have there the research, it will be complicated thus...
– Jorge B.