0

I have a text file which have the company details in the top and bottom. in the middle, it has the all employee details like this

Company name: ABC Group of company
Company Location: AYZ
Company Address: XYS,YTS,123
Company found: 2013
Company website: abc.com
Company email: email@abc.com

Employee number: 001
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

Employee number: 003
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 004
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 011
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 022
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

Employee number: 044
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

Employee number: 009
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager  

Employee number: 031
Employee name: ABC
Employee phone number: 123456789
Employee address: XYX, HHH,123
employee date of birth:xxxx
employee email:xxxx
employee joined date:xxxx
employee position: manager

company chairman name:jjjj
company type of business:ffffff

i need to use create a php page with following function

list all employee details
edit a employee details
delete a employee details
add new employee details

if this information stored in the database, i can easily do it. i think by reading line by line, i can easily list all employee details.

1) is there any other best way to do listing?
2) how can i edit/delete/add employee details?

can you help on this?

Thanks

  • 1
    Is there any *real* reason you cannot use a database? This is possible to do in a file, but really not the best approach. – James Feb 21 '14 at 14:45
  • thank you for your quick reply. the text file is used by another application. we need to develop web app to edit it. – user3337608 Feb 21 '14 at 14:48
  • Can't the other application store to a database? If not, then you want to read about accessing a file in PHP. http://uk3.php.net/manual/en/function.file.php <- starting page, see the list on the left too – James Feb 21 '14 at 14:51
  • no. need to use the text file – user3337608 Feb 21 '14 at 17:57
  • No probs then. As said in my last comment, you need to read about the PHP file functions. Once you have tried and have some code we can help with it. – James Feb 21 '14 at 18:58
  • i have no idea which function should i use. can you help on this? – user3337608 Feb 21 '14 at 19:47
  • I already linked to the function you could use. `file()`. But I don't want to advise the approach you should use - A) Because this isn't a tutorial site; and B) (Mainly) because I wouldn't do this, and it's fiddly and (arguably) hacky so I don't really want to advise on it. You just need to use the examples on the page I linked to, and get the file into an array, then explode based on whatever separator you use between customers data. Then you can reiterate through them with the array however you need to. – James Feb 21 '14 at 20:05
  • I'm not being awkward, I just don't feel I'm best suited to be advising how to do something that I feel strongly shouldn't be done. Search this site and Google etc for "PHP Flat File Databases" - eg http://stackoverflow.com/questions/85/flat-file-databases – James Feb 21 '14 at 20:08
  • thank you for your reply. I can add/edit/delete employee by reading whole file line by line and rewriting all again. i thought, it it OK if the file size is small. is there any other way to change only perpendicular line without proceeding whole data – user3337608 Feb 22 '14 at 05:47

1 Answers1

0

To read the data into a database is the fun part, because you can just truncate the tables if you made a mistake.

But to do CRUD (Create Read Update Delete) is not so fun, lots of basic tedious work which no-one here is going to give you the code for OR complete the solution for you. If you want to become a good programmer you need to learn and teach yourself all the time!

I'm sure there are better resources out there but a crash course with w3schools will definitely put you on your way.

1. PHP: http://www.w3schools.com/php/default.asp

2. MySQL: http://www.w3schools.com/php/php_mysql_intro.asp

3. PHP mySQLi ext: http://www.w3schools.com/php/php_ref_mysqli.asp

4. JavaScript, jQuery and AngularJS: http://www.w3schools.com/js/default.asp

Ok, on to some free code to read the Data into your DB:

http://phpfiddle.org/main/code/ix3s-bmzz

$lines = file("employeedata.txt");

$final_company = array();
$final_employees = array();
$employee = array();
foreach($lines as $line)
{
    $line = str_replace("\n","",$line); // Clear end of line character

    if ($line != "") {  // Check for blank line (indicator of next record)
        $arr = explode(":",$line);
        $fields = explode(" ",$arr[0]);
        $vals = $arr[1];
        switch(strtolower($fields[0])) {
            case "company" : $final_company[$fields[1]] = $vals;
            break;
            case "employee" : $employee[$fields[1]] = $vals;
            break;
        }
    } else {        
        if (!empty($employee)) array_push($final_employees, $employee);
        $employee = array(); // Clear array for next employee
    }
}
if (!empty($employee)) array_push($final_employees, $employee); //Catch last entry

// Enter your DB INSERT statement here, following is a dump of the arrays

echo "<pre> <b>COMPANY DATA:</b><br/>";
print_r($final_company); 
echo "<b>EMPLOYEE DATA:</b><br/>";
print_r($final_employees);
echo "</pre>";
johan
  • 998
  • 6
  • 20