Problem listing all images

Asked

Viewed 36 times

1

Good people and the following I have a photo upload that is working well, I’m just trying the problem in displaying all the photos through the for, an image always appears only needs your own to discover where the problem is.

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="">';
}

In the HTML where you should display all the images is so it gets inside an array that is then returned via ajax to list on the page.

    <div style="float:left; margin:0px 5px 10px 0px;">'.$fotos_post.'</div>

1 answer

0


You are always rewriting the same variable, start it and concatenate there later you print, follows below an example.

// 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 use!! Search for PDO.

About for.. , it is easier for you to use while for this, follow 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.

  • That’s right settled thanks for the help after all it was a simple thing, mysql_query it is no longer recommended to be used because you can explain to me ?

  • The PHP community will stop supporting it, in future versions of PHP your website may no longer work.

  • The correct term is deprecated.

  • already this marked so I will have to research on PDO as it is the future

  • That’s right, evolving is part of it!!! As you will be studying, look for Design Partner in PHP object orientation, and use of the Smarty class to separate HTML from your business rule.

  • But PDO changes the programming structure a lot ? I want to know to be prepared if I will have to change a lot on the site ?

  • It changes a little, but it’s nothing complex.

Show 3 more comments

Browser other questions tagged

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