php cycles with database

Asked

Viewed 109 times

1

I’m having trouble with logic maybe.

So here’s the thing:

                            $secao="select distinct seccao from foto";
                            $manda=  mysqli_query($ligacao, $secao);
                            $array_sec=array();
                            while (($recebe=  mysqli_fetch_assoc($manda))!=null){
                                array_push($array_sec, $recebe["seccao"]);
                            }
                            $re=count($array_sec);


$sql = "select imagem, seccao from foto order by seccao desc";
                            $resultado = mysqli_query($ligacao, $sql);

                            for($i=0; $i<$re; $i++){
                                echo '<ul>';
                                while (($registo = mysqli_fetch_assoc($resultado)) != null) {
                                    if($registo["seccao"]==$array_sec[$i]){ 
                                        echo "<li><a href=\"img/galeria/".$registo["seccao"]."/" . $registo["imagem"] . "\" data-smoothzoom=\"group1\"><img src=\"img/galeria/".$registo["seccao"]."/" . $registo["imagem"] . "\" width='188' height='119'></a></li>";
                                     }   
                                }
                                echo '</ul>';
                            }

In the first query I will fetch the sections or categories and store them in an array, do Count to know the size of it. Then I do the second query that is to fetch all the images and their respective sections.

Finally I do a cycle where I go through the size of the array I measured using Count and then do the while where I will go through all the images received by the second query. I do a check to see if the for section equals the while cycle section or the image section, the problem here is that the first <ul> does well, but then the rest does not.

The goal was to get all the images of each section into one <ul> different.

  • Felipe, what’s the mistake? The query works?

1 answer

0


The problem with your code is you don’t keep it in a array the result of the second query, you do the mysqli_fetch_assoc($resultado) during the while and after that ends the first for your $result will be empty.

To avoid this is only you save in an array similar to the first:

$array_segunda_query = array();
while ($array = mysql_fetch_row($resultado)) {
    $array_segunda_query[] = $array;
}

And then you do TWO for according to the size of each

for($i=0; $i<count($array_sec); $i++){
    for($j=0; $j<$count($array_segunda_query); $j++){
         //seu codigo
    }
}

But I believe you can do everything in one SELECT and for.

$sql = "select imagem, seccao from foto order by seccao desc";
$resultado = mysqli_query($ligacao, $sql);

$atualUl = "";
while (($registo = mysqli_fetch_assoc($resultado)) != null) {

    // precisa criar nova tag
    if ($registo["seccao"] != $atualUl) {

        // precisa fechar a tag anterior
        if ($atualUl != "") {
            echo '</ul>';
        }

        // criando a tag
        echo '<ul>';

        // atualiza o atual
        $atualUl = $registo["seccao"];
    }
    echo "<li><a href=\"img/galeria/".$registo["seccao"]."/" . $registo["imagem"] . "\" data-smoothzoom=\"group1\"><img src=\"img/galeria/".$registo["seccao"]."/" . $registo["imagem"] . "\" width='188' height='119'></a></li>";

}
// fecha ultima tag
echo '</ul>';

Ideone example

Browser other questions tagged

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