我知道基本的PHP和SQL,通常我会用数据库解决以下问题。
我有几个CSV文件都包含一个公共PERSON_ID。对于此任务,我无法使用任何数据库,只能使用同一文件夹中的文件,并且我希望使用PHP和HTML显示与ID对应的数据。
有没有办法从所有文件中获取与ID关联的数据,然后将值设置为可显示的HTML格式?
例如,FILE-1.CSV看起来像这样:
PERSON_ID,THE_DATE,CODE,VALUE1,VALUE2,VALUE3,VALUE4
123,11/21/2013,ABC987,6,2,5,8
234,10/15/2011,DEF542,4,2,5,8
345,8/23/2012,GHI34,6,7,5,8
456,2/7/2012,FDA1,2,1,5,8
567,4/30/2011,QWERTY4,6,2,4,8
678,1/23/2014,ABC987,2,2,6,8
而FILE-2.CSV看起来像这样:
CASE_NUMBER,PERSON_ID,STATUS,STATE
98,456,Active,CA
97,678,Active,WA
96,123,Completed,NJ
95,567,Active,TX
94,456,Completed,OH
93,345,Active,UT
如果我有URL“my.php?person_id = 456”,我可以得到PHP输出说:
PERSON_ID: 456
Number of Cases: 2
Date on system: 2/7/2012
Cases: 98 (Active in CA), 94 (Completed in OH)
提前谢谢,我希望这是可以实现的:)
答案 0 :(得分:0)
你应该添加一些支票。例如,如果url中没有传递id,那么你不应该遍历任何文件。如果找不到该人,则不循环csv案例并显示有关未找到该人的消息。此外,如果该人没有任何案例,您应该显示一条消息,说明没有找到案件。
$id = $_GET['person_id'];
$person = array();
$cases = array();
$case_str = '';
if (($handle = fopen("file-1.csv", "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$person_id = $data[0];
$the_date = $data[1];
$code = $data[2];
$value1 = $data[3];
$value2 = $data[4];
$value3 = $data[5];
$value4 = $data[6];
if($person_id == $id) {
$person = array('person_id' => $person_id, 'the_date' => $the_date, 'code' => $code, 'value1' => $value1, 'value2' => $value2, 'value3' => $value3, 'value4' => $value4);
break;
}
}
fclose($handle);
}
if (($handle = fopen("file-2.csv", "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$case_number = $data[0];
$person_id = $data[1];
$status = $data[2];
$state = $data[3];
if($person_id == $id) {
$cases[] = array('case_number' => $case_number, 'person_id' => $person_id, 'status' => $status, 'state' => $state);
}
}
fclose($handle);
}
echo "PERSON_ID: " . $id . "<br />\n";
echo "Number of Cases: " . count($cases) . "<br />\n";
echo "Date on system: " . $person['the_date'] . "<br />\n";
echo "Cases: ";
foreach($cases as $case) {
$case_str .= $case['case_number'] . " (" . $case['status'] . " in " . $case['state'] . "),";
}
echo rtrim($case_str, ',');