0

我正在使用 php 代码:

<body id="<?php echo str_replace("index.php?","",(basename($_SERVER['REQUEST_URI'],".php"))); ?>">

<?php
    if(isset($_GET['start'])){
     include('includes/start.php');
    }else if(isset($_GET['help'])){
     include('includes/help.php');
    }else{
     include('includes/start.php');
    }
?>

它工作得很好——在正文 ID 中将 index.php?help 切割为“帮助”,将 index.php?start 切割为“start”。但是当我在正文中输入 index.php 时,ID 是“index”而不是“start”。有没有办法告诉 index.php 包含 start.php 显示“开始”id body ID?

更新

它应该动态工作 - 正文 ID 是包含的 .php 文件的名称,类似于以下代码:

<?php 
   $page = str_replace(array( 'server_name', 'index', '?',  '/', '.php'), '', $_SERVER['REQUEST_URI']);
   $page = $page ? $page : 'start';
?>

<body id="<?php echo $page ?>">
4

2 回答 2

0

我不确定我能得到你 100% 但你可以试试

$id = $_SERVER['QUERY_STRING'];
$bodyID = $id;
switch ($id) {
    case "help" :

        include ('includes/help.php');
        break;
    default :
        $bodyID = "start";
        include ('includes/start.php');
        break;
}

printf("<body id=\"%s\"  >", $bodyID);

如果您运行index.php?help它将包括include ('includes/help.php');并输出

<body id="help"  >
于 2012-10-13T12:36:10.653 回答
0

最好改用这个:

<?php
    $ids = 'start,help,something';
    $ids = explode(',',$ids);
    $included = 0;
    foreach ($ids as $id) {
        if (isset($_GET[$id])) {
            include('includes/'.$id.'.php');
            $included = 1;
            break;
        }
    }
    if (!$included) include('includes/'.$ids[0].'.php');
?>

然后:

<body id="<?php echo $id; ?>">
于 2012-10-13T12:36:50.877 回答