Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

PHP replacing new value in a text file

Links for php scripts

Moderators: macek, egami, gesf

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?
Thanks

Code: Select all

<html>
  <body>
 
    <?php

// 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 +
                     $Bananas_cost;
      $total_items = $Apples + $Oranges + $Bananas;

// Return the results to the browser in a table

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

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


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

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

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


fclose($handle);


$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);

 fclose($Handle);
 ?>

  </body>
</html>


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

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?
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.