使用php编辑文本文件中的行组

时间:2014-02-21 14:42:46

标签: php text-files

我有一个文本文件,其顶部和底部都有公司详细信息。在中间,它具有像这样的所有员工细节

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

我需要使用以下功能创建一个php页面

list all employee details
edit a employee details
delete a employee details
add new employee details
如果这些信息存储在数据库中,我可以轻松地做到这一点。 我认为通过逐行阅读,我可以轻松列出所有员工的详细信息。

1) is there any other best way to do listing?
2) how can i edit/delete/add employee details?
你可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:0)

将数据读入数据库是有趣的部分,因为如果你犯了错误就可以截断表格。

但要做CRUD(创建读取更新删除)并不是那么有趣,许多基本的繁琐工作在这里没有人会给你代码或者为你完成解决方案。如果你想成为一名优秀的程序员,你需要一直学习和自学!

我确信那里有更好的资源,但是w3schools的速成课程一定会让你顺利上路。

<强> 1。 PHP: http://www.w3schools.com/php/default.asp

<强> 2。 MySQL: http://www.w3schools.com/php/php_mysql_intro.asp

第3。 PHP mySQLi分机: http://www.w3schools.com/php/php_ref_mysqli.asp

<强> 4。 JavaScript,jQuery和AngularJS :http://www.w3schools.com/js/default.asp

好的,可以使用一些免费代码将数据读入数据库:

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>";