freya-san Posted November 24, 2006 Share Posted November 24, 2006 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. Quote Link to comment Share on other sites More sharing options...
taith Posted November 24, 2006 Share Posted November 24, 2006 wouldnt it be much easier with a cookie? or a session?[code]setcookie("cookie[two]", "cookietwo");setcookie("cookie[one]", "cookieone");[/code]then have it write at the end? Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 I am unsure. The problem is, the specification I have been given asks me to do it using a write. Thanks for your suggestion, though! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 [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 Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 That makes sense. Could you give me some pointers on how to do that, please? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 Please post the source for your input form.Ken Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 It sounds like the buyer.php script.Ken Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 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>Å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ô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é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 belowforeach ($_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> </td> <td> </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. Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 *bump* Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 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 Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 Using that code brings up this error:Parse error: parse error, unexpected $end in C:\Program Files\xampp\htdocs\ebassignment\confirm.php on line 41- Freya. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 I didn't check my code for syntax, so it's possible that there is a missing closing brace. If you have problems locating the problem, post the whole new script.Ken Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 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] Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 Ah.. the minute I posed that, found the missing semi-colon. Sorry! I take it to add the price, description etc, I would need to do the same for them? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 Yes, make each into an array.Ken Quote Link to comment Share on other sites More sharing options...
freya-san Posted November 24, 2006 Author Share Posted November 24, 2006 Thank you very much for your help! :)As this is for an academic project, the help will be acknowledged. The only thing I would like to know is how you want to be acknowledged - personally or the forum/website in general?Thanks, again!- Freya. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 24, 2006 Share Posted November 24, 2006 If you use the other techniqueif (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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.