Dynamic menu with PHP

Asked

Viewed 194 times

0

The menu is practically ready, in the last part I am facing a problem with the foreach, as shown on the right side of the image (the menu on the left is still the mechanical menu).

inserir a descrição da imagem aqui

For the categories, first I will need their id, so far so good. The problem is the foreach is repeating the ids:

<?php
$sql_2_a = mysqli_query($config, "SELECT DISTINCT menu FROM recursos ORDER BY menu ASC") or die(mysqli_error($config));
if(@mysqli_num_rows($sql_2_a) <= '0'){
    echo "
        <div class=\"row\">
            <div class=\"col-lg-6\">
                <div class=\"alert alert-danger\">
                    <strong>Erro!</strong> $erro
                </div>
            </div>      
        </div>
        ";
    }else{
        while($r_sql_2_a = mysqli_fetch_array($sql_2_a)){
            $id_menu_sel = $r_sql_2_a[0];

            $sql_3 = mysqli_query($config, "SELECT url FROM recursos WHERE menu = '$id_menu_sel'") or die(mysqli_error($config));
            if(@mysqli_num_rows($sql_3) <= '0'){
                echo "
                    <div class=\"row\">
                        <div class=\"col-lg-6\">
                            <div class=\"alert alert-danger\">
                                <strong>Erro!</strong> $erro
                            </div>
                        </div>      
                    </div>
                    ";
                }else{
                    $p = Array();
                    while($r_sql_3 = mysqli_fetch_array($sql_3)){
                        $p[] = $r_sql_3[0];

                    }
                }



                foreach ($p as $key => $value) {
                    if($value==$pagina_link){
                        echo "<b>".$id_menu_sel."</b>";
                    }else{
                        echo $id_menu_sel;
                    }
                }
            }
        }
?>

What you’re repeating is this foreach ($p as $key => $value), I tried to change the beginning of the loop, but nothing.

  • Why are you making 2 selects in the same table? takes the menu and the url in the same sql,

  • I did that and it got better. There’s still a point I need to adjust. I can delete the explanation above and put a new version or create another topic?

1 answer

-1

From what I understand there, this repeating because you are passing everything to while, and then traversing again with foreach, implement the foreach straight from mysqli_fetch_array because it already generates a traversable array for you. I think it should work!

  • This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication. - Of Revision

  • It was not critical, it was a response, to use instead of while to use only foreach

  • I tried, but it increased even more the repeated numbers.

  • I made the menu without being dynamic, by the deadline I have to do. After the customer starts using in parallel, I will return to work on this dynamic menu. I thank you very much for the time.

Browser other questions tagged

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