将php structred程序转换为面向对象的编程

时间:2015-05-11 05:29:11

标签: php oop csv

$classes = array("1A","1B","1C","1D","1E","1F","2A","2B","2C","2D","2E","2F","3A","3B","3C","3D","3E","4A","4B","4C","4D","4E","5A","5B","5C","5D","6A","6B","6C","6D");
$columns = array ("Sl. NO","Grade","ADMN NO","Name of the child","Exam","Eng-M","Eng-G","Mat-M","Mat-G","Sci-M","Sci-G","Soc-M.","Soc-G","Fr-M","Fr-G","Ar-M","Ar-G","Isl-M","Isl-G","Q.Hist","Q.h-Grade","ICT-M","ICT-G","Remark from the CT");
$count=sizeof($columns);

$result=fopen("Result/Result.csv","w");

//Print Heading
for ($i=0;$i<$count-1;$i++)
{
    fwrite($result,$columns[$i]);
    fwrite($result,",");
}
fwrite($result,$columns[$i]);
fwrite($result,"\n");

$slno=0;
foreach ($classes as $class)
{
    //foreach (glob("$class*.csv") as $filename) 
    //{
    //    $files[] = $filename;
    //}
    //natsort($files); // sort.

    // print.
    //foreach($files as $file) 
    //{
    //        echo("$file\n");
    //}
    //$files=array();


    $clstst1 = fopen("$class-clstst1.csv","r");
    $midyr = fopen("$class-midyr.csv","r");
    $clstst2 = fopen("$class-clstst2.csv","r");
    $annual = fopen("$class-Annual.csv","r");

    $annual_line=fgetcsv($annual);  
    $clstst1_line=fgetcsv($clstst1);
    $midyr_line=fgetcsv($midyr);
    $clstst2_line=fgetcsv($clstst2);
    $count1_6=sizeof($annual_line);

    while($annual_line=fgetcsv($annual))
    {
        //Printing Student name and admission number
        $slno++;
        fwrite($result,"$slno,");
        fwrite($result,"$class,");
        for ($i=1;$i<3;$i++)
        {
            fwrite($result,$annual_line[$i]);
            fwrite($result,",");
        }

        //Printing Class Test 1 marks
        fwrite($result,"Class Test 1,");
        $clstst1_line=fgetcsv($clstst1);
        for ($i=3;$i<$count1_6-1;$i++)
        {
            fwrite($result,$clstst1_line[$i]);
            fwrite($result,",");
        }
        fwrite($result,"\"$clstst1_line[$i]\"");
        fwrite($result,"\n");

        //Printing Mid Year marks
        $midyr_line=fgetcsv($midyr);
        fwrite($result,",,,,");
        fwrite($result,"Mid Year,");
        for ($i=3;$i<$count1_6-1;$i++)
        {
            fwrite($result,$midyr_line[$i]);
            fwrite($result,",");
        }
        fwrite($result,"\"$midyr_line[$i]\"");
        fwrite($result,"\n");

        //Printing Class Test 2 marks
        $clstst2_line=fgetcsv($clstst2);
        fwrite($result,",,,,");
        fwrite($result,"Class Test 2,");
        for ($i=3;$i<$count1_6-1;$i++)
        {   
            fwrite($result,$clstst2_line[$i]);
            fwrite($result,",");
        }
        fwrite($result,"\"$clstst2_line[$i]\"");
        fwrite($result,"\n");

        //Printing Annual Marks
        fwrite($result,",,,,");
        fwrite($result,"Annual,");
        for ($i=3;$i<$count1_6-1;$i++)
        {       
            fwrite($result,$annual_line[$i]);
            fwrite($result,",");
        }
        fwrite($result,"\"$annual_line[$i]\"");
        fwrite($result,"\n");

    }

    fclose($clstst1);
    fclose($midyr);
    fclose($clstst2);
    fclose($annual);

}
fclose($result);

&GT?; “

我正在研究PHP。我的目标是将各种csv文件中的所有信息转储为一个学生标记格式的csv文件。以上代码完美无缺。你能帮我把这个程序转换成面向对象的PHP程序。

由于

1 个答案:

答案 0 :(得分:0)

这样做的简单方法..

<?php
class CsvController{

public function __construct()
{
    // write your code
}

function convertCsv() {

    $classes = array("1A","1B","1C","1D","1E","1F","2A","2B","2C","2D","2E","2F","3A","3B","3C","3D","3E","4A","4B","4C","4D","4E","5A","5B","5C","5D","6A","6B","6C","6D");
    $columns = array ("Sl. NO","Grade","ADMN NO","Name of the child","Exam","Eng-M","Eng-G","Mat-M","Mat-G","Sci-M","Sci-G","Soc-M.","Soc-G","Fr-M","Fr-G","Ar-M","Ar-G","Isl-M","Isl-G","Q.Hist","Q.h-Grade","ICT-M","ICT-G","Remark from the CT");
    $count=sizeof($columns);

    $result=fopen("Result/Result.csv","w");

    //Print Heading
    for ($i=0;$i<$count-1;$i++)
    {
        fwrite($result, $columns[$i]);
        fwrite($result,",");
    }
    fwrite($result,$columns[$i]);
    fwrite($result,"\n");

    $slno=0;
    foreach ($classes as $class)
    {
        //foreach (glob("$class*.csv") as $filename) 
        //{
        //    $files[] = $filename;
        //}
        //natsort($files); // sort.

        // print.
        //foreach($files as $file) 
        //{
        //        echo("$file\n");
        //}
        //$files=array();


        $clstst1 = fopen("$class-clstst1.csv","r");
        $midyr = fopen("$class-midyr.csv","r");
        $clstst2 = fopen("$class-clstst2.csv","r");
        $annual = fopen("$class-Annual.csv","r");

        $annual_line=fgetcsv($annual);  
        $clstst1_line=fgetcsv($clstst1);
        $midyr_line=fgetcsv($midyr);
        $clstst2_line=fgetcsv($clstst2);
        $count1_6=sizeof($annual_line);

        while($annual_line=fgetcsv($annual))
        {
            //Printing Student name and admission number
            $slno++;
            fwrite($result,"$slno,");
            fwrite($result,"$class,");
            for ($i=1;$i<3;$i++)
            {
                fwrite($result,$annual_line[$i]);
                fwrite($result,",");
            }

            //Printing Class Test 1 marks
            fwrite($result,"Class Test 1,");
            $clstst1_line=fgetcsv($clstst1);
            for ($i=3;$i<$count1_6-1;$i++)
            {
                fwrite($result,$clstst1_line[$i]);
                fwrite($result,",");
            }
            fwrite($result,"\"$clstst1_line[$i]\"");
            fwrite($result,"\n");

            //Printing Mid Year marks
            $midyr_line=fgetcsv($midyr);
            fwrite($result,",,,,");
            fwrite($result,"Mid Year,");
            for ($i=3;$i<$count1_6-1;$i++)
            {
                fwrite($result,$midyr_line[$i]);
                fwrite($result,",");
            }
            fwrite($result,"\"$midyr_line[$i]\"");
            fwrite($result,"\n");

            //Printing Class Test 2 marks
            $clstst2_line=fgetcsv($clstst2);
            fwrite($result,",,,,");
            fwrite($result,"Class Test 2,");
            for ($i=3;$i<$count1_6-1;$i++)
            {   
                fwrite($result,$clstst2_line[$i]);
                fwrite($result,",");
            }
            fwrite($result,"\"$clstst2_line[$i]\"");
            fwrite($result,"\n");

            //Printing Annual Marks
            fwrite($result,",,,,");
            fwrite($result,"Annual,");
            for ($i=3;$i<$count1_6-1;$i++)
            {       
                fwrite($result,$annual_line[$i]);
                fwrite($result,",");
            }
            fwrite($result,"\"$annual_line[$i]\"");
            fwrite($result,"\n");

        }

        fclose($clstst1);
        fclose($midyr);
        fclose($clstst2);
        fclose($annual);

    }
    fclose($result);
}// end of function convertCsv

#call the function by creating object of the class. you can do here ie in the same class OR in differnt class.

$myobj = new CsvController();

$myobj->convertCsv();

}// end of class
?>