[英]Show data from two related tables
I have this structure in my database: 我的数据库中有以下结构:
table certificados -> id PK, title, description 表证书-> ID PK,标题,说明
table cert_gal ->id PK, id_certificados FK, file 表cert_gal-> id PK,id_certificados FK,文件
The table certificates have title and description, the cert_galeria table, contains the images of each certificate. 表证书具有标题和描述,cert_galeria表包含每个证书的图像。 The cert_galeria table has the field id_certified as FK.
cert_galeria表的字段id_certified为FK。
I want to display all the certificates and their respective gallery in the view, asking the query using join, I have the data in duplicate certificates, the title and description must be unique and show the corresponding image gallery. 我想在视图中显示所有证书及其各自的库,使用join进行查询,我的数据存在重复的证书,标题和描述必须唯一,并显示相应的图像库。
I currently have this result: 我目前有这个结果:
2 =>
array (size=6)
'titulo' => string 'teste' (length=5)
0 => string 'teste' (length=5)
'descricao' => string 'teste' (length=5)
1 => string 'teste' (length=5)
'arquivo' => string 'c45e41a73c02356cc3370caea4e37358.png' (length=36)
2 => string 'c45e41a73c02356cc3370caea4e37358.png'
3 =>
array (size=6)
'titulo' => string 'teste' (length=5)
0 => string 'teste' (length=5)
'descricao' => string 'teste' (length=5)
1 => string 'teste' (length=5)
'arquivo' => string 'c29ecd046c6d90eb23eebd6d95ab3cff.png' (length=36)
2 => string 'c29ecd046c6d90eb23eebd6d95ab3cff.png' (length=36)
4 =>
array (size=6)
'titulo' => string 'teste' (length=5)
0 => string 'teste' (length=5)
'descricao' => string 'teste' (length=5)
1 => string 'teste' (length=5)
'arquivo' => string '471ed3267f3e342029523269083c3277.png' (length=36)
2 => string '471ed3267f3e342029523269083c3277.png' (length=36)
my model: 我的模特:
public function getCertificados(){
$sql = "SELECT certificados.titulo, certificados.descricao, cert_galeria.arquivo FROM certificados LEFT JOIN cert_galeria ON certificados.id = cert_galeria.id_certificado";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()>0):
$dados = $stmt->fetchAll();
endif;
return $dados;
var_dump($dados); die;
}
my controller: 我的控制器:
class certificadosController extends Controller{
public function index(){
$data = array();
$certificado = new Certificados;
$dados['certificado'] = $certificado->getCertificados();
$this->loadTemplate('certificados', $dados);
}
} }
How can I display is correctly in my view without repeating the data (title, description) coming from the table certificates? 如何在视图中正确显示而不重复来自表证书的数据(标题,描述)?
Solved the problem. 解决了问题。
Models: 楷模:
public function getCertificados(){
$sql="SELECT id_certificado, titulo, descricao FROM certificados";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()>0):
$dados = $stmt->fetchAll();
endif;
return $dados;
}
public function getImagesGal($id){
$sql = "SELECT cert_galeria.id_certificado, cert_galeria.arquivo FROM cert_galeria right JOIN certificados ON cert_galeria.id_certificado = :id GROUP BY cert_galeria.arquivo";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(":id", $id);
$stmt->execute();
if($stmt->rowCount()>0):
$dados = $stmt->fetchAll();
endif;
return $dados;
}
Controller: 控制器:
class certificadosController extends Controller{
public function index(){
$dados = array();
$certificados = new Certificados();
$dadosCert = $certificados->getCertificados();
$lista = null;
foreach ($dadosCert as $dadosC):
$lista[$dadosC['titulo']] = ["id_certificado" => $certificados->getImagesGal($dadosC['id_certificado']), 'desc' => $dadosC['descricao']];
endforeach;
$dados['certificados'] = $lista;
$this->loadTemplate('certificados', $dados);
}
}
View: 视图:
<?php foreach ($certificados as $cert => $value): ?>
<h2><?php echo $cert;?></h2>
<?php echo $value['desc']; ?>
<?php foreach($value['id_certificado'] as $arquivo): ?>
<?php echo $arquivo['arquivo']; ?>
<?php endforeach; ?>
<?php endforeach; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.