PHP replacing new value in a text file

Links for php scripts

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Sat Apr 06, 2013 8:49 am

PHP replacing new value in a text file

Postby Trendnet18 » Sat Apr 06, 2013 8:58 am

I have only put the PHP code I dont think HTML is needed as its quite a standard form.
I am kind stuck here. What i need to is everytime a user enters the no of oranges, apples and bananas this value be added on to what ever value there is in the text file currently.

in the text file if the user submits for the first time it will print out in the example below:

The total Number of Apples: 10
The total Number of Bananas: 10
The total Number of Oranges: 10

subsequent submission it will add on to the new value entered and it will replace the value 10 in this case.

Can someone assist in solving this problem?

Code: Select all


// Get form data values

      $Apples = $_POST["Apples"];
      $Oranges = $_POST["Oranges"];
      $Bananas = $_POST["Bananas"];
      $username = $_POST["username"];
      $payment = $_POST["payment"];

// Compute the item costs and total cost

      $Apples_cost = 0.69 * $Apples;
      $Oranges_cost = 0.59 * $Oranges;
      $Bananas_cost = 0.39 * $Bananas;

      $total_price = $Apples_cost + $Oranges_cost +
      $total_items = $Apples + $Oranges + $Bananas;

// Return the results to the browser in a table

    <h4> Customer:
      print ("$username");

    <table border = "border">
      <caption> Order Information </caption>
        <th> Product </th>
        <th> Unit Price </th>
        <th> Quantity Ordered </th>
        <th> Item Cost </th>
      <tr align = "center">
        <td> Apples </td>
        <td> $0.69 </td>
        <td> <?php print ("$Apples"); ?> </td>
        <td> <?php printf ("$ %4.2f", $Apples_cost); ?>
      <tr align = "center">
        <td> Oranges </td>
        <td> $0.59 </td>
        <td> <?php print ("$Oranges"); ?> </td>
        <td> <?php printf ("$ %4.2f", $Oranges_cost); ?>
      <tr align = "center">
        <td> Bananas </td>
        <td> $0.39 </td>
        <td> <?php print ("$Bananas"); ?> </td>
        <td> <?php printf ("$ %4.2f", $Bananas_cost); ?>
         <td> Total </td>
         <td> NA </td>
         <td><?php printf ("Quantity:  $total_items <br />"); ?>  </td>
         <td><?php printf ("Cost: $ %5.2f <br />", $total_price); ?></td>

<!---- update textfile on the server after a new order has taken place--->

$filename = "order.txt";
$handle = fopen($filename, "r");

$Data = fread($handle, 512);
echo $Data;
$pattern = "?P<digit>\d+";


$Handle = fopen($File, 'w');
fwrite($Handle, "The total Number of Apples: ".$Apples.PHP_EOL);
fwrite($Handle, "The total Number of Bananas: ".$Bananas.PHP_EOL);
fwrite($Handle, "The total Number of Oranges: ".$Oranges.PHP_EOL);



php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: PHP replacing new value in a text file

Postby seandisanti » Fri Apr 12, 2013 7:48 pm

so what's it doing now that's different from what you want it to be doing?

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests