2

I am creating the table in the database in which the month fields gets created dynamically according to the date values existing in the different table. The code is:

//Get avaialable moths in the table
$sql ="Select Distinct MONTHNAME(STR_TO_DATE(MONTH(test_date), '%m')) monthname from emp_csv order by MONTH(test_date) ASC ";
$i=0;
foreach($_pdo->query($sql) as $row)    {
     $reportnoarr[$row["monthname"]]=$i;
     $monthconstruct[$i++]=$row["monthname"];

}
$implodedmonthname=implode($monthconstruct," varchar(30),")." varchar(30),totalreport varchar(30) ";


//Creating dynamic table 
$sql ="DROP  TABLE t_month";
$stat = $_pdo->query($sql); 
$sql ="CREATE  TABLE t_month ( region varchar(25),engineer varchar(50),$implodedmonthname)";
$stat = $_pdo->query($sql); 

   for($kj=0;$kj<12;$kj++){
                   $reportnumberarray[$kj]=0;
           }
//inserting data to the newly ceated table
$sql ="Select territory,web_rep_no, test_performed_by,MONTHNAME(STR_TO_DATE(MONTH(test_date), '%m')) monthname  from emp_csv order by test_performed_by, monthname,web_rep_no ";



$preveng="";
$totalrep=0;
$territoryname="";
$prevreport="";
$countrep=0;
$prevmonth="";
foreach($_pdo->query($sql) as $row)   {
$nexteng= $row["test_performed_by"];
$nextreport=$row["web_rep_no"];
$nextmonth=$row["monthname"];
if($preveng!=$nexteng && $preveng!=""){
$totalrep=$totalrep+$countrep;
for($mj=0;$mj<count( $monthconstruct);$mj++){
$reportar[$mj]=$reportnumberarray[ $reportnoarr[$monthconstruct[$mj]]];
}
$imploded="'".implode($reportar,"','")."'";
$sql = "INSERT INTO t_month VALUES ('$territoryname','$preveng',$imploded,'$totalrep')"; 

$stat = $_pdo->query($sql); 
$totalrep=0;
$countrep=0;
$prevmonth="";
$prevreport="";
   for($kj=0;$kj<12;$kj++){
                   $reportnumberarray[$kj]=0;
           }
}
if($prevmonth!=$nextmonth){
$totalrep=$totalrep+$countrep;
$countrep=0;

}
if($prevreport!=$nextreport)
$countrep++;



$reportnumberarray[$reportnoarr[$row["monthname"]]]=$countrep;

$preveng=$nexteng;
$territoryname=$row["territory"];
$prevreport=$nextreport;
$prevmonth=$nextmonth;
}

Now I want to display all this dynamically created fields on the output dynamically as they are created without using select * from table function. Can anyone help me on this?

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Akshay Vasu
  • 445
  • 1
  • 12
  • 33

0 Answers0