Problem listing all images

1

Good people and the following I have a photo upload that is working fine, I'm just trying to see the problem in displaying all the photos through for , there always appears an image just needs yours to find out where the problem.

Code

// MOSTRA FOTOS DO POST
$result_anexo_post = mysql_query("SELECT * FROM posts_anexos WHERE post_id = '".$row_posts->id_post."' AND seccao='fotos_posts'");
$num_fotos = mysql_num_rows($result_anexo_post);

for ($i=0;$i<$num_fotos;$i++) {
    $row_anexo_post = mysql_fetch_assoc($result_anexo_post);
    $fotos_post = '<img src="../php/timthumb.php?src=gtm/anexos/posts_fotos/'.$row_anexo_post['id_anexo'].'.'.$row_anexo_post['tipo'].'&h=100&w=100&zc=1" alt="">';
}

No% w / w where to display all images is as soon as it is inside an array which is then returned via ajax to list on the page.

    <div style="float:left; margin:0px 5px 10px 0px;">'.$fotos_post.'</div>
    
asked by anonymous 06.03.2015 / 00:15

1 answer

0

You are always rewriting the same variable, start it and go concatenating it after you print, here's an example below.

// MOSTRA FOTOS DO POST
$result_anexo_post = mysql_query("SELECT * FROM posts_anexos WHERE post_id = '".$row_posts->id_post."' AND seccao='fotos_posts'");
$num_fotos = mysql_num_rows($result_anexo_post);

$fotos_post = ''; // Inicializo a váriavel
for ($i=0;$i<$num_fotos;$i++) {
    $row_anexo_post = mysql_fetch_assoc($result_anexo_post);
    //Faço a concatenação usando o .
    $fotos_post .= '<img src="../php/timthumb.php?src=gtm/anexos/posts_fotos/'.$row_anexo_post['id_anexo'].'.'.$row_anexo_post['tipo'].'&h=100&w=100&zc=1" alt="">';
}
  

Note: mysql_query is no longer recommended to be used !! Look for the PDO.

On the for .., it's easier for you to use the while for this, see example below:

$fotos_post = ''; // Inicializo a váriavel
while ($row_anexo_post = mysql_fetch_assoc($result_anexo_post)) {
    //Faço a concatenação usando o .
    $fotos_post .= '<img src="../php/timthumb.php?src=gtm/anexos/posts_fotos/'.$row_anexo_post['id_anexo'].'.'.$row_anexo_post['tipo'].'&h=100&w=100&zc=1" alt="">';
}

Then just print the contents of the variable as you did in the div.

    
06.03.2015 / 00:49