如何使用echo与li选择单个当前页面?

时间:2014-05-15 14:00:01

标签: php

我想通过电流从完整菜单中检索单个当前页面 菜单代码:

 <?php
$sql = mysql_query("SELECT * FROM pages WHERE isRoot='1' ORDER BY pageID");
        while ($row = mysql_fetch_object($sql))
        {
            echo "<li class=\"current\"><a href=\"".DIR."?p=$row->pageID\">$row->pageTitle</a></li>"; 
        }
    ?>

预览代码:

?php    
    //if no page clicked on load home page default to it of 1
    if(!isset($_GET['p'])){
        $q = mysql_query("SELECT * FROM pages WHERE pageID='1'");
    } else { //load requested page based on the id
        $id = $_GET['p']; //get the requested id
        $id = mysql_real_escape_string($id); //make it safe for database use
        $q = mysql_query("SELECT * FROM pages WHERE pageID='$id'");
    }

    //get page data from database and create an object
    $r = mysql_fetch_object($q);

    //print the pages content
    echo "<h1>$r->pageTitle</h2>";
    echo $r->pageCont;
    ?>

但是它给了我用当前

标记的li页面

1 个答案:

答案 0 :(得分:0)

我想我理解你的意思......我会尝试这样的事情:

<?php
$sql = mysql_query("SELECT * FROM pages WHERE isRoot='1' ORDER BY pageID");
    while ($row = mysql_fetch_object($sql))
    {
       $active = '';
       if($_SERVER['REQUEST_URI'] == DIR . '?p=' . $row->pageID)
           $active = 'class="current"';
       echo "<li " . $active . "><a href=\"".DIR."?p=$row->pageID\">$row->pageTitle</a></li>"; 
    }
?>

这样,您只会添加&#39; class =&#34; current&#34;&#39;如果用户实际上在此页面上。

希望这有帮助!