<?php
	
	
	/*
	// https://www.aqcm.fr/v2/graphiques_hebdo/hebdo_mails_1.php
	include ("../_identifiants/sql_identification_sans_base.php");
	$NB_sites = 1 ;
	$aqcm_SITES_ID = ["05230_002"];
	*/
	
	//--------------------------------------------------------------------------------------------------------
	// initialisations des variables
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_A = false ;
	$Flag_debug_A = true ;

	for ( $index_site = 0; $index_site < $NB_sites; $index_site++ ) {
		
		if ( $Flag_debug_A ) { echo "<br>" ; }

		//--------------------------------------------------------------------------------------------------------
		// initialisation des variables indicées sur $index_site
		//--------------------------------------------------------------------------------------------------------
		$NB_users[$index_site]		=  0 ;
		$USER_ID[$index_site]		= [] ;
		$USER_PW[$index_site]		= [] ;
		$USER_STATUS[$index_site]	= [] ;
		$USER_LEVEL[$index_site]	= [] ;
		$USER_T_MAX[$index_site]	= [] ;
		$USER_MAIL[$index_site]		= [] ;

		$get_users_of_one_site_link_OK[$index_site]		= false ;
		$get_users_of_one_site_erreur[$index_site]		= 0 ;
		$get_users_of_one_site_message[$index_site]		= "none" ;
		
		//--------------------------------------------------------------------------------------------------------
		// connexion à la base de données du site
		//--------------------------------------------------------------------------------------------------------
		$conn_AQCM05 = new mysqli( $mysql_host, $mysql_user, $mysql_password );
		
		$mysql_base_site[$index_site] = "AQCM05_SITE_" . $aqcm_SITES_ID[$index_site]  ;

		if ( empty ( mysqli_fetch_array( mysqli_query( $conn_AQCM05, "SHOW DATABASES LIKE '" . $mysql_base_site[$index_site] . "'" ) ) ) ) {
			
			$get_users_of_one_site_erreur[$index_site]  = 1 ;
			$get_users_of_one_site_message[$index_site] = "la base du site " . $aqcm_SITES_ID[$index_site] . " n'existe pas !!!" ;
			
		}

		if ( $get_users_of_one_site_erreur[$index_site] == 0 ) {
			
			$link_AQCM05 = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_base_site[$index_site]);
			
			if ( ! $link_AQCM05 ) {
				
				$get_users_of_one_site_erreur[$index_site]  = 2 ;
				$get_users_of_one_site_message[$index_site] = "connexion à la base du site " . $aqcm_SITES_ID[$index_site] . " : échec !!!" ;
				
				if ( $Flag_debug_A ) {
					echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo '<br>';
					echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; echo '<br>';
					echo "Connexion impossible à la base de donnée MySQL !" . PHP_EOL; echo '<br>';
				}

			}
			else {
				
				$get_users_of_one_site_link_OK[$index_site] = true ;
				
			}
			
		}

		//--------------------------------------------------------------------------------------------------------
		// recherche des users du site demandé
		//--------------------------------------------------------------------------------------------------------
		if ( $get_users_of_one_site_link_OK[$index_site] ) {

			$query_2[$index_site] = "SELECT USER_ID,USER_STATUS,USER_LEVEL,USER_T_MAX,USER_WEEK_MAIL FROM AQCM05_SITE_USERS ORDER BY USER_ID";

			$result = mysqli_query( $link_AQCM05, $query_2[$index_site] ) ; 
			
			if ( $result ) {
				
				if ( mysqli_num_rows($result) > 0 ) {	
					
					$index_users = 0 ;
					
					while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {

						$USER_ID[$index_site][$index_users]			= $row["USER_ID"];
						$USER_STATUS[$index_site][$index_users]		= $row["USER_STATUS"];
						$USER_LEVEL[$index_site][$index_users]		= $row["USER_LEVEL"];
						$USER_T_MAX[$index_site][$index_users]		= $row["USER_T_MAX"];
						$USER_WEEK_MAIL[$index_site][$index_users]	= $row["USER_WEEK_MAIL"];
						
						$index_users++ ;
						
					}
					
					$NB_users[$index_site] = $index_users ;

				}
				else {
					
					$get_users_of_one_site_erreur[$index_site]  = 3 ;
					$get_users_of_one_site_message[$index_site] = "résultat de la requête sur " . $aqcm_SITES_ID[$index_site] . " : vide !!!" ;
				}


			}
			else {
				
				$get_users_of_one_site_erreur[$index_site]  = 4 ;
				$get_users_of_one_site_message[$index_site] = "requête sur " . $aqcm_SITES_ID[$index_site] . " : échec !!!" ;

			}

		}
		
		//--------------------------------------------------------------------------------------------------------
		// clôture
		//--------------------------------------------------------------------------------------------------------
		if ( $get_users_of_one_site_link_OK[$index_site] ) {
			
			mysqli_close($link_AQCM05);
			
		}

		//--------------------------------------------------------------------------------------------------------
		// affichage console
		//--------------------------------------------------------------------------------------------------------
		if ( $Flag_debug_A ) {
			
			if ( $get_users_of_one_site_erreur[$index_site] != 0 ) {
				
				echo( "SITE: " . $aqcm_SITES_ID[$index_site] . " ; status : " . $get_users_of_one_site_erreur[$index_site] . " ; " . $get_users_of_one_site_message[$index_site] . "<br>") ;
				
			} 
			else {
				
				echo( "SITE: " . $aqcm_SITES_ID[$index_site] . " ; NB_users[" . $index_site . "] = " . $NB_users[$index_site] . "<br>");
				
				for ( $index_users = 0; $index_users < $NB_users[$index_site]; $index_users++ ) {
					
					echo("i = " . $index_site . " ; " . "j = " . $index_users . " ; " . $USER_ID[$index_site][$index_users] . " ; " . $USER_STATUS[$index_site][$index_users] . " ; " . $USER_LEVEL[$index_site][$index_users] . " ; " . $USER_T_MAX[$index_site][$index_users] . " ; " . $USER_WEEK_MAIL[$index_site][$index_users] . "<br>" );
					
				}
				
			}
			
		}
						
	}

?>

