全局变量的问题 [英] problems with global variables
问题描述
我在PHP中包含页面时遇到问题。图片显示了我想要做的事情。我想在我的index.php页面中包含水平和垂直菜单。但现在我只能包括其中一个。在global.php中有数据库名称,密码和变量,它们定义了我现在使用的语言。
我包含所有derictives:include,include_once,require,require_once。没什么帮助。你可以建议我做什么? Thanx!
编辑: 以下是我的代码: Index.php: global.php makeHoriz.php和makeVert.php完全相同,只有一个$ db if 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:
global.php
makeHoriz.php and makeVert.php identical except one read from db and shows rows and second cols
if 这篇关于全局变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title> KUSS< / title>
< link href =styles / default.css =stylesheettype =text / css>
< / head>
< body>
< table width =90%border =0align =centercellpadding =0cellspacing =0class =main_border>
<?php
include_once(modules / php / mainMenu.php);
?>
<? include_once(modules / php / vertMenu.php); ?><! - 头 - >
< / table>
< / body>
< / html>
<?php
//所有全局变量必须在这里定义
//表示当前语言
$ gl_Lang =UKR;
//当前水平菜单单击
$ gl_MaimMenuChoice;
//当前垂直子菜单单击
$ gl_SubMenuChoice;
$ gl_dbName =127.0.0.1;
$ gl_UserName =user1;
$ gl_Password =12345;
$ gl_adminDatabase =admin;
?>
$ b $ p $ <?php
函数MakeLeftVMenu($ tableName,$ levelID, $ parentName)
{
include_once(modules / php / globals.php);
//连接到数据库或死)
$ db = mysql_connect($ gl_dbName,$ gl_UserName,$ gl_Password)或die(Unable to connect);
//预防者????? unicode中的符号 - utf-8编码
mysql_query(SET NAMES'UTF8');
//选择数据库
mysql_select_db($ gl_adminDatabase,$ db);
$ sql =SELECT。$ gl_Lang。 ,链接从。 $ tableName。WHERE LevelID =。$ levelID。 和ParentName ='。 $ parentName。
echo $ sql;
//执行SQL查询
$ result = mysql_query($ sql,$ db);
//将数据读取到数组
$ myRow = mysql_fetch_array($ result);
//将其打印到屏幕中
do
{
echo< tr>< h3>< a href =。trim($ myRow [''链接']) > 中。 trim($ myRow [$ gl_Lang])。< / a>< / h3>< / tr>;
$ b $} while($ myRow = mysql_fetch_array($ result));
//关闭数据库=非常重要
mysql_close($ db);
}
?>
horizonal_menu.php
和 vertical_menu.php
都包含 global.php
,您应该确保它们都是正在使用 require_once
。如果是这样,你不应该有任何问题。<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>
<?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";
?>
<?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);
}
?>
horizonal_menu.php
and vertical_menu.php
are both including global.php
, you should make sure both of them are using require_once
. If that is the case, you shouldn't have any problems.