Greetings, I have a code that is giving me problems with a foreach
and I have not achieved its correct operation, it is a query to a database SQLServer
with PDO ODBC, I have tested the queries separately in PHP and it does not give problems (These queries must be dynamic has resulted from the results of a static), I have tested the queries directly in the database engine and the results are the expected the problem arises in the php, I hope you can help me.
PHP
function cargosUsuarios(){
$mssql=conectarBDSOF();
$sql="SELECT codiCC, DescCC FROM v_TPersonalB WHERE codiCC LIKE '20%' AND mes<>'' GROUP BY DescCC, codiCC";
$cargosNo=array('002','006','009','010','027','67','001','021','016','011','005','038','054','76','003','007','62','81');
$tablaCC='<table class="table" style="margin-bottom:0;">';
$tablaCC.='<tbody>';
foreach ($mssql->query($sql) as $row) {
$sqlCARGOS="SELECT CarNom, carCod FROM v_TPersonalB WHERE codiCC='".trim($row["codiCC"])."' AND mes<>'' AND carCod<>'' GROUP BY CarNom, carCod";
if("40-04"!=$row["codiCC"]){
$cargos='<table class="table" style="margin-bottom:0;">';
$cargos.='<tbody>';
echo $sqlCARGOS;
foreach ($mssql->query($sqlCARGOS) as $row2) { // AQUI SURGE EL PROBLEMA
if(in_array($row2["carCod"], $cargosNo)){}else{
$cargos.='<tr>';
$cargos.='<td>'.$row2["CarNom"].'</td>';
$cargos.='<td><input type="checkbox" name="cargosUsusarios[]" value="'.$row["codiCC"].'/'.$row2["carCod"].'"></td>';
$cargos.='</tr>';
}
}
$cargos.='</tbody>';
$cargos.='</table>';
$tablaCC.='<tr>';
$tablaCC.='<td style="vertical-align:middle;">'.$row["DescCC"].'</td>';
$tablaCC.='<td colspan="2">'.$cargos.'</td>';
$tablaCC.='</tr>';
}
}
$tablaCC.='</tbody>';
$tablaCC.='</table>';
echo $tablaCC;
}