jschofield Posted March 2, 2007 Share Posted March 2, 2007 Hello all. What I am trying to do is change the number9school code) to the school name. For some reason that I dont know it wont work for me. Please help. I would owe anyone who helps me huge. Row 11 is where the school code resides in the file. Thanks, Johnnie <?php $inputfile = "C:/SMDIM/Data/LSDSTUDENTS.csv"; $outputfile = "C:/SMDIM/Data/Livingston_Students_Data.csv"; $logfilename = "C:/SMDIM/Logs/Livingston_Student_Log.txt"; include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; //open the file if(!$outfilefp = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile"); }else{ wlog("Opened $outputfile for writing"); } if(!$inputfp = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile"); }else{ wlog("Opened $inputfile for reading"); } //replace school code with school name while ($row = fgetcsv($inputfp, 1000, ",")){ $schoolnames = array( "0" => "Central Office", "00" => "Central Office", "1" => "Burnet Hill Elementary School", "01" => "Burnet Hill Elementary School", "2" => "Collins Elementary School", "02" => "Collins Elementary School", "3" => "Harrison Elementary School", "03" => "Harrison Elementary School", "4" => "Hillside Elementary School", "04" => "Hillside Elementary School", "5" => "Mt. Pleasant Elementary School", "05" => "Mt. Pleasant Elementary School", "6" => "Riker Hill Elementary School", "06" => "Riker Hill Elementary School", "60" => "Mt. Pleasant Middle School", "55" => "Heritage Middle School", "50" => "Livingston High School", ); If (isset($schoolnames[$row[11]])) { $campusname = $schoolnames[$row[11]]; } else { $campusname = "Bad Name"; } //bring needed data and push out unwanted data //add word student at end of each record $temp = array($row[0], $row[1], $row[2], $row[3], $row[4], $row[6], $row[8], $row[9], $row[10], $row[12], $row[13]); $row[14] = "Student"; $count++; writeLine($outfilefp, $row, $campusname); } if($count !== '0') { wlog("finished the import with a total of $count records"); }else{ wlog("Failed to import data"); } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2007 Share Posted March 3, 2007 I see a number of problems with your script: Move the "$schoolnames" array out of the loop and get rid of the redundant entries. You create a "$temp" array, but never use it When you write an array to a file, you need to write the elements, if you don't you will get the word "Array" written. Here's code I think will work: <?php $inputfile = "C:/SMDIM/Data/LSDSTUDENTS.csv"; $outputfile = "C:/SMDIM/Data/Livingston_Students_Data.csv"; $logfilename = "C:/SMDIM/Logs/Livingston_Student_Log.txt"; include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; //open the file if(!$outfilefp = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile"); }else{ wlog("Opened $outputfile for writing"); } if(!$inputfp = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile"); }else{ wlog("Opened $inputfile for reading"); } $schoolnames = array( "Central Office", "Burnet Hill Elementary School", "Collins Elementary School", "Harrison Elementary School", "Hillside Elementary School", "Mt. Pleasant Elementary School", "Riker Hill Elementary School", 50 => "Livingston High School", 55 => "Heritage Middle School", 60 => "Mt. Pleasant Middle School", ); //replace school code with school name while ($row = fgetcsv($inputfp, 1000, ",")){ if (isset($schoolnames[$row[11]])) { $campusname = $schoolnames[$row[11]]; } else { $campusname = "Bad Name"; } //bring needed data and push out unwanted data //add word student at end of each record $temp = array($row[0], $row[1], $row[2], $row[3], $row[4], $row[6], $row[8], $row[9], $row[10], $row[12], $row[13]); $temp[] = "Student"; $count++; fwrite($outfilefp, implode(','$temp) . ',' . $campusname . "\r\n"); } if($count !== '0') { wlog("finished the import with a total of $count records"); }else{ wlog("Failed to import data"); } ?> Ken Quote Link to comment Share on other sites More sharing options...
Archadian Posted March 3, 2007 Share Posted March 3, 2007 your / needs to be \ and if that doesn't work try \\ ... / only works in URLs...if you are trying to retrieve a file from the actual webserver you have to use \ or \\ Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2007 Share Posted March 3, 2007 No, PHP will do the right thing when running on Windows if you use forward slashes in your filenames. This makes code very portable when moving between different OS's. Ken Quote Link to comment Share on other sites More sharing options...
jschofield Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks guys The code from ken will work but I keep getting a T_VARIABLE error on the fwrite line. Probalby just needing a comma somewhere. I will be looking into it but not to good at php as you have seen so might need more help. Thanks guys. Johnnie Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 5, 2007 Share Posted March 5, 2007 Sorry, I missed a comma. Here is the corrected line: <?php fwrite($outfilefp, implode(',',$temp) . ',' . $campusname . "\r\n"); ?> Ken Quote Link to comment Share on other sites More sharing options...
jschofield Posted March 6, 2007 Author Share Posted March 6, 2007 It all worked great. Thanks Ken! You are the man. I greatly thank you for your support. Johnnie Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.