I created a loop that shows all the 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 $ _GET variable:
$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 have been paid by person X. For example if person X has paid their monthly fee in the months: January, February and March , 2,3). Then $ _GET would get the values: 1,2 and 3 in the following way: link that would later be thrown into an ARRAY php by explode ()
$arrayMeses = explode(",",$listaMesesPagos);
But to use explode () it is first necessary to know if the variable $ _GET received in the following rule: n, n, n that in this example is : 1,2,3 so I used 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 if the variable $ _GET is in this rule above: (1,2,3). If it is, PHP will throw the numbers into an array similar to this: array (1,2,3); otherwise php will create an empty array to avoid errors as it is in the following code:
if(preg_match($regraDaListaDosMeses, $listaMesesPagos) == true){
$arrayMeses = explode(",",$listaMesesPagos);
}else{
$arrayMeses = array();
}
Then the for loop would check if the current month is in the array $ arrayMeses if it would not show that the month is pending. Here is 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 this " delicate " explanation. :) Let's go to the problem that is in the preg_match rule.
see, in the rule: / ^ ([0-9] {1,2} \, {1}) {1,11} ([0-9]) {0,1} $ / . This is not appropriate. I want a rule that verifies 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?