Jump to content

Write to File Help


joebgiddings

Recommended Posts

I am trying to complete a project for uni,

This is the code as you can see i am trying to write to a .dat, with the ISBN and title taken from another .txt but i also want to write the customer number to that file aswell (the customer number is input on the previous page (below)), at the moment the ISBn and title are writing but the customer number is not.
Is that bit right? ($content = $cust_num .'|'. $title .'|'. $ISBN .'\end';)
Also is it creating a new entry every time?

Your help will be great, as our tutor has no clue!!!!!

This is the code for the write to file part.

<html>
<head> <title>Buyer </title> </head>
<body>
<h2>You have purchased, Thank You</h2>
<table>   
<?php
    $myFile = "(path)purchases.dat";
    $fh = fopen($myFile, 'a') or die("can't open file\n");
    $total=0;
    if (!($lines = file('items.txt')))
{echo 'ERROR: Unable to open file! </body></html>'; exit;}
    // assumes POST as method - see note below
    foreach ($_POST as $varname => $varvalue) {
        foreach($lines as $theline) {
list($ISBN, $type, $title, $author, $price_inc_vat, $price_ex_vat, $vat, $image, $qty) = split('\|', $theline);   
        if ($ISBN==$varname) {
//print out the line to browser

echo " <tr>
            <td><img src='$image' ></td>
          <td> $author </td> <td> $price_inc_vat GBP </td> <td> $ISBN </td> </tr>";
$total=$total+$price_inc_vat;
//create an entry for append-write to file
        $content = $cust_num .'|'. $title .'|'. $ISBN .'\end';

          if (!(fwrite($fh , $content)))
              { echo "Cannot write to ($myFile)\n</p>";  exit; }
    }
}  }  
// ending the 'foreach' loops
echo "  <tr>    <td> </td>

              <td align='right'> Total:  </td>
    <td> $total GBP  </td> </tr>";




?> 
</table> </body> </html>


This is the code where the customer and chosen books are input.

<html>
<head> <title>Buyer </title> </head>
<body>
<form method="POST" action="purchases1.php">
<h2>Your Shopping Cart</h2>
<table>   
<?php

if (!($lines = file('items.txt')))
{echo 'ERROR: Unable to open file! </body></html>'; exit;}
    // assumes POST as method - see note below
    foreach ($_POST as $varname => $varvalue) {
        foreach($lines as $theline) {
list($ISBN, $type, $title, $author, $price_inc_vat, $price_ex_vat, $vat, $image, $qty) = split('\|', $theline);   
        if ($ISBN==$varname) {
//print out the line to browser

echo " <tr>
            <td><img src='$image' ></td>
          <td> $author </td> <td> $price_inc_vat GBP </td> <td> $ISBN </td> <td> <input type='checkbox' name='$ISBN' CHECKED> </td>  </tr>";
$inctotal=$inctotal+$price_inc_vat;
$extotal=$extotal+$price_ex_vat;
}
}  }  
// ending the 'foreach' loops
echo "  <tr>    <td> </td>
<td align='right'> Total Excluding VAT:  </td>
    <td> $extotal GBP  </td> </tr>;
              <td align='right'> Total Including VAT:  </td>
    <td> $inctotal GBP  </td> </tr>";
?>
<table align="center">
    <tr>

<td>Customer Number<input type="text" name="cust_num"></td>
    <td ><input type="submit" name="Submit" value="Buy"></td>
      <td><input type="reset" name="Reset" value="Reset"></td>
    </tr>
  </table>
</table>
</form>
</body> </html>
Link to comment
https://forums.phpfreaks.com/topic/28503-write-to-file-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.