问题描述:

i'm calling multiple functions from a function...as per my scope i want to call a specific function from parent function as per file that calls that function...

function dispcategories() {

include ('database/connection.php');

$select = mysqli_query($con, "SELECT * FROM categories");

while ($row = mysqli_fetch_assoc($select)) {

echo "<table class='category-table'>";

echo "<tr><td class='main-category' colspan='2'>".$row['category_title']."</td></tr>";

dispsubcategories($row['cat_id']);

dispsubcategoriesstate($row['cat_id']);

echo "</table>";

}

}

From dispcategories i want to call dispsubcategoriesstate() if and only if file updatestate.php calls dispcategories()...if any other file calls dispcategories() than it should not call to dispsubcategoriesstate()...

Hope everyone get the question it is possible to check file name threw if condition or not who calls the function

网友答案:

Including a file is just like typing it into your source code. You can use the magic constant __FILE__ to determine the current file, but this won't tell you what file the calling function was written in.

I'd suggest that you should rather pass a parameter to your function and then call the subroutine based on that.

Declare your function like this and replace the line dispsubcategoriesstate($row['cat_id']); with the block shown below

function dispcategories($fetchSubCategories = false) {
    // your code goes here
    if ($fetchSubCategories === true) {
        dispsubcategoriesstate($row['cat_id']);
    }
    // the rest of your code goes here

}

When you call it from the file where you do not want sub-categories you will call it like you do now:

dispcategories();

Whenever you want it to fetch the subcategories you can pass the parameter through like this:

dispcategories(true); 

I'm using an optional function argument and if this is confusing you should read the manual page.

相关阅读:
Top