Jump to content

[SOLVED] Change of number to name using an array


jschofield

Recommended Posts

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

}

 

?>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.