Pull several images of the same id in the comic

Asked

Viewed 163 times

3

I’m having a hard time pulling to the front-end 4 images of the same id I have in the database.

I was able to normally insert the images into the database, I’m using two separate tables.

Follow the database below the first:

Tabela: produtos
id  nome       descricao             slug
1  produto um  descricao do produto  produto-um

Below is the second:

tabela:produto_imagens
id  produto_id  imagens
1      1        hsaudhuahdsu.jpg
2      1        gsagaasftfsa.jpg
3      1        assdoakdaado.jpg

follows the php code below:

<?php
$url = explode('/',$_GET['url']);
$produto = MySql::run()->prepare("SELECT * FROM `produto` WHERE slug = ?");
$produto->execute(array($url[0]));// código para ver se a url existe.
if($profile->rowCount() == 0){
die("a página não existe!");

}
foreach ($produto as $key => $value) {
$produto->fetchAll();
$imagens = MySql::run()->prepare("SELECT * FROM 
`produto_imagens` WHERE produto_id = $value[id]");
$imagens->execute();

?>
// nao coloquei as tags do php pq se nao o 
//codigo nao aparece para vcs.

<h2>Nome do produto:  echo $value['nome'];<h2>
<h3>Descrição do produto</h3>
<p>echo $value['descricao']; </p>
<?php }?>//fecha foreach"produto".

<?php
foreach ($imagens as $key => $value) {
$imagens = $imagens->fetch()['imagem'];

?>
<img src='echo $imagens'>   
<?php } ?>//fecha foreach $imagens.

Thus, I can recover all od data from the table "products", and from the table "producto_images" I can recover only the first, and if I replicate the code it returns the same image. Does anyone know how to pull from the database all images of the same products table id and show to the user on front-end?

  • Wouldn’t it be interesting to just save the path of the images in the bank and the images on the disk? Large applications do so to facilitate maintenance and reduce seat size, giving faster

1 answer

2

You are overwriting the variable images.

foreach ($imagens as $key => $value) {
    $imagens = $imagens->fetch()['imagem']; // <====== AQUI
                                            // Já existe a variável $imagens

So this showing only 1 image, as you are always overwriting it with the name of an image, inside the foreach.

You are overwriting the variable images.

Another thing I found in your code that I found strange was this part:

<img src='echo $imagens'>   

You are running a echo without the PHP tags before.

Corrected code

foreach ($imagens as $key => $value) { ?>
    <img src="<?php echo $value ?>">
<?php }
  • Even worse, you can’t even know what is happening because there is no functional code in the question (opens { but does not close)

  • Also, it’s really hard to understand.

  • However, your observation is valid, so upei.

  • So...I took out the php tags and the keys...I ended up forgetting some, because I was not able to post on the forum with the tags... I will try to edit.

  • Patrick Maciel, I took ['image'], I put it like this: <img src="<? php echo $value['image']? >" />, and returned only one image. and instead of fetch()- gave error with fetch-, put fetchAll()

  • @Leandro put the correction in the answer. Test and let me know please.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.