SQL结果为空时的不同子菜单

时间:2016-02-13 20:27:03

标签: php html

我有一个包含不同子菜单的菜单,如果SQL结果为空,我想显示不同的特定子子菜单。

如果该数据库中存在用户名(会话用户名),则会输出名字和姓氏,因为$result不为空。但是,如果我登录到该数据库中没有用户名的帐户,则不会输出<li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>

它只是输出:

                <li class='active has-sub'><a href=""><span>Sub1</span></a>
                    <ul>
                                            </li>
                    </ul>

应该是:

            <li class='active has-sub'><a href=""><span>Sub1</span></a>
                <ul>
                    <li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>
                </ul>

为什么它是空的?我做错了什么?

<form action="user.php" method="GET">
<div id="cssmenu">
    <ul>
        <li class='active has-sub'><a href='#'><span><?php echo htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?></span></a>
            <ul>
                <tr>
                    <li class='last'><a href=""><span>Menu1</span></a></li>
                    <li class='last'><a href=""><span>Menu2</span></a></li>
                    <li class='active has-sub'><a href=""><span>Menu3</span></a>
                        <ul>
                        <li class='active has-sub'><a href=""><span>Sub1</span></a>
                            <ul>
                            <?php
                            require('connections/db_connection.php');
                            $username = $_SESSION['username'];


                            $sql = "SELECT name, lastname FROM servers";
                            $sql .= " WHERE username = '$username'";

                            $result = $connection->query($sql);

                            while($row = $result->fetch_assoc()){
                                $name = $row['name'];
                                $lastname = $row['lastname'];
                                if (empty($result)){
                                ?>
                                        <li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>
                                <?php
                                }else{
                                ?>
                                        <li class='last'><a href=""><span>SubSub1: <?php echo $name; ?><br>
                                        Last name: <?php echo $lastname; ?></span></a></li>
                                        <li class='last'><a href=""><span>SubSub2</span></a></li>
                                    <?php
                                }

                            }

                            $connection->close();
                            ?>
                        </li>
                            </ul>
                        <li class='last'><a href=""><span>Sub2</span></a></li>
                        <li class='last'><a href=""><span>Sub3</span></a></li>
                    </li>
                    </ul>
                    <li class='last'><a href="logout.php"><span>Logout</span></a></li>
                </tr>   
            </ul>
        </li>
    </ul>
</div>
</form>

1 个答案:

答案 0 :(得分:2)

如果您的$result为空 - 则永远不会输入

while($row = $result->fetch_assoc())

因为没有什么可以取的。

所以我想你应该检查返回的行数。如果在您的数据库中找到用户,则编号为1,否则为0。 所以代码可以是这样的(我想你使用mylsqi):

$result = $connection->query($sql);
if (0 < $result->num_rows) {
    $row = $result->fetch_assoc(); // you can fetch only one result if you're sure that there's only one user;
    $name = $row['name'];
    $lastname = $row['lastname'];?>
    <li class='last'><a href="">
        <span>SubSub1: <?php echo $name; ?><br>
        Last name: <?php echo $lastname; ?>
        </span>
    </a></li>
    <li class='last'><a href=""><span>SubSub2</span></a></li>
<?php
} else {
     // no user found:?>
     <li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>
<?php
}

顺便说一下:

    </li>
</ul>

在您</li>中已关闭li时删除此if