简体   繁体   English

在PHP中包含函数之前定义一个变量,然后使用变量

[英]Define a variable before including function in PHP, and use variable

I am Working on making the menu for our content management software using php and we are having this small issue. 我正在使用php为我们的内容管理软件制作菜单,但这个问题很小。 Since we want everything to eventually be called in chunks, were breaking certain page items into chunks and loading them via functions through an included file. 由于我们希望最终以块为单位调用所有内容,因此将某些页面项分解为块,然后通过函数通过包含的文件加载它们。 Since this is hard to explain, I will post some example code of what i mean below. 由于这很难解释,因此我将在下面发布一些示例代码。

This is the file page.php (removed needless html code). 这是文件page.php(已删除的不必要的html代码)。 This is the page the user is on: 这是用户所在的页面:

<?php
define("CURRENT_PAGE", "page.php");
include_once("data/main.inc.php");
?><html>
Content loads here.
<? desktopMenu(); ?>
</html>

Okay and here's the function for desktopMenu() from main.inc.php: 好的,这是来自main.inc.php的desktopMenu()函数:

function desktopMenu() {
    // Query to get the top level navigation links with no parents
    $query = mysql_query("SELECT * FROM menu WHERE p_id = '0'");
    if(mysql_num_rows($query) > 0) {
        while($result = mysql_fetch_array($query)) {
            extract($result);
            if($isparent == "1") {
                // Just check if they have children items
                $sub_menu_query = mysql_query("SELECT * FROM menu WHERE p_id = '$id'");
                if(mysql_num_rows($sub_menu_query) > 0) {
                    // CODE TO SHOW THE MENU ITEM AND ITS SUBS
                }
            } else {
                // CODE TO SHOW REGULAR MENU ITEMS
                // WANT TO INSERT class='active' if the CURRENT_PAGE is this value..
                echo "<li><a href='#'>link</a></li>";
        }
    } else {
        echo "<li><a href='javascript:void(0);'>Error Loading Menu</a></li>";
    }
}

I am wondering how I can get the CURRENT_PAGE on the included script so I can load the class="active" onto the correct page. 我想知道如何在包含的脚本上获取CURRENT_PAGE,以便将class="active"加载到正确的页面上。 I am already using the following: 我已经在使用以下内容:

$config = include('config.inc.php');
$GLOBALS = $config;

on the top of main.inc.php , above this menu function so I could set global variables and include my $config['database'] variables for calling the SQL database within a function (doesn't work otherwise). main.inc.php顶部,此菜单功能上方,因此我可以设置全局变量,并包含$config['database']变量,以在函数内调用SQL数据库(否​​则无法正常工作)。

How can I check the current_page variable so I can set it active in the menu? 如何检查current_page变量,以便可以在菜单中将其设置为活动状态? I have tried a few different things but nothing is showing the way we expect it to. 我尝试了一些不同的方法,但是没有任何方法显示出我们期望的方式。 Thanks guy. 谢了,兄弟们。

First of all I would recommend looking at MVC architecture when building your apps. 首先,我建议在构建应用程序时考虑MVC架构。 I believe the use of GLOBALS is frowned upon. 我相信对GLOBALS的使用不被接受。

To answer your question: Since you are defining a constant define("CURRENT_PAGE", "page.php"); 回答您的问题:由于您正在定义常量define(“ CURRENT_PAGE”,“ page.php”); then this will be globally available within the scope of the function desktopMenu() so you may use something like: 那么它将在函数desktopMenu()的范围内全局可用,因此您可以使用以下方法:

$className = (isset(CURRENT_PAGE) && CURRENT_PAGE=='xxxxx')?'class="active"':'';
echo "<li><a href='#' ".$className.">link</a></li>";

xxxx string is most likely a field output from you database as the page name which will match the defined constant. xxxx字符串很可能是您数据库中输出的字段,作为与定义的常量匹配的页面名称。

$className = (isset(CURRENT_PAGE) && CURRENT_PAGE==$result['page_name'])?'class="active"':'';

This is the basic form and you will most likely need additional conditions for the 'active' menu switch mapping to different pages. 这是基本形式,您很可能需要其他条件才能将“活动”菜单开关映射到其他页面。 I've tried to answer your question with an example although the structure you have used run the app is not the recommended way to develop. 尽管您使用的运行应用程序的结构不是推荐的开发方法,但我尝试通过一个示例回答您的问题。

I would look at the way modern frameworks are structured (Laravel, Zend, Symphony...) and utilise these. 我将研究现代框架的结构方式(Laravel,Zend,Symphony ...)并加以利用。 I would also try and automate the page mapping (eg look at the URL and pull out the page from a rewrite which matches to the menu in your database) 我还将尝试自动执行页面映射(例如,查看URL并从与数据库菜单匹配的重写中拉出页面)

best of luck 祝你好运

There are multiple options. 有多种选择。 Including static functions, global variables and passing the variable or object into the function. 包括静态函数,全局变量以及将变量或对象传递到函数中。
The consensus for various reasons is to pass the variable into the function 由于各种原因,共识是将变量传递给函数

$myVar = new Object\Or\Data();

function myFunction($myVar) {
    //do stuff with $myVar
}

//then call the function
myFunction($myVar);

There are lots of answers to this question on stackOverflow, so have a deeper search. 在stackOverflow上,此问题有很多答案,因此请进行更深入的搜索。 Here is an example 这是一个例子

I found the solution to my problem and thought I would share here. 我找到了解决问题的方法,并认为我会在这里分享。 I first set the call on the page.php to use desktopMenu(CURRENT_PAGE); 我首先在page.php上设置调用以使用desktopMenu(CURRENT_PAGE); and then on my main.inc.php I added this line 然后在我的main.inc.php上添加了这一行

$thispage = CURRENT_PAGE;
function desktopMenu($thispage) {
    //REST OF FUNCTION
}

And I set a table variable on each menu item called menu-group, so I can define the current menu group for a menu item and have the appropriate menu item highlighted when you're on that page or one of it's sub pages. 而且,我在名为menu-group的每个菜单项上设置了一个表变量,因此我可以为该菜单项定义当前菜单组,并在该页面或其子页面之一中突出显示适当的菜单项。

Thanks so much for the answers guys! 非常感谢你们的答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM