2013-03-31 4 views
0

안녕하세요. 저는 .csv 파일에서 이름, 성 및 전자 메일을 가져올 수있는 코드가 이미 있지만 제대로 작동하지 않고 사용할 수 있습니다. 이걸 얻는 손..csv 파일의 특정 열을 얻는 방법

Heres는 현재 코드 : 나는 $의 FIRSTNAME 값을 인쇄 할 때

//this file is used to grab the usernames of the csv file and put them in a session, returning it memberinterface that will open a dialog box with the usernames 
$seterror = 0; 

function returnBack(){ 

    header("Location:../memberinterface.php?getmsg=15"); 
    exit; 

} 
function returnBackfile(){ 

    header("Location:../memberinterface.php?getmsg=16"); 
    exit; 
} 
if (!empty($_FILES['csvfile']['error'])) 
    { 

     $seterror = 1; 
     returnBack(); 

    } 
    if (empty($_POST['firstname'])){ 

     $seterror =1; 
     returnBack(); 
    } 
    if (empty($_POST['lastname'])){ 
     $seterrro =1; 

     returnBack(); 
    } 
    if (empty($_POST['email'])){ 
     $seterror =1; 

     returnBack(); 
    } 
    if ($seterror !== 1){ 

    $file = $_FILES['csvfile']['tmp_name']; 

    $handle = fopen($file , "r"); 

    $fileop = fgetcsv($handle,1000,","); 

    $fileop=array_map("strtoupper",array_map("trim",$fileop)); 

    $firstname_index = array_search(strtoupper($_POST["firstname"]),$fileop); <!-----these right here are not working correctly and will always send me back with the error, it does not find the firstname column? and same with the other ones. 
    if ($firstname_index===false){ 

     returnBack(); 
    } 
    $lastname_index = array_search(strtoupper($_POST['lastname']),$fileop); 
    if ($lastname_index===false){ 

     returnBack(); 
    } 
    $email_index = array_search(strtoupper($_POST['email']),$fileop); 
    if ($email_index===false){ 

     returnBack(); 
    } 
     //open file and store values 
     $j = 0; 
     while (($fileop=fgetcsv($handle)) !== false) 
     { 

      $fileop=array_map("trim",$fileop); 
      $firstname[$j] = $fileop[$firstname_index]; 
      if (empty($firstname[$j])){ 

      returnBackfile(); 

      } 

      $lastname[$j] = $fileop[$lastname_index]; 
       if (empty($lastname[$j])){ 

      returnBackfile(); 

      } 
      $email[$j] = $fileop[$email_index]; 
       if (empty($email[$j])){ 

      returnBackfile(); 


      } 
      $j++; 
     } 

     print_r($firstname); 


    } 

때문에, 그것은 .csv 파일에서하지만, 모든 값으로 값을 출력? 그래서

은 ...

경우 제발, 내가 .csv 파일에서 LASTNAME, 그것은 FIRSTNAME을받을 수 있도록 노력 및 이메일 열하고 정리해은 $ FIRSTNAME 배열, $의 LASTNAME 배열 등에 저장하기 당신은 내가 그것을 감사하게 도울 수 있었다!!

다윗이

가 업데이트 : 나는이 .csv 파일로 결과를 얻었다

$firstname_index = array_search($_POST["firstname"],$fileop);

의 ontop을 var_dump($fileop); var_dump($POST); 퍼팅 후 : 기본적으로

array(1) { [0]=> string(162) "FIRSTNAME LASTNAME EMAIL PHONE COMBINED SINGLE LIMIT BODILY INJURY EACH PERSON BODILY INJURY EACH ACCIDENT PROPERTY 
DAMAGE STATUS ADDRESS NOTES STATUS CHANGE DATE" } 
NULL Array ([0] => Hank Aaron [email protected] 777.777.7777 '0 20000 
40000 10000 Non-Active [1] => Fred Astaire [email protected] 222.431.5843 '0 
'0 '0 '0 Open 98 Ocean Blvd [2] => Jim Belushi [email protected] 
333.333.3333 '0 '0 '0 '0 Open 98 Ocean Blvd [3] => Charlie  
Brown [email protected] 909.111.1111 '0 '0 '0 '0 Open 98 
Ocean Blvd [4] => Carol Burnett [email protected] 911.911.9999 '0 '0 '0 
'0 Enrolled - PCG to Follow-Up 98 Ocean Blvd [5] => Bill Cosby 
[email protected] 989.989.9898 '0 100000 300000 100000 Confirmed 23 Surf Rd 
[6] => Charles Dickens [email protected] 897.333.3333 '0 '0 '0 '0 
Non-Active - Driver Chose Non-Compliance 23 Surf Rd [7] => Walt Disney 
[email protected] 909.909.9090 '0 '0 '0 '0 Non-Active - Driver Chose 
Non-Compliance 23 Surf Rd [8] => Bob Dylan [email protected] 333.444.5555 '0 '0 
'0 '0 Enrolled - Agent Emailed 23 Surf Rd [9] => Thomas Edison 
[email protected] 765.222.1111 '0 '0 '0 '0 Enrolled - Agent Emailed  
23 Surf Rd [10] => Harrison Ford [email protected] 345.543.5555 '0 '0 
'0 '0 Enrolled - Agent Emailed 23 Surf Rd [11] => Bill Gates 
[email protected] 123.456.7890 350000 '0 '0 '0 Enrolled - Employee Follow- 
Up Required 16 Tulip Ln [12] => Tom Hanks [email protected] 777.888.9999 '0 '0 
'0 '0 Enrolled - Agent Emailed 16 Tulip Ln [13] => Alfred Hitchcock 
[email protected] 878.000.9999 '0 '0 '0 '0 Non-Active - Other 16 
Tulip Ln [14] => Michael Jackson [email protected] 123.123.1234 '0 '0 
'0 '0 Non-Active - PolicyHolder Cancelled Additional Interest 16 Tulip Ln [15] => 
Michael Jordan [email protected] 888.888.8888 '0 '0 '0 '0 Non-Active 
- Employee Not Covered Under Listed Policy 16 Tulip Ln [16] => John Lennon 
[email protected] '098.765.5432 '0 '0 '0 '0 Non-Active - Insurance Cancelled 
16 Tulip Ln [17] => Joe Lewis [email protected] 888.999.1111 '0 15000 
30000 5000 Confirmed 4765 E Palm Rd [18] => Peyton Manning [email protected] 
111.222.3333 '0 '0 '0 '0 Enrolled - Agent Emailed 4765 E Palm 
Rd [19] => Marilyn Monroe [email protected] 212.121.1212 '0 '0 '0 '0 
Enrolled - Policyholder Follow-Up Required 4765 E Palm Rd [20] => Elvis Presley 
[email protected] 909.000.8761 '0 '0 '0 '0 Enrolled - New Carrier 
Being Processed 4765 E Palm Rd [21] => Dan Rather [email protected] 333.333.8888 '0 
25000 50000 15000 Confirmed 909 Kodak Ln [22] => Chris Rhodes 
[email protected] 916-205-2474 '0 '0 '0 '0 Open 909 Kodak 
Ln [23] => William Shakespeare [email protected] 444.444.4444 '0 
100000 300000 100000 Confirmed 926 Maple St [24] => Meryl Streep 
[email protected] 411.411.4111 '0 100000 300000 100000 Confirmed 412 East 
3rd St [25] => Mark Twain [email protected] 323.323.3232 '0 '0 '0 '0 
Enrolled - Agent Emailed 412 East 3rd St [26] => Darth Vader [email protected] 
874.321.4444 '0 25000 50000 15000 Confirmed 1234W U Parkway [27] => 
George Washington [email protected] 876.321.1234 '0 100000 300000 100000 
Confirmed 12 oak blvd [28] => Robin Williams [email protected] 
444.555.6666 '0 '0 '0 '0 Open 12 oak blvd) 
+0

당신이 게시 할 수'var_ CSV에서 첫 번째 줄을 읽은 후'dump ($ fileop)'와'var_dump ($ POST)'? – Barmar

+0

어디에서 ... $ firstname_index 섹션에 있습니까? –

+0

예, 문제가있는 코드입니다. – Barmar

답변

1

fgetcsv()가로 쉼표를 사용합니다 열 구분 기호. 파일은 탭으로 구분되어 있으므로 구분 기호를 명시 적으로 지정해야합니다.

변경 :

$fileop = fgetcsv($handle,0,"\t"); 

변경 :

$fileop = fgetcsv($handle,1000,","); 

while (($fileop=fgetcsv($handle)) !== false) 

에 :

while (($fileop=fgetcsv($handle, 0, "\t")) !== false) 
관련 문제