notverybright Posted September 2, 2008 Share Posted September 2, 2008 Hi everyone, i'm in the process of attempting to learn various aspects of php. The current thing i am trying to accomplish is to take data that a user enters into a simple html form, have the information get saved into a text file, and to be able to display the information into a table. At present i have designed it so it starts off as a button click menu screen. The first button takes you to the form that is used to key in and gather the data which is processed and saved into a text file* The second button is used to display the contents of the text file into a table* I seem to be having trouble with these 'basic' concepts and was hoping someone might be able to help me out, by pointing me in the right direction for further advice or could possibly help with the code to spot errors that i can't see (a second pair of eyes would be great). The lumps of code i have are a little confusing to look at, i've read the forum rules..... but i might need to post them in large blocks (i'll hold off for the time being). Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/122355-issue-with-forms-submitting-data-into-txt-file-then-display-into-a-table/ Share on other sites More sharing options...
JonnoTheDev Posted September 2, 2008 Share Posted September 2, 2008 This could be a difficult task dependent on how you will display the data in the browser. You may need to set tags around each piece of data you are saving in the file so you can extract it correctly using string functions or regex. i.e. // text file [TITLE]This is the title[/TITLE] [bODY]This is the body[/bODY] The basics of this are to first validate the user input. Then store the data into a file using file(), fwrite(), etc. Retreiving the data may again involve the use of file() or file_get_contents(), etc Then you will need to break the file contents up into how you want to display it on screen. Maybe use some string functions to build up an array of the content parts in the text file. Quote Link to comment https://forums.phpfreaks.com/topic/122355-issue-with-forms-submitting-data-into-txt-file-then-display-into-a-table/#findComment-631793 Share on other sites More sharing options...
notverybright Posted September 2, 2008 Author Share Posted September 2, 2008 This is the form that provides the user the information to enter: <?php //open the file for writing $fh = fopen("Employees.txt","a+w"); $line = fgets($fh); $ID = $_POST['ID']; $givenName = $_POST['givenName']; $familyName = $_POST['familyName']; $department = $_POST['department']; $salery = $_POST['salery']; $dateStarted = $_POST['dateStarted']; echo '<html>'; echo ' <head>'; echo ' <title>'; echo ' Employee Records'; echo ' </title>'; echo ' </head>'; echo ' <body>'; echo ' <form action="Main.php" method="post">'; echo ' <table>'; echo ' <tr>'; echo ' <td>'; echo ' ID'; echo ' </td>'; echo ' <td>'; echo ' <input name="ID" type="text" />'; echo ' </td>'; echo ' </tr>'; echo ' <tr>'; echo ' <td>'; echo ' Given Name'; echo ' </td>'; echo ' <td>'; echo ' <input name="givenName" type="text" />'; echo ' </td>'; echo ' </tr>'; echo ' <tr>'; echo ' <td>'; echo ' Family Name'; echo ' </td>'; echo ' <td>'; echo ' <input name="familyName" type="text" />'; echo ' </td>'; echo ' </tr>'; echo ' <tr>'; echo ' <td>'; echo ' Department'; echo ' </td>'; echo ' <td>'; echo ' <select name="department">'; echo ' <option value="Sales">'; echo ' Sales'; echo ' </option>'; echo ' <option value="Marketing">'; echo ' Marketing'; echo ' </option>'; echo ' <option value="Management">'; echo ' Management'; echo ' </option>'; echo ' <option value="Accounts">'; echo ' Accounts'; echo ' </option>'; echo ' </td>'; echo ' </tr>'; echo ' <tr>'; echo ' <td>'; echo ' Salery'; echo ' </td>'; echo ' <td>'; echo ' <input name="salery" type="text" />'; echo ' </td>'; echo ' </tr>'; echo ' <tr>'; echo ' <td>'; echo ' Date'; echo ' </td>'; echo ' <td>'; echo ' <input name="dateStarted" type="text" />'; echo ' </td>'; echo ' </tr>'; echo ' <tr>'; echo ' <td colspan="2">'; echo ' <input type="submit" />'; echo ' </td>'; echo ' </tr>'; echo ' </table>'; { $data = $ID . ":" . $givenName . ":" . $familyName . ":" . $department . ":" . $salery . ":" . $dateStarted . "\n" ; fwrite($fh, $data); } //close the file fclose($fh); ?> Here is the 'processing' page that should take what the user had typed in and process the file: <html> <head> <title>Employee Database</title> </head> <body> <a href="Main.html">Click here to return to the main page</a><b /> <?php //open the file for writing $fh = fopen("Employees.txt","a+w"); $line = fgets($fh); $ID = $_GET['ID']; $givenName = $_GET['givenName']; $familyName = $_GET['familyName']; $department = $_GET['department']; $salery = $_GET['salery']; $dateStarted = $_GET['dateStarted']; //assemble the data into a variable $data= $ID . ":" . $givenName . ":" . $familyName . ":" . $department . ":" . $salery . ":" . $dateStarted ; // write the data to the file fwrite($fh, $data); //close the file echo "data output to file complete."; fclose($fh); ?> </body> </html> This is what i use to display what is processed into the text file: <?php echo '<table border="1"><tr><td>ID</td><td>Given Name</td><td>Family Name</td><td>Department</td><td>Salery</td><td>Date Started</td></tr>'; $fh = fopen("Employees.txt","r"); $line = fgets($fh); //check if end of file, if not process this line while(!feof($fh)) { $array = explode(":",$line); //put each element of the array into another variable $ID = $array[0]; $givenName = $array[1]; $familyName = $array[2]; $salery = $array[3]; $dateStarted = $array[4]; echo "<tr><td>".$array[0]."</td><td>".$array[1]."</td><td>".$array[2]."</td><td>".$array[3]."</td><td>".$array[4]."</td></tr>"; $line = fgets($fh); } //close the file fclose($fh); ?> Depending on the variation, i can get it to display in the table. But usually i can't, i think it is down to the way the write to file is formatted. The Text file should look something like :20:John:Smith:Sales:20:2/2/2008 but it comes out looking like: ::::::::::20:John:Smith:Sales:20:2/2/2008 I'm sorry to be a pain, i hope this helps diagnosing my problem further. Quote Link to comment https://forums.phpfreaks.com/topic/122355-issue-with-forms-submitting-data-into-txt-file-then-display-into-a-table/#findComment-632246 Share on other sites More sharing options...
JonnoTheDev Posted September 3, 2008 Share Posted September 3, 2008 When writing to the file get your data in an array so: $items[] = "item 1"; $items[] = "item 2"; $items[] = "item 3"; The you can use implode() to properly write the separated items in a line: $line = implode(":", $items); echo $line; This will print item 1:item 2:item 3 When retrieving the data you will need to explode() it back into an array so: $items = explode(":", $row); print_r($items); This will produce [0] => item1, [1] => item 2, [2] => item 3 and will make it much easier to display the data Quote Link to comment https://forums.phpfreaks.com/topic/122355-issue-with-forms-submitting-data-into-txt-file-then-display-into-a-table/#findComment-632580 Share on other sites More sharing options...
notverybright Posted September 5, 2008 Author Share Posted September 5, 2008 Thanks i've slightly cleaned up the code and changed a few things. I'm now able to use the form as the write to file now works. I've also managed to get the contents of the written file, into a table. <?php if (isset($_POST['posted'])) { $id = $_POST['id']; $gname= $_POST['givenName']; $fname = $_POST['familyName']; $depart = $_POST['department']; $salary = $_POST['salary']; $date = $_POST['date']; echo "<br />"; //validation echo "<br />"; if (! strlen($_POST['givenName'])) {print 'You must enter your first name. '; } echo "<br />"; if (! strlen($_POST['familyName'])) {print 'You must enter your surname name. '; } echo "<br />"; if (! strlen($_POST['department'])) {print 'You must enter the department you work for '; } echo "<br />"; if (! strlen($_POST['salary'])) {print 'You must enter your salary '; } echo "<br />"; if (! strlen($_POST['date'])) {print 'You must enter a date. '; } echo "<br />"; if ($_POST['salary'] != strval(intval($_POST['salary']))) {print 'You cannot enter text... must be a number '; } //open the file for writing $fh = fopen("records.txt","a"); $line = fgets($fh); $write = "\n".$id.":".$gname.":".$fname.":".$depart.":".$salary.":".$date; fwrite($fh, $write); echo "Information Posted"; } ?> and - displayed into a table: <?php $bigstring = file_get_contents("records.txt"); $bigarray = explode("\n",$bigstring); echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>ID</th>"; echo "<th>Given Name</th>"; echo "<th>Family Name</th>"; echo "<th>Department</th>"; echo "<th>Salary</th>"; echo "<th>Date</th></tr>"; foreach ($bigarray as $key => $value) { $smallarray=explode(":", $value); echo "<br>"; if (!empty($smallarray[0])){ $id=$smallarray[0]; $gname=$smallarray[1]; $fname=$smallarray[2]; $depart=$smallarray[3]; $salary=$smallarray[4]; $date=$smallarray[5]; echo "<tr><td>"; echo $id; echo "</td><td>"; echo $gname; echo "</td><td>"; echo $fname; echo "</td><td>"; echo $depart; echo "</td><td>"; echo $salary; echo "</td><td>"; echo $date; echo "</td></tr>"; } } echo "</table>"; ?> Thank you for your help What i might need help with later on is an extension of what i've just done. What i'd like to be able to do next is to make a few more table lists - one that views the names of those belonging to a certain department. And following on from that - displaying the total number of employees in each department. I know what i want to do and how i'd like it too look. Putting it into practice with the right php coding is what i will struggle with. Quote Link to comment https://forums.phpfreaks.com/topic/122355-issue-with-forms-submitting-data-into-txt-file-then-display-into-a-table/#findComment-634149 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.