多重循环遍历统计 (横坐标、纵坐标、统计数


横坐标是分类,纵坐标是分组,中间数据是对应的统计总数。

思路:

          用全部数据 遍历分类,对分类进行一一对比,并把当前的类型与上次对比,一致时+1  否则进行下次循环。


全部报名数据

多重循环遍历统计 (横坐标、纵坐标、统计数_第1张图片

$sql = "SELECT m.eventtypeid, eventgroupid,Count(*) as Num FROM
				(SELECT tf.teamid, e.eventid, eventtypeid, eventgroupid, t.status FROM teamform tf, events e, team t 
				WHERE tf.matchid = $matchid AND tf.eventid = e.eventid AND tf.teamid = t.teamid) AS m
				GROUP BY m.eventtypeid,m.eventgroupid ORDER BY m.eventgroupid, m.eventtypeid";



调用 


C层

$matchid = $_SESSION['adminmatch'];
  	$EventType = $this->Search_model->getEvntType($matchid);//横坐标 :分类
  	$GroupName = $this->Search_model->getGropuName($matchid);//纵坐标:分组, 此次,它只做显示层
  	//var_dump($GroupName);
  	//die();
  	//var_dump($EventType);

  	// 查询比赛项目分组分类型汇总的数据
  	$GroupTypeSummary = $this->Search_model->countTeamSignNum($matchid, $EventType);//此为进行业务处理用全部数据 遍历分类




多重循环遍历统计 (横坐标、纵坐标、统计数_第2张图片

M层:

public function countTeamSignNum($matchid, $EventType)  方法

        if($arrSignSum){
        	//$EventType = $this->getEvntType($matchid);
        	$intSignSum = count($arrSignSum);
        	$intTypeTotal = count($EventType);
	        $intGroupCount = 0;
        	$i = 0;
        	//echo "intSignSum:".$intSignSum."-".$intTypeTotal."<BR>";

        	while($i<$intSignSum){

        		// 新的赛事分组开始
        		if($lastGroupID<>$arrSignSum[$i]["eventgroupid"]){
        			$lastGroupID=$arrSignSum[$i]["eventgroupid"];
        			
        			//echo "arrSignSum[i][eventgroupid]:".$arrSignSum[$i]["eventgroupid"]."<BR>";
        			//die;
        			// 根据比赛类型,循环给某一分组的所有赛事类型统计数赋值
        			for($j = 0;$j < $intTypeTotal;$j++){

        				// 如果统计数组中比赛类型eventypeid相同,则输出统计数Num到相应的类型所对应的列中。
        				if($i>=$intSignSum){
        					$returndata[$intGroupCount][$j]=0;
        				}else{
	        				if($arrSignSum[$i]["eventtypeid"]==$EventType[$j]["eventtypeid"]){
	        					$returndata[$intGroupCount][$j]=(int)$arrSignSum[$i]["Num"];
	        					//echo "eventtypeid:".$EventType[$j]["eventtypeid"]."-i:".$i."-j:".$j."-sum:".$arrSignSum[$i]["Num"]."<Br>";
	        					$i = $i + 1;
	        				}else{
	        					$returndata[$intGroupCount][$j]=0;
	        					//echo $i."<Br>";
	        				}
	        			}
        			}
        			$intGroupCount = $intGroupCount + 1;

        			//echo "Type End:".$i."<Br>";
        		}
        	}
        }

C层:总方法(包含控制 写入excel, 先写横坐标再写纵坐标)

public function summary(){


  	$matchid = $_SESSION['adminmatch'];
  	$EventType = $this->Search_model->getEvntType($matchid);
  	$GroupName = $this->Search_model->getGropuName($matchid);
  	//var_dump($GroupName);
  	//die();
  	//var_dump($EventType);

  	// 查询比赛项目分组分类型汇总的数据
  	$GroupTypeSummary = $this->Search_model->countTeamSignNum($matchid, $EventType);
  	//var_dump($GroupTypeSummary);
  	//die;
  	$strTableData = '';
  	$matchid = $_SESSION['adminmatch'];
		error_reporting(E_ALL);     
		set_include_path(get_include_path() . PATH_SEPARATOR . 'download/Classes/'); 
		include 'PHPExcel.php';   
		include 'PHPExcel/Writer/Excel2007.php'; 
		  
		//echo date('H:i:s') . " 创建新的PHPExcel对象<br/>";   
		$objPHPExcel = new PHPExcel();   
		   
		// 设置导出excel格式
		//echo date('H:i:s') . " Set properties\n";   
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");   
		$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");   
		$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");   
		$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");   
		$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");   
		$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");   
		$objPHPExcel->getProperties()->setCategory("Test result file");   
		   
		

		 $objPHPExcel->setActiveSheetIndex(0);   
		 $objPHPExcel->getActiveSheet()->setCellValue('A1', '组别');   
		
		$arrCellLetter = array('B','D','F','H','J','L','N','P','R','T','V','X','Z');//每个组另占两个单元格
		//var_dump($arrCellLetter);
		//echo $arrCellLetter['1'];
		
		//var_dump(1%2);
		$EventTypeData = $this->Search_model->getEvntType($matchid);
		$intrecord = 0;
		
		$countgroup = count($EventTypeData);//组别总数
		
		if($EventTypeData){
			foreach($EventTypeData as $key => $value){
				//var_dump($value['typename']);
				
				   $objPHPExcel->getActiveSheet()->setCellValue($arrCellLetter[$intrecord].'1', $value['typename']);
				
				$intrecord = $intrecord + 1;
			}
		}
		
		//$objPHPExcel->getActiveSheet()->setCellValue('O1', '');   
		$objPHPExcel->getActiveSheet()->setCellValue('A2', '岁别');   
		

		$arrCellLetter = array('B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
		
		$nu = 0; //$arrCellLetter数组下标
		for($i=1; $i<=$countgroup*2; $i++){
			if($i%2==1){
				$objPHPExcel->getActiveSheet()->setCellValue($arrCellLetter[$nu].'2', '队数');
			}else{
				$objPHPExcel->getActiveSheet()->setCellValue($arrCellLetter[$nu].'2', '场数');
			}
				$nu = $nu + 1;
		}
		//==============================设置单元格格式=====================================
		$objStyleA1 = $objPHPExcel->getActiveSheet()->getStyle('A1:O2');  
    $objAlignA1 = $objStyleA1->getAlignment();  
		$objAlignA1->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    //左右居中  
    $objAlignA1->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);  //上下居中  
    //字体及颜色  
    $objFontA1 = $objStyleA1->getFont();  
    $objFontA1->setName('黑体');  
    $objFontA1->setSize(12);  
		//==============================合并单元格=====================================
		$objActSheet = $objPHPExcel->getActiveSheet();
		$objActSheet->mergeCells('B1:C1'); 
		$objActSheet->mergeCells('D1:E1'); 
		$objActSheet->mergeCells('F1:G1');
		$objActSheet->mergeCells('H1:I1'); 
		$objActSheet->mergeCells('J1:K1'); 
		$objActSheet->mergeCells('L1:M1');  
		$objActSheet->mergeCells('N1:O1');

		//--------------------------写入数据到excel--------------开始------------------------------------------ 
		
		// $matchid = $_SESSION['adminmatch'];
	 //  	$EventType = $this->Search_model->getEvntType($matchid);
	 //  	$GroupName = $this->Search_model->getGropuName($matchid);	  	
	 //  	$GroupTypeSummary = $this->Search_model->countTeamSignNum($matchid, $EventType);
		
		//var_dump($NewarrGroup);
		//var_dump($NewarrGroup[6]['smname']); 

		//先放队数
		if($GroupTypeSummary){
			$intrecord = 2;
			
			for($i=0; $i<count($GroupTypeSummary); $i++){
				$intrecord= $intrecord+1;
				if($i<count($GroupName)){
					$objPHPExcel->getActiveSheet()->setCellValue('A'.$intrecord, $GroupName[$i]['groupname']);   
				}
				$nu = 0;
				for($j = 0;$j < count($EventType);$j++){
										//echo "<td>".$GroupTypeSummary[$i][$j]."</td>";
										
					 
					 	$objPHPExcel->getActiveSheet()->setCellValue($arrCellLetter[$nu].$intrecord, $GroupTypeSummary[$i][$j]);
				$nu = $nu + 2;
				}
				

			}


			
			
		}else{
			
		}

		//再放场数,场数为队数-1,重新循环队数将场数值得到

			$fieldnumber = array();
			$num = 0; //队数给于变量num
			if($GroupTypeSummary){
			$intrecord = 2;
			
			for($i=0; $i<count($GroupTypeSummary); $i++){
				$intrecord= $intrecord+1;
			
				$nu = 1;
				for($j = 0;$j < count($EventType);$j++){

						 $num = $GroupTypeSummary[$i][$j];
				 		 if($num==0){
				 		 	$num = 1;
				 		 }
					 	 $fieldnumber[$i][$j] = $num-1;
					 	$objPHPExcel->getActiveSheet()->setCellValue($arrCellLetter[$nu].$intrecord, $fieldnumber[$i][$j]);
		
					$nu = $nu + 2;
				}
			}	

			}else{
			
		}
		//var_dump($fieldnumber);
			
		//--------------------------写入数据到excel--------------结束------------------------------------------  
		// 重命名sheet   
		//echo "<br/>".date('H:i:s') . " 重命名sheet<br/>";   
		$objPHPExcel->getActiveSheet()->setTitle('sheet1');   
		   
		// 第一张工作表组活跃表索引,所以Excel打开这是第一个表   
		$objPHPExcel->setActiveSheetIndex(0);   
		   
		// Save Excel 2007 file  //保存为 Excel2007格式
		//echo date('H:i:s') . " 保存为Excel2007格式<br/>";   
		$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
		//var_dump(__FILE__); 
		//var_dump($_SERVER['DOCUMENT_ROOT']);
		$excelname = "summary".date('Ymd');//"导出参赛组数场数统计表excel";
		$path = $_SERVER['DOCUMENT_ROOT']."/download/excel/".$excelname.".xlsx"; 
		$pathgbk = mb_convert_encoding($path, "GBK", "UTF-8"); 
		$objWriter->save(str_replace("/","\\",$pathgbk));
		//$objWriter->save(str_replace('application\\controllers\\down.php', 'download\\excel\\text1.xlsx', __FILE__));     
		//echo "<br/>".date('H:i:s') . "保存文件>>".$path; 
		
		//$strTable = "各项目报名数统计信息共计".$intCount."条,导出成功,产生文件时间:".date('Y-m-d H:i:s',time())."。";
		//$strTableData="";
		
		
		
		/*
		$this->load->view('header');
		$this->load->view('downexcel',$data);
		$this->load->view('footer');
		*/

	
		$data['EventType'] = $EventType;
		$data['GroupName'] = $GroupName;
		$data['GroupTypeSummary'] = $GroupTypeSummary;
		$this->load->view('header');
		$this->load->view('export',$data);
		$this->load->view('footer');







	}

V层:

 <div class="box-content">
			    <table class="table table-striped table-bordered bootstrap-datatable ">
				    <?php 
				    //var_dump($GroupTypeSummary);
				    //echo $GroupTypeSummary['0']['groupname'];
				    ?>
				    <?php
							if($EventType){
								echo "<thead><tr><th>组别</th>";
								foreach($EventType as $key => $value){
									echo "<th>".$value['typename']."</th>";
								}
								echo "</tr></thead>";
							}
							if($GroupTypeSummary){
								for($i = 0;$i < count($GroupTypeSummary);$i++){
								   if($i<count($GroupName)){
								   		echo "<tr><td>".$GroupName[$i]['groupname']."</td>";
								   }
                 					
									for($j = 0;$j < count($EventType);$j++){
										echo "<td>".$GroupTypeSummary[$i][$j]."</td>";
									}
								echo "</tr>";
								}
							}
				    ?>
			      <tbody>
				   
			      </tbody>
		      </table>            
		    </div>


你可能感兴趣的:(多重循环遍历统计 (横坐标、纵坐标、统计数)