Dúvida em array com id no banco em php

10/01/2023

0

Olá pessoal,

Abaixo uma parte do código que estou em dúvida,
Eu fiz uma pesquisa em php pra me trazer informações de um sistema de cursos, ai preciso que venha no td abaixo a informação do municipio, só que está vindo a id (numero) dele e não o nome, como faço para vir o nome dele, ja que tenho o banco de dados das ids e nomes de cada municipio....

o banco dos municipios é diferente do atual abaixo (este atual pega o cpf por exemplo)

o outro bd chama "municipios" que contem as colunas "id" e "municipio"

try {
								$con = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
								$con->exec("SET CHARACTER SET utf8");      
								
								$sql = "SELECT * FROM `inscricao` WHERE `ins_curso` = 13 AND `ins_situacao` = 1 ORDER BY `ins_nome` ASC";
								$result = $con->query($sql);
													
								
								$html_table = '
								<table id="myTable" class="table table-sm table-responsive">
								<thead>
								<tr>
                                                                <th style="font-size:15px;text-align:center;">CPF</th>
								<th style="font-size:15px;text-align:center;">Municipio</th>
								</tr>
								</thead>';

								foreach($result as $row) {
									$html_table .= '
									<tr>
                                                                        <td style="font-size:15px;">' .$row['ins_cpf']. '</td>
									<td style="font-size:15px;">' .$row['ins_municipio']. '</td>										
									</td>										
									</tr>';
Responder

Post mais votado

10/01/2023

Não sei como editar a mensagem anterior, assim estou retificando o código:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css"
rel="stylesheet" 
integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" 
crossorigin="anonymous">
<?php
try {
$con = new PDO("mysql:host=localhost;dbname=astudy","root","");
$con->exec("SET CHARACTER SET utf8");
$sql = "SELECT * FROM inscricao 
JOIN municipio on inscricao.ins_municipio = municipio.ins_municipio
WHERE ins_curso = 13 AND ins_situacao = 1 ORDER BY ins_nome ASC";
$result = $con->query($sql);
$html_table = "
<table id='myTable' class='table table-sm table-responsive'>
<thead>
<tr>
<th style='font-size:15px;text-align:left;'>CPF</th>
<th style='font-size:15px;text-align:left;'>Municipio</th>
</thead>";
foreach($result as $row) {
$html_table .= "
<tr>
<td style='font-size:15px;'>" .$row['ins_cpf']. "</td>
<td style='font-size:15px;'>" .$row['nome_municipio']. "</td> 
</td></tr>";}
$html_table .= "</table></div>";
echo "<div class='w-50 m-auto'>$html_table</div>";

}
catch (exception $e){echo "problema erro $e";}


Frank Hosaka

Frank Hosaka
Responder

Mais Posts

10/01/2023

Frank Hosaka

Supondo que além da tabela inscricao temos uma tabela municipio com os campos (ins_municipio e nome_municipio), o codigo ficaria assim


<?php
try {
$con = new PDO("mysql:host=localhost;dbname=astudy","root","");
$con->exec("SET CHARACTER SET utf8");
$sql = "SELECT * FROM inscricao
JOIN municipio on inscricao.ins_municipio = municipio.ins_municipio
WHERE ins_curso = 13 AND ins_situacao = 1 ORDER BY ins_nome ASC";
$result = $con->query($sql);
$html_table = "
<table id=''myTable'' class=''table table-sm table-responsive''>
<thead>
<tr>
<th style=''font-size:15px;text-align:center;''>CPF</th>
<th style=''font-size:15px;text-align:center;''>Municipio</th>
</thead>";
foreach($result as $row) {
$html_table .= "
<tr>
<td style=''font-size:15px;''>" .$row[''ins_cpf'']. "</td>
<td style=''font-size:15px;''>" .$row[''nome_municipio'']. "</td>
</td></tr>";}
echo $html_table;

}
catch (exception $e){echo "problema erro $e";}


Responder

11/01/2023

Jim

Olá Frank, valeu amigo, deu certo aqui!
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar