让 Superfish jQuery 插件与 PHP 中的多个循环一起动态工作

发布于 2024-09-19 08:43:30 字数 2622 浏览 10 评论 0原文

希望一些聪明的小伙子或小伙子可以帮助我弄清楚以下代码的逻辑。

我已经开始使用 superfish jquery 插件进行导航,并且使用静态链接使代码工作正常且轻松,但是当我尝试将其更改为动态链接时,我无法让第二级标签正常工作;仅在找到二级链接时显示,然后在从数据库中写出二级链接之前仅显示一次。

我希望我已经把问题说清楚了。如果我没有的话,抱歉。

<ul class="sf-menu">
        <?php
            // A varible that increments on every loop of the below "while" statement
            $count == 0;

            $result = mysql_query("SELECT * FROM web_navbar_links WHERE link_parent='1' AND visible='1' ORDER BY position ASC");
            while ($row = mysql_fetch_object($result)) {
                $parentID    = $row->id;
                $parentLevel = $row->slug;
                // Increment the counter varible by one
                $count++;

                echo "<li";
                    switch ($parentLevel) {
                        case "business":
                            echo " id='business'";
                            break;

                        case "education":
                            echo " id='education'";
                            break;

                        case "consumer":
                            echo " id='consumer'";
                            break;
                    }
                    echo "><a>".ucwords(trim($row->link_name))."</a>";

                    $result2 = mysql_query("SELECT * FROM web_navbar_links WHERE link_child='1' AND parent_relationID='".$parentID."' AND visible='1' ORDER BY position ASC");
                    if ($row2 = mysql_fetch_object($result2)) {
                        echo "<ul>"; //PROBLEM LIES HERE!! ATM THE <UL> WRITES ONCE BUT THE LOOP ONLY READS BACK THE LAST ENTRY FROMT HE db.
                        while ($row2 = mysql_fetch_object($result2)) {
                            echo "<li><a href='http://". ROOT . ADMIN . SECTIONS . TEMPLATES . $row2->link_href."?".$row2->slug."=".$row2->slug."' title='".$row2->link_title."'>".$row2->link_name."</a>";
                                echo "<ul>";
                                    echo "<li><a href='#'>blah</a></li>";
                                echo "</ul>";
                            echo "</li>";
                        }
                        echo "</ul>";
                    }
                echo "</li>";

                if ($count < 3) {
                    echo "<li class='breaker'></li>";
                }
                else {
                    echo "";
                }
            }
        ?>
    </ul>

Hopefully some clever lad or ladette can help me figure out the logic for the follwoing code.

I have started to use superfish jquery plugin for my navigation and have got the code working fine and easily with static links, but when I try to change that to dynamic links I am having trouble getting the 2nd level tag to work properly; to only show when 2nd level links are found and then only show once before the 2nd level links are written out from the database.

I hope I have made myself clear with the problem. Appologies if I havn't.

<ul class="sf-menu">
        <?php
            // A varible that increments on every loop of the below "while" statement
            $count == 0;

            $result = mysql_query("SELECT * FROM web_navbar_links WHERE link_parent='1' AND visible='1' ORDER BY position ASC");
            while ($row = mysql_fetch_object($result)) {
                $parentID    = $row->id;
                $parentLevel = $row->slug;
                // Increment the counter varible by one
                $count++;

                echo "<li";
                    switch ($parentLevel) {
                        case "business":
                            echo " id='business'";
                            break;

                        case "education":
                            echo " id='education'";
                            break;

                        case "consumer":
                            echo " id='consumer'";
                            break;
                    }
                    echo "><a>".ucwords(trim($row->link_name))."</a>";

                    $result2 = mysql_query("SELECT * FROM web_navbar_links WHERE link_child='1' AND parent_relationID='".$parentID."' AND visible='1' ORDER BY position ASC");
                    if ($row2 = mysql_fetch_object($result2)) {
                        echo "<ul>"; //PROBLEM LIES HERE!! ATM THE <UL> WRITES ONCE BUT THE LOOP ONLY READS BACK THE LAST ENTRY FROMT HE db.
                        while ($row2 = mysql_fetch_object($result2)) {
                            echo "<li><a href='http://". ROOT . ADMIN . SECTIONS . TEMPLATES . $row2->link_href."?".$row2->slug."=".$row2->slug."' title='".$row2->link_title."'>".$row2->link_name."</a>";
                                echo "<ul>";
                                    echo "<li><a href='#'>blah</a></li>";
                                echo "</ul>";
                            echo "</li>";
                        }
                        echo "</ul>";
                    }
                echo "</li>";

                if ($count < 3) {
                    echo "<li class='breaker'></li>";
                }
                else {
                    echo "";
                }
            }
        ?>
    </ul>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-09-26 08:43:30

我认为问题在于这一行:

if ($row2 = mysql_fetch_object($result2)) {

这会将记录计数器向前移动,因此下次您调用:

while ($row2 = mysql_fetch_object($result2)) {

$row2 将填充第二行。由于您没有在两条突出显示的行之间的任何位置使用第一行,因此您将丢失它。

我建议调用 mysql_num_rows($result2) 来获取返回的行数,而不是 if 语句中的第一个 mysql_fetch_object($result2) 。

                $result2 = mysql_query("SELECT * FROM web_navbar_links WHERE link_child='1' AND parent_relationID='".$parentID."' AND visible='1' ORDER BY position ASC");
                if (mysql_num_rows($result2) > 0) {
                    echo "<ul>"; //PROBLEM LIES HERE!! ATM THE <UL> WRITES ONCE BUT THE LOOP ONLY READS BACK THE LAST ENTRY FROMT HE db.
                    while ($row2 = mysql_fetch_object($result2)) {
                        echo "<li><a href='http://". ROOT . ADMIN . SECTIONS . TEMPLATES . $row2->link_href."?".$row2->slug."=".$row2->slug."' title='".$row2->link_title."'>".$row2->link_name."</a>";
                            echo "<ul>";
                                echo "<li><a href='#'>blah</a></li>";
                            echo "</ul>";
                        echo "</li>";
                    }
                    echo "</ul>";
                }

I think the problem lies with this line:

if ($row2 = mysql_fetch_object($result2)) {

This will move the record counter forwards, so the next time you call:

while ($row2 = mysql_fetch_object($result2)) {

$row2 will have been populated with the second row. Since you are not using the first row anywhere between the 2 highlighted lines, you will lose it.

I suggest calling mysql_num_rows($result2) to get the number of rows returned instead of the first mysql_fetch_object($result2) in the if statement.

                $result2 = mysql_query("SELECT * FROM web_navbar_links WHERE link_child='1' AND parent_relationID='".$parentID."' AND visible='1' ORDER BY position ASC");
                if (mysql_num_rows($result2) > 0) {
                    echo "<ul>"; //PROBLEM LIES HERE!! ATM THE <UL> WRITES ONCE BUT THE LOOP ONLY READS BACK THE LAST ENTRY FROMT HE db.
                    while ($row2 = mysql_fetch_object($result2)) {
                        echo "<li><a href='http://". ROOT . ADMIN . SECTIONS . TEMPLATES . $row2->link_href."?".$row2->slug."=".$row2->slug."' title='".$row2->link_title."'>".$row2->link_name."</a>";
                            echo "<ul>";
                                echo "<li><a href='#'>blah</a></li>";
                            echo "</ul>";
                        echo "</li>";
                    }
                    echo "</ul>";
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文