ibz786 Posted February 27, 2011 Share Posted February 27, 2011 I have a text file with the following: HA11QS, 200, house1.jpg, 4 HA22BR, 280, house2.jpg, 10 HA33AB, 390, house3.jpg, 3 HA44CD, 320, house4.jpg, 8 I have a form: <?php if (isset($_POST['price'])) { $filename = "houses.txt"; $fileOpen = fopen($filename, "r"); $max = $_POST['price']; $rowsArr = file ($filename); foreach ($rowsArr as $row) { $lineDetails = $row; $item_array = explode (",", $row); if (((int) $item_array[1]) <= $max) { ?> <form action='visit2.php' method='post'> <?php echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3]. "<br>"); ?> <input type='checkbox' name='mybox'> <input type='submit' value='Visit Property'> </form> <?php } } fclose($fileOpen); } ?> and i also have "visit.php" <?php if (isset($_POST['mybox'])) { $filename = "houses.txt"; $fileOpen = fopen($filename, "r+"); $filename2 = "houses2.txt"; $fileWrite = fopen($filename2, "w"); $visit = $_POST['mybox']; $rowsArr = file ($filename); foreach ($rowsArr as $row) { $lineDetails = $row; $item_array = explode (",", $row); $fileText = $item_array[3]+1; if ($visit == true) { echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3] . "<br>"); echo("<br>"); fwrite($fileWrite, $fileText); } } fclose($fileOpen); fclose($fileWrite); } ?> What i need is , when the user in the form, clicks on the checked box, it sends the information to 'visits.php' and it writes to the text file, increasing the selected value by one Please can anyone help, im really struggling with this Thank You Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 27, 2011 Author Share Posted February 27, 2011 Im really confused, if the thing i need is either to write or append I feel it maybe appened, but how do i ? e.g. After clicking on the first house, increase (and write to the file) the number of visits from 4 to 5? If anyone knows where im going wrong please help in addition i know in the code provided i have: "houses.txt" and then "houses2.txt", i was just experimenting since i didnt want to spoil the original file if anything were to go wrong Quote Link to comment Share on other sites More sharing options...
.josh Posted February 27, 2011 Share Posted February 27, 2011 is there any reason why you are trying to do this with a text file instead of a database? Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 try this $item_array[3] += 1; instead of $fileText = $item_array[3]+1; Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 27, 2011 Author Share Posted February 27, 2011 Indeed, its part of my coursework I know for a fact that all that i am doing would be so much easier via an SQL Database, and i have more of an idea on how to do it on there since arrays as such wouldnt be needed etc ... However our lecturer wants to read it from a text file which is really troublesome Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 27, 2011 Author Share Posted February 27, 2011 try this $item_array[3] += 1; instead of $fileText = $item_array[3]+1; Hi i tried that: echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3] += 1 . "<br>"); unfortunately it didnt work :S I may have not put it in the right area, but still not sure :/ Edit: Also i have realised that when i write "houses2.txt" Instead of updating the number of visits, the whole file becomes blank :S Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 you changed the wrong code. replace $fileText = $item_array[3]+1; with $item_array[3] += 1; Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 27, 2011 Author Share Posted February 27, 2011 you changed the wrong code. replace $fileText = $item_array[3]+1; with $item_array[3] += 1; My apologies, I tried changing that however, the problem remains the same, it doesnt seem to replace 4 with 5 (for the first property) I just seems to overwrite the whole txt file and leaves it blank Not sure what to do here really :/ Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 that's because you write to the file with fwrite($fileWrite, $fileText); however, $fileText is an empty variable. I think what you need to do is changing all the "echo" above this line to $fileText .= //so this echo("Post Code - " . $item_array[0]. "<br>"); //becomes this $fileText .= "Post Code - " . $item_array[0]. "<br>"; additionally, I think you check twice whether the checkbox is checked or not, once with if (isset($_POST['mybox'])) and once with if ($visit == true) Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 27, 2011 Author Share Posted February 27, 2011 Ok ive made the code to work slightly: It has indeed incremented it, by one however i have a few errors, i have uploaded a picture of the error My current code is: <?php if (isset($_POST['mybox'])) { $filename = "houses2.txt"; $fileOpen = fopen($filename, "a"); $visit = $_POST['mybox']; $rowsArr = file ($filename); foreach ($rowsArr as $row) { $lineDetails = $row; $item_array = explode (",", $row); $newVisit = $item_array[3]+=1; $fileText = "Number of Visits - " . $newVisit . "<br>"; echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3]. "<br>"); echo("<br>"); fwrite($fileWrite, $fileText); } fclose($fileOpen); } ?> Not sure where im going wrong :S [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 The error message tells you everything you need to know: "Undefined variable: fileWrite" You save the opened file to a variable called $fileOpen, but try to write to it using a variable called $fileWrite. Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 27, 2011 Author Share Posted February 27, 2011 Yeah sorry i realised that straight after i posted that my aplogies, been a long day Ok now what happens is that ive got the code to increment the house visits by one, however, it doesnt overwrite e.g. 4 (for house one) to 5, i just creates a whole new line :s Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 <?php if (isset($_POST['mybox'])) { $filename = "houses2.txt"; $file = fopen($filename, "a"); $rowsArr = file($filename); foreach ($rowsArr as $row) { $item_array = explode (",", $row); $item_array[3] += 1; //no need to save this to a new variable $fileText = "Post Code - " . $item_array[0]. "<br>"; $fileText .= "Price - " . $item_array[1]. ",000 <br>"; $fileText .= "Picture - " . $item_array[2]. "<br>"; $fileText .= "Number of Visits - " . $item_array[3]. "<br>"; $fileText .= "<br>"; fwrite($file, $fileText); } fclose($file); } ?> hope it works! Quote Link to comment Share on other sites More sharing options...
flolam Posted February 28, 2011 Share Posted February 28, 2011 I just realized that I completely misunderstood what you wanted to do. I think this should work, but it's untested <?php //form.php if (isset($_POST['price'])) { $filename = "houses.txt"; $fileOpen = fopen($filename, "r"); $max = $_POST['price']; $rowsArr = file($filename); $i = 0; foreach ($rowsArr as $row) { $item_array = explode(", ", $row); if (((int) $item_array[1]) <= $max) { ?> <form action='action.php' method='post'> <?php echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3]. "<br>"); ?> <input type="hidden" name="linenumber" value="<?php echo $i; ?>" /> <input type='checkbox' name='mybox' /> <input type='submit' value='Visit Property' /> </form> <?php } $i++; } fclose($fileOpen); } //action.php if (isset($_POST['mybox'])) { $filename = "houses.txt"; $file = fopen($filename, "a"); $rowsArr = file($filename); $row = $rowsArr[$_POST["linenumber"]]; //get the row we want to change $item_array = explode(", ", $row); //split it into an array so we can change $item_array[3] += 1; //do the change $row = implode(", ", $item_array); //re-assemble the row $rowsArr[$_POST["linenumber"]] = $row; //replace the old row with the re-assembled one $fileText = implode("\n", $rowsArray); //re-assemble the file by joining all rows echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3]. "<br>"); echo("<br>"); fwrite($file, $fileText); fclose($file); } ?> Quote Link to comment Share on other sites More sharing options...
ibz786 Posted February 28, 2011 Author Share Posted February 28, 2011 Hi, flolam Thanks for all your help i really appreciate it The code works enough for me to be satisfied with it, your efforts are much appreciated it, Thank you very much All the best to you 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.