全局变量的问题

时间:2010-11-08 21:18:26

标签: php include global-variables

我在PHP中包含页面时遇到问题。图片显示了我想要做的事情。我想在我的index.php页面中包含水平和垂直菜单。但现在我只能包括其中一个。在global.php中有数据库名称,密码和变量,它们定义了我现在使用的语言。 我包含了所有derictives:include,include_once,require,require_once。什么都没有帮助。你有什么建议我做的?感谢名单!

alt text

修改

这是我的代码:

的index.php:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>KUSS</title>
<link href="styles/default.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="main_border">
<?php 
    include_once ("modules/php/mainMenu.php"); 
?>

<? include_once ("modules/php/vertMenu.php"); ?><!--Head-->


</table>
</body>
</html>

global.php

<?php
// All global variables MUST be defines here

//representing current language
$gl_Lang = "UKR";       

//current horizontal menu click
$gl_MaimMenuChoice;     
//current vertical sub menu click
$gl_SubMenuChoice;

$gl_dbName = "127.0.0.1";
$gl_UserName = "user1";
$gl_Password = "12345";
$gl_adminDatabase = "admin";

?>

makeHoriz.php和makeVert.php相同,除了从db读取并显示行和第二列

<?php
function MakeLeftVMenu($tableName, $levelID, $parentName)
{
    include_once ("modules/php/globals.php");

    //connect to db or die)
    $db = mysql_connect($gl_dbName, $gl_UserName, $gl_Password ) or die ("Unable to connect");

    //to prevenr ????? symbols in unicode - utf-8 coding
    mysql_query("SET NAMES 'UTF8'");

    //select database
    mysql_select_db($gl_adminDatabase, $db);
    $sql = "SELECT " .$gl_Lang. ", Link FROM ". $tableName." WHERE LevelID = ".$levelID. " AND ParentName = '". $parentName."'";
    echo $sql;
    //execute SQL-query
    $result = mysql_query($sql, $db);
    //read data to array
    $myRow = mysql_fetch_array($result);
    //print it to screen into the table
    do
    {
        echo "<tr><h3><a href=".trim($myRow['Link']).">". trim($myRow[$gl_Lang]) ."</a></h3></tr>";


    }while($myRow = mysql_fetch_array($result));
    //close database  = very inportant
    mysql_close($db);
}
?>

3 个答案:

答案 0 :(得分:2)

如果horizonal_menu.phpvertical_menu.php都包含global.php,则应确保它们都使用require_once。如果是这种情况,你不应该有任何问题。

答案 1 :(得分:0)

如果所有页面都有水平/垂直菜单,您可能会创建一个包含水平/垂直菜单和global.php的header.php。

答案 2 :(得分:0)

每个菜单页面都可以自己调用吗?例如,您是否曾在浏览器中访问http://mywebsite.com/horizontalmenu.php?如果没有,则可以安全地假设horizo​​ntalmenu.php所需的任何包含文件都可以从以前包含的php文件中获得,或者您可以将require_once用于所有包含。

例如

的index.php

include('global.php');
include('horizontalmenu.php');
include('verticalmenu.php');

horiztontalmenu.php

 //No include statments as it is being included by Index.php which
 //has provided access to all global.php functions and variables.

或者,如果对每个require或include语句使用require_once,则应避免此问题。