php cycles with database

1

I'm having trouble logically perhaps.

Then it's the following:

                            $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'll get the sections or categories and store them in an array, I count it to know the size of it. Then I make the second query that is to get all the images and their respective sections.

Finally, I cycle through the length of the array that I measured using the count, and then do the while in which it will cycle through all of the images received by the second query. I do a check to see if the for section is the same as the while loop or 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 in a <ul> different.

    
asked by anonymous 07.06.2015 / 10:40

1 answer

0
The problem with your code is that you do not save the result of the second query on array , you mysqli_fetch_assoc($resultado) during while and after the first for its result will be empty.

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

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

And then you make 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 1 single 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

    
26.06.2015 / 16:49