Result of multiple querys based on other querys values in PHP

I have a php that receive from a form some values as variables, i make a query with this values and show them in a table. PHP code is:

 $sql1="SELECT via,codvia,cp,municipio,provincia,( 6371000 * acos(cos(radians($gslat)) * cos(radians(lat)) * cos(radians(lng) - radians($gslng)) + sin(radians($gslat)) * sin(radians(lat)))) AS distancia
FROM $tabla where municipio ='$gsmunicipio' HAVING distancia < 500 ORDER BY distancia";
 
$result = mysqli_query($con, $sql1);
echo "<div class='table-responsive'>";
echo "<table class='table'>
<thead class='thead-dark'>
<tr>
<th scope='col'>Vía</th>
<th scope='col'>CodVia</th>
<th scope='col'>Distancia</th>
</tr>
</thead>
<tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td scope='col'>" . $row['via'] . "</td>";
echo "<td scope='col'>" . $row['codvia'] . "</td>";
echo "<td scope='col'>" . $row['distancia'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "</div>";

It returns the next data:

via                 codvia  distancia   
ANTONIO DE LEYVA        00460   12.20611003690737
SANTA MARIA DE LA CABEZA    01064   230.34840588687064
ANTONIO DE LEYVA        00460   233.86739822576007
MARQUESA DE SILVELA         03899   287.57855871469
DOCTOR CARMENA RUIZ         01727   348.5000437937512
BALEARES            00699   430.2384750664121
ENRIQUE FUENTES         01968   463.78877067869473
SAN MAGIN           05512   479.92500262187957
ALEJANDRO SANCHEZ       00173   495.8582670209417

With each codvia value, i need send a query to another table (t2) to obtain oter values from a column called “secc”. The query in mysql will be:


SELECT
DISTINCT(secc)
FROM t2
where
cpro=28 and cmum=079 and codvia in (00460,01064,00460,03899,01727,06312,00699,01968,05512,00173); 

This returns:

secc
2807911036
2807911037
2807911038
2807911039
2807911034
2807911035
2807911029
2807911024
2807911023
2807911022
2807911010
2807911011
2807911013

For each secc, i must obtain another valuer from table “t3”. The query y tested in mysql is:

SELECT
Seccs,Total
FROM t3
where cprov=28 and cmun=079 and
Seccs in (2807911036,2807911037,2807911038,2807911039,2807911034,2807911035,2807911029,2807911024,2807911023,2807911022,2807911010,2807911011,2807911013);

And returns:

Seccs       Total   
2807911010  820
2807911011  823
2807911013  1493
2807911022  1538
2807911023  1332
2807911024  1425
2807911029  872
2807911034  1076
2807911035  1217
2807911036  1314
2807911037  1412
2807911038  1114
2807911039  2199

I need help to or make all in only one query and put it in my php file, or how in php from the first results, show in separate tables the other values.

First, i recevice from the first query the codvia. This codvia returns me secc of each via, and with this seccs i obtain Total values.
I’m missing.