You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

测验系统开发问题:管理员身份下无法echo出添加问题下拉菜单

Hey there! Let's troubleshoot why your "Add questions" dropdown isn't working as expected when echoing that HTML snippet for admin users. Here are the most common fixes to try:

1. First, Verify Your Admin Check Logic

Before worrying about the dropdown itself, make sure your code is actually triggering the echo for admin users. Add a quick test line to confirm:

if ($is_admin) { // Replace with your actual admin check (e.g., $_SESSION['role'] === 'admin')
    echo "Admin status confirmed!"; // Test line
    echo '<ul><li><a href="admin.php">Add questions</a></li></ul>';
}

If you don't see the test message when logged in as admin, your role-check condition is likely reversed or incorrect. Double-check that you're targeting the right user role value.

2. Fix the Dropdown HTML Structure

A standalone <ul> won't act as a dropdown on its own—you need the full component structure, especially if you're using a framework like Bootstrap, or custom CSS for a vanilla dropdown:

Example with Bootstrap (most common):

if ($is_admin) {
    echo '
    <div class="dropdown">
        <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
            Admin Options
        </button>
        <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="admin.php">Add questions</a></li>
        </ul>
    </div>
    ';
}

Make sure you've included Bootstrap's CSS and JS files in your page header/footer—without them, the dropdown toggle won't work.

Example for Vanilla Custom Dropdown:

If you're not using a framework, add basic CSS to make the dropdown functional:

/* Add this to your stylesheet */
.dropdown-container {
    position: relative;
    display: inline-block;
}
.dropdown-menu {
    display: none;
    position: absolute;
    background: #fff;
    border: 1px solid #ddd;
    padding: 8px 0;
    min-width: 150px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.dropdown-container:hover .dropdown-menu {
    display: block;
}
.dropdown-menu li {
    list-style: none;
}
.dropdown-menu a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
    color: #333;
}

Then echo the matching HTML:

if ($is_admin) {
    echo '
    <div class="dropdown-container">
        <span>Admin Options ▾</span>
        <ul class="dropdown-menu">
            <li><a href="admin.php">Add questions</a></li>
        </ul>
    </div>
    ';
}

3. Check for PHP Echo Syntax Errors

Make sure you're handling quotes correctly in your echo statement. Mismatched quotes will break the HTML output:

  • Correct: Use single quotes for the outer echo wrapper to avoid escaping inner double quotes:
    echo '<ul><li><a href="admin.php">Add questions</a></li></ul>';
    
  • Avoid: Unescaped double quotes inside a double-quoted echo:
    echo "<ul><li><a href="admin.php">Add questions</a></li></ul>"; // This will throw an error!
    

4. Inspect the Page Source

Right-click your page and select "View Page Source" to check if the dropdown HTML is actually being rendered. If it's missing, go back to your admin role check. If it's present but not showing up, your CSS is hiding it (check for display: none without a trigger to show it).

内容的提问来源于stack exchange,提问作者Zestyy99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:05:52