0
我在我的數據庫這樣的結構:顯示數據
表certificados - > ID PK,標題,描述
表cert_gal - > ID PK,id_certificados FK,文件
表格證書具有標題和說明,cert_galeria表格包含每個證書的圖像。 cert_galeria表的字段id_certified爲FK。
我想在視圖中顯示所有證書及其各自的庫,請求使用連接的查詢,我有重複證書中的數據,標題和描述必須是唯一的,並顯示相應的圖庫。
我現在有這樣的結果:
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)
我的模型:
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;
}
我的控制器:
class certificadosController extends Controller{
public function index(){
$data = array();
$certificado = new Certificados;
$dados['certificado'] = $certificado->getCertificados();
$this->loadTemplate('certificados', $dados);
}
}
如何顯示是正確的我沒有repea的觀點來自表格證書的數據(標題,描述)?
我認爲你會需要超過1個查詢來實現這一目標。您可以查詢第一個表(certificados),然後對該表上的每一行從第二個表中獲取相關記錄。 – Andreas