|
发表于 2024-8-2 21:57
来自手机
|
显示全部楼层
wengjl 发表于 2024-8-2 13:08
我不懂归集概念,但我有一个思路:
用第一行里的每个教师姓名,到以下各行中查找,找到的就 写上 1 ,再 ...
这个思路不错,参与一下。
<?php
$sapi_type = php_sapi_name();
if($sapi_type=='cli'){
$rootPath='/storage/emulated/0/Download/htdocs';
}else{
$rootPath=$_SERVER["DOCUMENT_ROOT"];
}
$db = new PDO('sqlite:'.$rootPath.'/EData/app/db');
function restoreNewGrp(){
global $db;
$sql="create temp table newGrp as select * from newGrp2";
$result=$db->query($sql);
$sql="select * from newGrp";
$result=$db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
//print_r($result);
}
function getNamesByClass($minClass){
global $db;
$sql="select 姓名 from newGrp where 班级 in ($minClass)";
//echo "\n".'getNamesByClass sql:'.$sql."\n";
$result=$db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$arrNames=[];
foreach ($result as $arrVal){
$arrNames[]=$arrVal["姓名"];
}
//print_r($arrNames);
$criteriaNames="'".implode("','",$arrNames)."'";
return $criteriaNames;
}
function getClasses($criteriaNames){
global $db;
$sql="select 班级 from newGrp where 姓名 in ($criteriaNames)";
//echo $sql;
$result=$db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$arrNames=[];
foreach ($result as $arrVal){
$arrNames[]=$arrVal["班级"];
}
//print_r($arrNames);
$criteriaNames="'".implode("','",$arrNames)."'";
return $criteriaNames;
}
function getNextClasses(){
global $db;
$sql="select 班级 from newGrp limit 1";
//echo $sql;
$result=$db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
if(sizeof($result)==0) return '';
$criteriaNames="'".$result[0]["班级"]."'";
return $criteriaNames;
}
function deleteByClasses($classes){
global $db;
$sql="delete from newGrp where 班级 in ($classes)";
$result=$db->query($sql);
return 1;
}
function reGrp($grpId,&$arrGroup,$minClass,$criteriaNames){
if(empty(trim($minClass,"'"))){
return;
}else{
$criteriaNames=getNamesByClass($minClass);
}
if(!empty(trim($criteriaNames,"'"))){
$arrMinClass=explode("','",trim($minClass,"'"));
foreach($arrMinClass as $aClass){
$arrGroup[$grpId][]=$aClass;
}
deleteByClasses($minClass);
}
$minClass=getClasses($criteriaNames);
if(empty(trim($minClass,"'"))){
$grpId++;
$minClass=getNextClasses();
if($criteriaNames=="''"){
return;
}else{
reGrp($grpId,$arrGroup,$minClass,$criteriaNames);
}
}else{
reGrp($grpId,$arrGroup,$minClass,$criteriaNames);
}
}
/*
restoreNewGrp();
$grpId=1;
$arrGroup=[];
$minClass="'1'";
$criteriaNames='';
reGrp($grpId,$arrGroup,$minClass,$criteriaNames);
echo '<pre>';
print_r($arrGroup);
*/
|
-
|