2
I created a loop that shows all months of the year with the following code:
for($i = 1;$i <= 12;$i++){
}
then I created a $_GET variable that would receive a rule like the following:
1,2,3,4,5,6,7,8,9,10,11,12
This is the variable $_GET:
$listaMesesPagos = isset($_GET["mesespg"]) ? $_GET["mesespg"] : null;$listaMesesPagos = is_string($listaMesesPagos) ? $listaMesesPagos : null;
The purpose of this $_GET variable is to show the months that were paid by person X. For example if person X paid their monthly fee in the months: January, February and March (1,2,3). Then $_GET would receive the values: 1,2 and 3 as follows: http://localhost/pagina.php? mesespg=1,2,3 that would later be played on a ARRAY php through the explode() being like this:
$arrayMeses = explode(",",$listaMesesPagos);
But to use the explode() you first need to know if the $_GET variable received in the following rule: n,n,n that in this example is: 1,2,3 so I used the preg_match with the following $Pattern:
$regraDaListaDosMeses = '/^([0-9]{1,2}\,{1}){1,11}([0-9]){0,1}$/';
Thus, before using the explode php checks whether the $_GET variable is in this above rule: (1,2,3). If so, PHP will play the numbers for an array that looks like this: array(1,2,3); otherwise php will create an empty array to avoid errors as in the following code:
if(preg_match($regraDaListaDosMeses, $listaMesesPagos) == true){
$arrayMeses = explode(",",$listaMesesPagos);
}else{
$arrayMeses = array();
}
So the loop for() would check if the current month is in the array $arrayMeses case would not show that the month is pending. Follow the code:
for($i = 1;$i <= 12;$i++){
/*mês pago*/if(in_array($i, $arrayMeses)){/*CÓDIGO AQUI*/}
/*mês não pago*/else{/*CÓDIGO AQUI*/}
}
Now, based on that "delicate" explanation. : ) we go to the problem that is in the rule of preg_match.
see, in the rule: /^([0-9]{1,2}\,{1}){1,11}([0-9]){0,1}$/. This is not appropriate. I want a rule that checks that, for example, the following characters are correct: 1,2,3;1,2,3,;1,2,3,4,5,6,7,8,9,10,11,12.
Can you help me?