Web Hosting Talk







View Full Version : PHP - collecting information from a text file ?


XtremeAU
09-08-2008, 02:43 AM
Hi all,

I have a question in regards to coding to a particular platform. Basically what i have done so far is design a little form, when data is entered and 'submitted' - the entered data writes to a text file. What i have then done is, had the data from the text file display into a html table

What i would like to do now is use what is in the text file and display the relevant data to the chosen category. The data to be entered is as follows: ID, First Name, Last Name, *Department*, Salary, Date of Employment.

Essentially i would like to be able to select from a choice of departments and then see how many employees are in the one department.

What i have done so far.....

This is the code that displays the contents of the text file 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>";
?>


I'm trying to get the 'department' category to display using variations of this code:


<?
$totals=array();
$records = file("records.txt");
foreach ($records as $key => $value) {
$department=explode(":", $value);
print_r($department);
$thisdept = $department[1];
if (array_key_exists($thisdept,$totals)) {
$totals[$thisdept]+=1;
} else {
$totals[$thisdept]=1;
}
print_r($totals);
}
?>



I'd appreciate some pointers as to where i am going wrong.

mod_webhosting
09-08-2008, 04:54 AM
Is a use of the database an option for you? If it is then you would be querying the database to find information you need. Something like SELECT COUNT(*) FROM records WHERE department = 7; would return the number of employees in a certain department.

Sure, this problem can be resolved using text files only, but it would complicate things.

bigfan
09-08-2008, 10:00 AM
Using a database is a better solution, but using a text file should be fairly simple. In either case, in your coding you should separate the data processing from its display, and you should use arrays more to your advantage.

Something like this:
$fields = array('ID', 'Given Name', 'Family Name', 'Department', 'Salary', 'Date');
$department_count = array();
$file_lines = file('records.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($file_lines as $line) {
$data_arr[] = array_combine($fields, explode(':', $line));
}
foreach ($data_arr as $each) {
if (!isset($department_count[$each['Department']])) {
$department_count[$each['Department']] = 1;
} else {
$department_count[$each['Department']]++;
}
}

// Now data is in the arrays $data_arr, $department_count, and $fields
// to use as you wish, such as:

echo '<table border="1" align="center"><tr>';
foreach ($fields as $field) {
echo '<th>' . $field . '</th>';
}
echo '</tr>';
foreach ($data_arr as $row) {
echo '<tr>';
foreach ($row as $item) {
echo '<td>' . $item . '</td>';
}
echo '</tr>';
}
echo '</table>';

echo '<table border="1" align="center">'
. '<tr><th>Department</th>'
. '<th># Employees</th></tr>';
foreach ($department_count as $key => $val) {
echo '<tr><td>' . $key . '</td><td>' . $val . '</td></tr>';
}
echo '</table>';

awatson
09-08-2008, 03:56 PM
Assuming you control the code that outputs the html flat file, you could also "seed" in some comments that won't show up for the user, but will make finding fields easier, i.e.


<td><!-- START DEPT -->$depart<!-- END DEPT --></td>


Then your processing script just has to look for what's between the comments, which should be pretty straightforward with preg_match.

But a real database is still a better option.