Jump to content

Appending variables to txt file


bambinou1980

Recommended Posts

Hello,

 

I am having a little problem and do not know where to start.

 

I have a printer that requires the following type of data to be written to a txt file:

 

#
Delivery;13:30;18-08-15;*               //Date of the order
121345*                                         //Order number
2;Chicken Tikka Massala;13.90;*  //Food item
5.00*                                             //Delivery Fees
18.90*                                           //Total Price                                 
Freddy Crugger;Street 154 beach house palier, town1;555444;Hi, Could you please call me when you are near the phone box after frankie's club;*//Customer Address
7;Cash on Delivery;                     //Type of delivery & Payment type
#

 

As you can see, each variable is delimited by an asterix *.

The whole order is delimited by #

The ; means that this fits on one line.

 

 

Now here is my problem.

 

Let's say I have this:

$date_of_the_order
$order_number
$food_item
$delivery_fees
$total_price  
$customer_address
$type-of-delivery
$payment_type

 

I have already the form, the form data from my $_POST is going into those variables, how can I add my variables in a way that it always creates the output in my txt file as:

 

#
Delivery;13:30;18-08-15;*               //Date of the order
121345*                                         //Order number
2;Chicken Tikka Massala;13.90;*  //Food item
5.00*                                             //Delivery Fees
18.90*                                           //Total Price                                 
Freddy Crugger;Street 154 beach house palier, town1;555444;Hi, Could you please call me when you are near the phone box after frankie's club;*//Customer Address
7;Cash on Delivery;                     //Type of delivery & Payment type
#

 

 

Could you please give me a quick example?

 

I looked for this:

<?php
$content = "foo";
file_put_contents("text_file.txt", $ip, FILE_APPEND);
?>

 

Would the above function works, if yes, how to add multiple variables to it?

 

Thank you so much!

 

Ben

 

 

 

 

Link to comment
Share on other sites

Hi,

 

I am adding the whole code, I have not yet added any validations...but I cannot get anything passed in the var_dump, I must have a tiny error somewhere but cannot work it out..

At the moment I am just trying to get a print test of the printer, this is why I added some values manually to some of the variables.

 

 

<?phpif(isset($_POST['submit'])){      $product1 = $_POST['product1'];    //$product2 = $_POST['product2'];    //$product3 = $_POST['product3'];    //$product4 = $_POST['product4'];           $quantity1 = $_POST['quantity1'];    // $quantity2 = $_POST['quantity2'];    // $quantity3 = $_POST['quantity3'];    // $quantity4 = $_POST['quantity4'];          $price1 = $_POST['price1'];    //$price2 = $_POST['price2'];    //$price3 = $_POST['price3'];    // $price4 = $_POST['price4'];  /*# Delivery;13:30;18-08-15;*                       //Date of the order 121345*                                         //Order number 2;Chicken Tikka Massala;13.90;*  //Food item 5.00*                                           //Delivery Fees 18.90*                                          //Total Price                                  Freddy Crugger;Street 154 beach house palier, town1;555444;Hi, Could you please call me when you are near the phone box after frankie's club; //Customer Address 7;Cash on Delivery;                             //Type of delivery & Payment type #  */ $item1 = $quantity1 . ";" . $product1 . ";" . $price1 . ";";//$item2 = $quantity2 . ";" . $product2 . ";" . $price2 . ";";//$item3 = $quantity3 . ";" . $product3 . ";" . $price3 . ";";//$item4 = $quantity4 . ";" . $product4 . ";" . $price4 . ";"; $start_of_order    = "#";$type_of_order     = "Delivery";$time              = "13:30;";$date              = "18-08-15;";$order_number      = "121345*";$total_price       = "18.90*";$delivery_fees     = "5.00*";$Customer_surname  = "Freddy;";$Customer_name     = "Crugger;";$customer_address  = "Street 154 beach house palier, town1;";$customer_phone    = "88585898;";$comments          = "Hi, Could you please call me when you are near the phone box after frankie's club;";$type_of_delivery  = "7"; //Order not Paid. Use 6 for Paid$payment_type      = "Cash on Delivery";$end_of_order      = "#";   file_put_contents("order_test.txt", $start_of_order .$type_of_order .$time .$date .$order_number .$item1 .$total_price .$delivery_fees .$Customer_surname .$Customer_name .$customer_address .$customer_phone .$comments .$type_of_delivery .$payment_type .$end_of_order . "", FILE_APPEND);  var_dump($_POST);} ?><!DOCTYPE HTML><html><head><meta http-equiv="content-type" content="text/html" /><meta name="author" content="lolkittens" /><!-- Latest compiled and minified CSS --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> <!-- Optional theme --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" /> <!-- Latest compiled and minified JavaScript --><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script><title>Test Form</title></head> <body><br /><br /><br /><br /><div class="row"><div class="col-md-2"></div> <div class="col-md-8"><form action="test.php" method="post" enctype="text/plain"> <div class="input-group">  <span class="input-group-addon" id="basic-addon1">Product 1</span>  <input type="text" class="form-control" value="Tal Fenek Pizza" name="product1"/>    <span class="input-group-addon" id="basic-addon1">Quantity 1</span>  <input type="text" class="form-control" value="2" name="quantity1"/>      <span class="input-group-addon" id="basic-addon1">Price 1</span>  <input type="text" class="form-control" value="25.60" name="price1" /></div>  <br /> <div class="input-group">  <span class="input-group-addon" id="basic-addon1">Product 2</span>  <input type="text" class="form-control" value="Cappriciosa Pizza" name="product2"/>    <span class="input-group-addon" id="basic-addon1">Quantity 2</span>  <input type="text" class="form-control" value="3" name="quantity2"/>      <span class="input-group-addon" id="basic-addon1">Price 2</span>  <input type="text" class="form-control" value="35.60" name="price2" /></div> <br /><div class="input-group">  <span class="input-group-addon" id="basic-addon1">Product 3</span>  <input type="text" class="form-control" value="Quatro Formaggi Pizza" name="product3"/>    <span class="input-group-addon" id="basic-addon1">Quantity 3</span>  <input type="text" class="form-control" value="5" name="quantity3"/>      <span class="input-group-addon" id="basic-addon1">Price 3</span>  <input type="text" class="form-control" value="45.60" name="price3" /></div> <br /> <div class="input-group">  <span class="input-group-addon" id="basic-addon1">Product 4</span>  <input type="text" class="form-control" value="Chicken Tikka Massala" name="product4"/>    <span class="input-group-addon" id="basic-addon1">Quantity 4</span>  <input type="text" class="form-control" value="6" name="quantity4"/>      <span class="input-group-addon" id="basic-addon1">Price 4</span>  <input type="text" class="form-control" value="65.60" name="price4" /></div><br /><input type="submit" class="btn btn-primary btn-block" /> </form></div><div class="col-md-2"></div></div>  </body></html>

Link to comment
Share on other sites

Is PHP set to show all errors and warnings? To make sure, you can add the following to the top of your script during the debugging process:


<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

 

Also note that file_put_contents() "returns the number of bytes that were written to the file, or FALSE on failure." Have you checked to see what the function returns?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.