简体   繁体   中英

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. 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). 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:

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

How can I check the current_page variable so I can set it active in the menu? 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. I believe the use of GLOBALS is frowned upon.

To answer your question: Since you are defining a constant define("CURRENT_PAGE", "page.php"); then this will be globally available within the scope of the function desktopMenu() so you may use something like:

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

$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. 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)

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. 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); and then on my main.inc.php I added this line

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

Thanks so much for the answers guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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