<?php
    $csvfile
'exam.csv';
    
$year =2004;
    
$session ='jan';
    
$monthnames= array('jan'=>1,'feb'=>2,'mar'=>3,'apr'=>4);

    if(!isset(
$csvfile) || $csvfile =='') {exit;}

    if(!(
$fhandle=fopen($csvfile,"r"))) {
       print(
" cannot open file $csvfile");
       exit;
    }

// create all the transactions first
   
    
$error=false;
    while (!
feof($fhandle)){
      
$recfgets($fhandle,256);
      
$rec=trim(chop($rec));   // remove termination crlf
//    ignore blank lines
      
if(strlen($rec)==0) { continue;}
      
$exam explode(',',$rec);  // split into an array on , separator
      
$mcodestrtoupper(trim($exam[0]));
      
$component=strtoupper(trim($exam[1]));  if ($component==''$component='null';
      
$title=trim($exam[2]);
      
$edate=$exam[4];  
      
$d explode('-',$edate);  
      
$m $monthnames[strtolower($d[1])];
      
$edate='20'.$d[2].'-'.$m.'-'.$d[0];
      
$stime$exam[5];
      
$ftime $exam[6];
      
$dur $exam[7];
      
$query[] = "insert into module(mcode,title) values ('$mcode', '$title')";
      
$roomsexplode('/',$exam[8]);
      
$query[]= "insert into exam (academicyear,session,mcode,component,startdate,starttime,duration,endtime)
                values ($year,'$session','$mcode','$component','$edate','$stime','$dur','$ftime')"
;
      foreach(
$rooms as $room) {
// we probably encountered this room before, so replace instead of insert
         
$query[] = "replace into room(roomno) values ('$room')";
         
$query[] = "insert into exam_room(academicyear,session,mcode,component,roomno) 
                     values ($year,'$session','$mcode','$component', '$room')"
;
      }
    }

   
fclose($fhandle);
   if (
$error) {
      print(
" errors found in schedule - failed");
      exit;
   }
//   print_r($query);

// now process them all
// connect to the MySQL server
   
require ('../../../examsql.inc');


   if(!
$dblink=mysql_connect($server,$user,$password)) {
     print(
"mysql_connect failed<br>\n");
     print(
mysql_error() . "<br>\n");
     exit;
   }

// select the database for subsequent queries
   
if(!mysql_select_db($db,$dblink)) {
      print(
"unable to connect to $db<br/>\n");
      print(
mysql_error() . "<br>\n");
      exit;
   }
   foreach(
$query as $q) {
      print (
"$q<br/>");
      if (!
mysql_query($q,$dblink)) {
         print(
mysql_error() . "<br/>\n");
      }
   }

// close the database
   
mysql_close($dblink);

?>

</body>
</html>