nubor Posted January 10, 2013 Share Posted January 10, 2013 Ok..so I am learning php and having isues with form processing and text files. What I am trying to accomplish is: submitting user input saving this data to an array validating that the data has been submitted sanitizing the data saving the array to a .txt file each form submission would append the new data array to the .txt file on a new line Then I am trying to create a page that: Pulls the data from the .txt file displays the data in a table on a webpage each line of the .txt file is a new array each array is a new row in the table. Once I get my head around this then, I want to update certain data fields. However, that will be after I can get my head around the above first. Below is the code that I have so far. Also, any insight on whether or not this is an appropriate and efficient method for validating and sanitizing would be helpful. I have read several articles and tutorials which have provided good information, but left gaps to be completely understood. Any assistance or insight would be appreciated. Thank You! The all in one form <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Software Bug</title> </head> <body> <?php if (isset($_POST['submit'])){ $bug["product"] =trim($_POST['product']); $bug["version"] = trim($_POST['version']); $bug["hardware"] = trim($_POST['hardware']); $bug["os"] = trim($_POST['os']); $bug["issue"] = trim($_POST['issue']); /* check user input fields for values. * if empty or not set or null echo key for empty field */ foreach ($bug as $key=>$value){ if (empty($value) || $value == "" || $value == NULL){ echo $key . " is not set<br />"; } else {echo $key . " is set<br />";} } /* create filter array to sanitize data */ $bugCleanser = array( "product" => FILTER_SANITIZE_STRING, "version" => FILTER_SANITIZE_ENCODED, "os" => FILTER_SANITIZE_STRING, "issue" =>FILTER_SANITIZE_STRING ); $cleanBugs = filter_var_array($bug, $bugCleanser); var_dump($cleanBugs); $file = fopen("solution.txt", "ab"); fputcsv($file, $cleanBugs); fclose($file); } ?> <form action="softwareform2.php" method="post"> <p>Product Name:<input type="text" name="product" value="" /> <p>Product Version:<input type="text" name="version" value="" /> <p>Hardware:<input type="text" name="hardware" value="" /> <p>OS:<input type="text" name="os" value="" /> <p>Issue:<br /><textarea rows="6'" cols="60" name="issue" value=""></textarea></p> <p><input type="submit" name="submit" value="Enter" /> <input type="reset" name="reset" value="reset" /></p> </form> <a href="showbugs.php">Show Software Bugs</a> </body> </html> This code to display the contents of the text file in a table. There are some commented out table tags because I had a somewhat working form, until I decided to a different method using array's <?php $solution = fopen("solution.txt", "r"); echo "<table width=\"80%\" border=\"1 solid black\">"; echo "<tr><th>Product Name</th><th>Product Version</th><th>Hardware</th><th>OS</th><th>Solution</th></tr>\n"; if ($solution){ while (!feof($solution)){ $lines[] = fgets($solution); } fclose($solution); foreach ($lines as $line){echo $line . "<br />";} } // for ($i=0; $i<count($solution); ++$i){ // $currSolution = explode(", ", $solution[$i]); // //echo "<p>Solution " . ($i+1) . "<br />\n"; // echo "<tr>"; // echo "<td>{$currSolution[0]}</td>"; // echo "<td>{$currSolution[1]}</td>"; // echo "<td>{$currSolution[2]}</td>"; // echo "<td>{$currSolution[3]}</td>"; // echo "<td>{$currSolution[4]}</td>"; // echo "</td></tr>\n"; // } // echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 Hi, welcome to the forums. Firstly I'm curious as to why your not using a database management system (DBMS)? This question dramatically influences what code you write. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2013 Share Posted January 10, 2013 Having used fputcsv() to write, why don't you use fgetcsv() to read? Quote Link to comment Share on other sites More sharing options...
nubor Posted January 10, 2013 Author Share Posted January 10, 2013 Thank you...good question.. First, I am still learning php and trying to get a grasp on working with directories and file systems. I know flat file systems are outdated where a database is preferred. However, I am trying to get a grasp on this before moving on to a DBMS for a solid foundation and understanding of working with php. Likewise, I know that this is procedural codeing and once I get a handle on the basics of PHP, I will be moving on to learning OOP and MVC with PHP. So, I guess this is part of my learning curve and if I can get a grasp on the PHP language, then I will start learning the different Frameworks. I find this fun, interesting, and fustrating all at the same time. I have been reading books, forums, and online tutorials which seem to leave gaps in the information. Being a novice it can be difficult to disseminate this information. Also, with so many different ways to accomplish the same task, it can be difficult to understand best practices. I have been following this forum for about a month and find it has helpful resources which why I joined. Thank you for your time and insight. Quote Link to comment Share on other sites More sharing options...
nubor Posted January 10, 2013 Author Share Posted January 10, 2013 when I used fputcsv and the used fgetcsv, I got an error unexpected array to string conversion. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2013 Share Posted January 10, 2013 Something like this should work $solution = fopen("solution.txt", "r"); echo "<table width=\"80%\" border=\"1 solid black\">"; echo "<tr><th>Product Name</th><th>Product Version</th><th>Hardware</th><th>OS</th><th>Solution</th></tr>\n"; if ($solution) { while ($line = fgetcsv($solution, 1024)) { echo '<tr><td>' . join ('</td><td>', $line) . "</td></tr>\n"; } fclose($solution); } echo "/table>\n"; Quote Link to comment Share on other sites More sharing options...
nubor Posted January 10, 2013 Author Share Posted January 10, 2013 Thanks alot...I did go back and used fputcsv and saved to a text file...and then used fgetcsv to retrieve the file...and it worked!! here is my code that retrieves the data and formats it as a table. Now what, I want to do is try to allow a user to update a field...not sure how to solve this one but I am going to try. Thanks for all your assistance. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Software Bug</title> </head> <body> <?php //Get Form data and assign it to an array if (isset($_POST['submit'])){ $bug["product"] =trim($_POST['product']); $bug["version"] = trim($_POST['version']); $bug["hardware"] = trim($_POST['hardware']); $bug["os"] = trim($_POST['os']); $bug["issue"] = trim($_POST['issue']); /* check user input fields for values. * if empty or not set or null echo key for empty field */ foreach ($bug as $key=>$value){ if (empty($value) || $value == "" || $value == NULL){ echo $key . " is not set<br />"; } else {echo $key . " is set<br />";} } /* create filter array to sanitize data */ $bugCleanser = array( "product" => FILTER_SANITIZE_STRING, "version" => FILTER_SANITIZE_ENCODED, "hardware" => FILTER_SANITIZE_STRING, "os" => FILTER_SANITIZE_STRING, "issue" =>FILTER_SANITIZE_STRING ); //assign sanitized data to a new array $cleanBugs = filter_var_array($bug, $bugCleanser); //write array data to a tab delimited text file $file = fopen("solution.txt", "a"); fputcsv($file, $cleanBugs, "\t"); fclose($file); } ?> <form action="softwareform2.php" method="post"> <p>Product Name:<input type="text" name="product" value="" /> <p>Product Version:<input type="text" name="version" value="" /> <p>Hardware:<input type="text" name="hardware" value="" /> <p>OS:<input type="text" name="os" value="" /> <p>Issue:<br /><textarea rows="6'" cols="60" name="issue" value=""></textarea></p> <p><input type="submit" name="submit" value="Enter" /> <input type="reset" name="reset" value="reset" /></p> </form> <a href="showbugs.php">Show Software Bugs</a> </body> </html> <?php echo "<table width=\"80%\" border=\"1 solid black\">\n"; echo "<tr><th>Product Name</th><th>Product Version</th><th>Hardware</th><th>OS</th><th>Solution</th></tr>\n"; $openFile = fopen("solution.txt", "r"); while (!feof($openFile)){ $fileContents = fgetcsv($openFile, filesize("solution.txt"), "\t"); echo "<tr>"; for ($i=0; $i <count($fileContents); ++$i){ echo "<td>" . $fileContents[$i] . "</td>"; } echo "</tr>\n"; } fclose($openFile); echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2013 Share Posted January 10, 2013 Now what, I want to do is try to allow a user to update a field This where you wish you were using a database table Quote Link to comment Share on other sites More sharing options...
nubor Posted January 10, 2013 Author Share Posted January 10, 2013 Understood...I am just trying to learn the hard way Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 Arguably the wrong way; at least for this day in age. Quote Link to comment Share on other sites More sharing options...
nubor Posted January 11, 2013 Author Share Posted January 11, 2013 (edited) Arguably the wrong way; at least for this day in age. For clearification...the code above is the wrong way or working with plain text files. It is my understanding that XML are a form of a flat file Database, and one of the reasons why I am trying to grasp some of these concepts. I understand that today is a database driven enviroment...so should I just forget about working with flat files and move to concentrating on more database interaction? Being that I am green to some of these concepts I appreciate the insight from more experienced developers. Thank you for your time and insights. Edited January 11, 2013 by nubor Quote Link to comment Share on other sites More sharing options...
Barand Posted January 11, 2013 Share Posted January 11, 2013 If all you want to do do is store data, flat file is fine. (As for XML I have a theory that someone at microshaft was instructed to buy up all the shares they could in companies selling storage devices as they were about to impose a technology that would increase storage requirements by a factor of ten.) I digress. If you want to do more with the data, such as update it, search it, sort it, then a database is the tool you should be using. Quote Link to comment Share on other sites More sharing options...
nubor Posted January 11, 2013 Author Share Posted January 11, 2013 Thank you .for the explianation.That in a nutshell was what I was looking for. So, I will move on. And thank you for the quick response, understanding, and patience for a newbie... Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 11, 2013 Share Posted January 11, 2013 While I agree 100% with Barand, I do think the flat-file concepts are very important. At some point in the future, you will have to read a flat-file to bring data into your database, or write a flat-file from data in the database (for someone else). So, don't give up on understanding the file system and flat-file use. And, as I understand your intent right now (to learn), even working to allow updates to that flat-file will benefit you in understanding how file operations behave. Quote Link to comment Share on other sites More sharing options...
haku Posted January 11, 2013 Share Posted January 11, 2013 I sort of agree. Knowing how to work with files will likely be necessary at some point in time, but knowing how to use a flat file database is pretty much pointless these days. Once you hit any level of complexity that needs a JOIN, your flat file database becomes worthless. Time is better spent learning how to manipulate files, and how to use databases. 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.