全局变量的问题

发布于 2024-10-01 09:36:16 字数 2325 浏览 1 评论 0原文

我在 PHP 中包含页面时遇到问题。图片显示了我想做的事情。我想在我的 index.php 页面中包含水平和垂直菜单。但现在我只能包括其中之一。在 global.php 中有数据库名称、密码和定义我现在使用的语言的变量。 我包含了所有衍生词: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 相同,除了从数据库读取并显示行和第二列

<?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!

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2024-10-08 09:36:18

如果所有页面都有水平/垂直菜单,您可能会创建一个包含水平/垂直菜单和 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.

触ぅ动初心 2024-10-08 09:36:18

菜单页面是否都会被单独调用?例如,您是否曾在浏览器中访问 http://mywebsite.com/horizo​​ntalmenu.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 ,则应该避免该问题。

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

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.

Alternatively if you use require_once for every single require or include statement you should avoid the problem.

青瓷清茶倾城歌 2024-10-08 09:36:17

如果 horizo​​nal_menu.phpvertical_menu.php 都包含 global.php,您应该确保它们都使用 require_once< /代码>。如果是这样的话,你应该不会有任何问题。

if 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文