Jump to content

Writing to files?


freya-san

Recommended Posts

Hello,

I am making a simple flat-file shopping cart system, and have it more or less completed, the problem is that the customer can only purchase one item because I can only get the code to write one item code to my purchases file. I am wondering if there is a way a loop could be used to write all the item codes (these codes are placed in multiple hidden fields with the same name="$code").

Here is the code I am trying to use to write the file:

<?php
$myFile = "purchases.txt";
$fh = fopen($myFile, 'a') or die("can't open file\n");
if(is_readable($myFile)) { echo "File is readable </p>";}
else { echo "File is Not readable</p>"; }
if( is_writable($myFile)) {echo "File is writable </p>";}
else { echo "File is Not writable</p>";}

$code=$_POST['itemcode'];
$description=$_POST['description'];
$price=$_POST['price'];
$image=$_POST['image'];

$content =$code."|".$description."|". $price."|".$image."\n";

if(!(fwrite($fh , $content)))
{  echo "Cannot write to file ($myFile)\n</p>";  exit; }
else {  echo "<p>Data written</p>
          <a href='write_ffdb1.htm' >Return to Input Form</a>";
        fclose($fh); }
?>

Any suggestions would be greatly appreciated!

- Freya.
Link to comment
Share on other sites

[quote]these codes are placed in multiple hidden fields with the same name="$code"[/quote]
If they all have the same name, then you're only getting the last value returned to your script. You need to make the name an array, then you can loop through the array values and write each to your file.

Ken
Link to comment
Share on other sites

The input is spanned across two pages:

The customer selects their items from a form, which is generated by reading from the 'items.txt', the next page is then generated: buyer.php (this contains the hidden fields $code), the form then points to confirm.php (which contains the code I posted earlier.

Which page's code would you like me to post to you?

- Freya.
Link to comment
Share on other sites

Okay, here's the buyer code:

[code]
<html>
<head> <title>Please Confirm Your Order</title> </head>
<body>

<h2>Your Order</h2>

<form method="post" action="confirm.php">

Title:<select name="title">
<option value="Mr. ">Mr.</option>
<option value="Mrs. ">Miss.</option>
<option value="Miss. ">Mrs.</option>
<option value="Ms. ">Ms.</option>
</select>
</br>
First Name:<input type="text" name="firstname">
</br>
Surname:<input type="text" name="surname">
</br>
Street:<input type="text" name="street">
</br>
Town:<input type="text" name="town">
</br>
City:<input type="text" name="city">
</br>
Post/ZIP code:<input type="text" name="postcode">
</br>
<!-- This drop-down box has been taken from: http://javascript.about.com/library/blcountry.htm --!>
Country:<select name="country">
<option>Afghanistan</option>
<option>&Aring;land Islands</option>
<option>Albania</option>
<option>Algeria</option>
<option>American Samoa</option>
<option>Andorra</option>
<option>Angola</option>
<option>Anguilla</option>
<option>Antarctica</option>
<option>Antigua and Barbuda</option>
<option>Argentina</option>
<option>Armenia</option>
<option>Aruba</option>
<option>Australia</option>
<option>Austria</option>
<option>Azerbaijan</option>
<option>Bahamas</option>
<option>Bahrain</option>
<option>Bangladesh</option>
<option>Barbados</option>
<option>Belarus</option>
<option>Belgium</option>
<option>Belize</option>
<option>Benin</option>
<option>Bermuda</option>
<option>Bhutan</option>
<option>Bolivia</option>
<option>Bosnia and Herzegovina</option>
<option>Botswana</option>
<option>Bouvet Island</option>
<option>Brazil</option>
<option>British Indian Ocean territory</option>
<option>Brunei Darussalam</option>
<option>Bulgaria</option>
<option>Burkina Faso</option>
<option>Burundi</option>
<option>Cambodia</option>
<option>Cameroon</option>
<option>Canada</option>
<option>Cape Verde</option>
<option>Cayman Islands</option>
<option>Central African Republic</option>
<option>Chad</option>
<option>Chile</option>
<option>China</option>
<option>Christmas Island</option>
<option>Cocos (Keeling) Islands</option>
<option>Colombia</option>
<option>Comoros</option>
<option>Congo</option>
<option>Congo, Democratic Republic</option>
<option>Cook Islands</option>
<option>Costa Rica</option>
<option>C&ocirc;te d'Ivoire (Ivory Coast)</option>
<option>Croatia (Hrvatska)</option>
<option>Cuba</option>
<option>Cyprus</option>
<option>Czech Republic</option>
<option>Denmark</option>
<option>Djibouti</option>
<option>Dominica</option>
<option>Dominican Republic</option>
<option>East Timor</option>
<option>Ecuador</option>
<option>Egypt</option>
<option>El Salvador</option>
<option>Equatorial Guinea</option>
<option>Eritrea</option>
<option>Estonia</option>
<option>Ethiopia</option>
<option>Falkland Islands</option>
<option>Faroe Islands</option>
<option>Fiji</option>
<option>Finland</option>
<option>France</option>
<option>French Guiana</option>
<option>French Polynesia</option>
<option>French Southern Territories</option>
<option>Gabon</option>
<option>Gambia</option>
<option>Georgia</option>
<option>Germany</option>
<option>Ghana</option>
<option>Gibraltar</option>
<option>Greece</option>
<option>Greenland</option>
<option>Grenada</option>
<option>Guadeloupe</option>
<option>Guam</option>
<option>Guatemala</option>
<option>Guinea</option>
<option>Guinea-Bissau</option>
<option>Guyana</option>
<option>Haiti</option>
<option>Heard and McDonald Islands</option>
<option>Honduras</option>
<option>Hong Kong</option>
<option>Hungary</option>
<option>Iceland</option>
<option>India</option>
<option>Indonesia</option>
<option>Iran</option>
<option>Iraq</option>
<option>Ireland</option>
<option>Israel</option>
<option>Italy</option>
<option>Jamaica</option>
<option>Japan</option>
<option>Jordan</option>
<option>Kazakhstan</option>
<option>Kenya</option>
<option>Kiribati</option>
<option>Korea (north)</option>
<option>Korea (south)</option>
<option>Kuwait</option>
<option>Kyrgyzstan</option>
<option>Lao People's Democratic Republic</option>
<option>Latvia</option>
<option>Lebanon</option>
<option>Lesotho</option>
<option>Liberia</option>
<option>Libyan Arab Jamahiriya</option>
<option>Liechtenstein</option>
<option>Lithuania</option>
<option>Luxembourg</option>
<option>Macao</option>
<option>Macedonia</option>
<option>Madagascar</option>
<option>Malawi</option>
<option>Malaysia</option>
<option>Maldives</option>
<option>Mali</option>
<option>Malta</option>
<option>Marshall Islands</option>
<option>Martinique</option>
<option>Mauritania</option>
<option>Mauritius</option>
<option>Mayotte</option>
<option>Mexico</option>
<option>Micronesia</option>
<option>Moldova</option>
<option>Monaco</option>
<option>Mongolia</option>
<option>Montserrat</option>
<option>Morocco</option>
<option>Mozambique</option>
<option>Myanmar</option>
<option>Namibia</option>
<option>Nauru</option>
<option>Nepal</option>
<option>Netherlands</option>
<option>Netherlands Antilles</option>
<option>New Caledonia</option>
<option>New Zealand</option>
<option>Nicaragua</option>
<option>Niger</option>
<option>Nigeria</option>
<option>Niue</option>
<option>Norfolk Island</option>
<option>Northern Mariana Islands</option>
<option>Norway</option>
<option>Oman</option>
<option>Pakistan</option>
<option>Palau</option>
<option>Palestinian Territories</option>
<option>Panama</option>
<option>Papua New Guinea</option>
<option>Paraguay</option>
<option>Peru</option>
<option>Philippines</option>
<option>Pitcairn</option>
<option>Poland</option>
<option>Portugal</option>
<option>Puerto Rico</option>
<option>Qatar</option>
<option>R&eacute;union</option>
<option>Romania</option>
<option>Russian Federation</option>
<option>Rwanda</option>
<option>Saint Helena</option>
<option>Saint Kitts and Nevis</option>
<option>Saint Lucia</option>
<option>Saint Pierre and Miquelon</option>
<option>Saint Vincent and the Grenadines</option>
<option>Samoa</option>
<option>San Marino</option>
<option>Sao Tome and Principe</option>
<option>Saudi Arabia</option>
<option>Senegal</option>
<option>Serbia and Montenegro</option>
<option>Seychelles</option>
<option>Sierra Leone</option>
<option>Singapore</option>
<option>Slovakia</option>
<option>Slovenia</option>
<option>Solomon Islands</option>
<option>Somalia</option>
<option>South Africa</option>
<option>South Georgia and the South Sandwich Islands</option>
<option>Spain</option>
<option>Sri Lanka</option>
<option>Sudan</option>
<option>Suriname</option>
<option>Svalbard and Jan Mayen Islands</option>
<option>Swaziland</option>
<option>Sweden</option>
<option>Switzerland</option>
<option>Syria</option>
<option>Taiwan</option>
<option>Tajikistan</option>
<option>Tanzania</option>
<option>Thailand</option>
<option>Togo</option>
<option>Tokelau</option>
<option>Tonga</option>
<option>Trinidad and Tobago</option>
<option>Tunisia</option>
<option>Turkey</option>
<option>Turkmenistan</option>
<option>Turks and Caicos Islands</option>
<option>Tuvalu</option>
<option>Uganda</option>
<option>Ukraine</option>
<option>United Arab Emirates</option>
<option>United Kingdom</option>
<option selected="selected">United States of America</option>
<option>Uruguay</option>
<option>Uzbekistan</option>
<option>Vanuatu</option>
<option>Vatican City</option>
<option>Venezuela</option>
<option>Vietnam</option>
<option>Virgin Islands (British)</option>
<option>Virgin Islands (US)</option>
<option>Wallis and Futuna Islands</option>
<option>Western Sahara</option>
<option>Yemen</option>
<option>Zaire</option>
<option>Zambia</option>
<option>Zimbabwe</option>
</select>

<?php
$myFile = "purchases.txt";
$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($code, $format, $type, $description, $manufact, $price, $image) = split('\|', $theline);
    if ($code==$varname)
  {
    //print out the line to browser
    echo "
<table border='1'>
<tr>
<td> $custname </td>
</tr>
<tr>
      <td rowspan='2'><img src='$image' width='70' height='100'></td>
      <td> $description </td> </br>
</tr>
<tr>
<td> £$price </td> <td> $code </td>
<td> <input type='hidden' name='itemcode' value='$code'> </td>
    </tr>";
    $total=$total+$price;
    }
  }
} // ending the 'foreach' loops

$percentage = (17.15/100) * $total;

$novat = $total - $percentage;

$total2 = round($novat, 2);

echo "

<table border='0'>
<tr>
<td> &nbsp; </td>
<td> &nbsp; </td>
</tr>
<tr>
<td> Total: </td>
<td> £$total2 </td>
</tr>
<tr>
<td> Total (inc. VAT @ 17.5%): </td>
<td> <b> £$total </b> </td>
</tr>
</table>";

?>

<input type="submit" name="Submit" value="Submit">
</table>
</br>
<a href="purchase.php">Return to Purchases</a> </br>
<a href="home.php">Return to Home</a> </br>
</form>

</body>
</html>
[/code]

Thanks.
Link to comment
Share on other sites

Change this line
[code]<?php
<td> <input type='hidden' name='itemcode' value='$code'> </td>
?>[/code]

to
[code]<?php
<td> <input type='hidden' name='itemcode[]' value='$code'> </td>
?>[/code]

I didn't see any lines in your form for inputting the price or description.

And change your processing script to something like:
[code]<?php
$content = array();
for ($i=0;$i<count($_POST['itemcode']);$i++) {
    $code=$_POST['itemcode'][$i];
    $description=$_POST['description'];
    $price=$_POST['price'];
    $image=$_POST['image'];

    $content[] =$code."|".$description."|". $price."|".$image; //save each record to be written
}

if(!(fwrite($fh , implode("\n",$content)))) // write all the saved records
{  echo "Cannot write to file ($myFile)\n</p>";  exit; }
else {  echo "<p>Data written</p>
?>[/code]

Ken
Link to comment
Share on other sites

I did manage to get it to work, but that was when I removed the last 'else' statement. I am still getting the $end error, though. So, I'll post the code; it's probably something really simple.

[code]
<?php
$myFile = "php_ffdb.txt";
$fh = fopen($myFile, 'a') or die("can't open file\n");
if(is_readable($myFile)) { echo "File is readable </p>";}
else { echo "File is Not readable</p>"; }
if( is_writable($myFile)) {echo "File is writable </p>";}
else { echo "File is Not writable</p>";}

$content = array();
for ($i=0;$i<count($_POST['itemcode']);$i++)
{
    $code=$_POST['itemcode'][$i];
    $description=$_POST['description'];
    $price=$_POST['price'];
    $image=$_POST['image'];

    $content[] =$code."|".$description."|". $price."|".$image; //save each record to be written
}

if(!(fwrite($fh , implode("\n",$content)))) // write all the saved records

echo "Cannot write to file ($myFile)\n</p>";  exit;
}
else

echo "<p>Data written</p>"
}
?>
[/code]
Link to comment
Share on other sites

If you use the other technique

if (whatever) {

}
then you can always associate the { with the control structure very easily, there are 3 different named styles to the placement of the bracket, this is the one that was recommended to be the easiest to debug, then all you have to do is match the number of closing one's with the number that you can obviously count next to the control structures.

also by what I have heard creating a shopping cart system to a flat file isn't safe, but that's just what I heard, I could be wrong.
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.