<?php require_once('Connections/conn.php'); ?>
<?php
    set_time_limit(0);
    mysql_select_db($database_conn, $conn);
	mysql_query ("SET NAMES 'utf8'");
	
	$query_inscritos = "SELECT distinct id_alumno FROM historial_alumnos where id_periodo='70'";
	$rst_inscritos = mysql_query($query_inscritos, $conn) or die(mysql_error());
	$total_rows_inscritos = mysql_num_rows($rst_inscritos);
	echo "Total registros a procesar: ".$total_rows_inscritos.'<br>';
	$n=0;
	while ($row_inscrito = mysql_fetch_row($rst_inscritos)) { 
	      $id_alumno=$row_inscrito[0];		
		  $semestre=semestre($id_alumno);
		  $query_semestre = "update historial_alumnos set semestre='$semestre' where id_periodo='70' and id_alumno='$id_alumno'";
	      mysql_query($query_semestre, $conn) or die(mysql_error());
		  $n++;
	}
	echo "Total de registros procesados: ".$n;
?>

<?php	
function semestre($id_alumno)
{
   global $conn;
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='1' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre1= current($row);
  
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='2' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre2= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='3' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre3= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='4' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre4= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='5' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre5= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='6' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre6= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='7' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre7= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='8' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre8= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='9' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre9= current($row);
   
   $sql = "SELECT COUNT(*) FROM `inscripciones` where semestre='10' and id_alumno='$id_alumno'";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   $row = mysql_fetch_assoc($res);
   reset($row);
   $semestre10= current($row);
  
   $matriz = array(0,$semestre1,$semestre2,$semestre3,$semestre4,$semestre5,$semestre6,$semestre7,$semestre8,$semestre9,$semestre10); 
   
   $max=0;
   for ($i=1; $i<count($matriz); $i++){
        if($matriz[$i]>$max){
		   $max=$matriz[$i];
		   $sem=$i;
		}
	}
	return $sem;   
    }
?>