全局变量的问题
我在 PHP 中包含页面时遇到问题。图片显示了我想做的事情。我想在我的 index.php 页面中包含水平和垂直菜单。但现在我只能包括其中之一。在 global.php 中有数据库名称、密码和定义我现在使用的语言的变量。 我包含了所有衍生词:include、include_once、require、require_once。没有任何帮助。你能建议我做什么?谢谢!
编辑:
这是我的代码:
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 相同,除了从数据库读取并显示行和第二列
<?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);
}
?>
I have a problem with include pages in PHP. Picture shows what I want to do. I want to include in my index.php page horizontal and vertical menu. But now I can include only one of them. In global.php there are database name, password, and variable which define what language I'm using now.
I included with all derictives: include, include_once, require, require_once. Nothing helps. What can you propose me to do? Thanx!
EDIT:
Here is my code:
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 and makeVert.php identical except one read from db and shows rows and second cols
<?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);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果所有页面都有水平/垂直菜单,您可能会创建一个包含水平/垂直菜单和 global.php 的 header.php。
If all pages are going to have a horizontal/vertical menu you might go to the extent of creating a header.php that includes horizontal/vertical menu and the global.php.
菜单页面是否都会被单独调用?例如,您是否曾在浏览器中访问 http://mywebsite.com/horizontalmenu.php ?如果没有,那么可以安全地假设horizontalmenu.php所需的任何包含文件都可以从先前包含的php文件中获得,或者您可以对所有包含使用require_once。
例如
Index.php
horiztontalmenu.php
或者,如果您对每个 require 或 include 语句都使用 require_once ,则应该避免该问题。
Do the Menu pages every get called on their own? eg do you ever go in the browser to http://mywebsite.com/horizontalmenu.php ? If not then it is safe to assume that any included files needed by horizontalmenu.php will be available from a previously included php file, or you can use require_once for all includes.
Eg
Index.php
horiztontalmenu.php
Alternatively if you use require_once for every single require or include statement you should avoid the problem.
如果
horizonal_menu.php
和vertical_menu.php
都包含global.php
,您应该确保它们都使用require_once< /代码>。如果是这样的话,你应该不会有任何问题。
if
horizonal_menu.php
andvertical_menu.php
are both includingglobal.php
, you should make sure both of them are usingrequire_once
. If that is the case, you shouldn't have any problems.