We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13507
    • 7 Posts
    Hi guys,
    While working on a module I have run across issues with including multiple snippets in the same document that require or include the same files.

    <?php
    /*
    Type: Snippet
    Name: search_form
    Description: Generated Search Form For Products
    */
    	ini_set("error_reporting", E_ALL);
    	ini_set("display_errors", true);
    
    $cssClass = (isset($cssClass))?$cssClass:"product_search";
    $mode = (isset($mode))?$mode:"home";
    $submitlabel = ($mode=="home")?"Go":"Search";
    $displayresults = ($mode=="home")?"":$modx->runSnippet('search_result_totals');
    $basePath = $modx->config['base_path'];
    
    include_once $basePath.'assets/modules/productmanager/config.inc.php';
    //global $pm_config;
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'options.php';
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'categories.php';
    
    $cats = new Categories();
    $categories = $cats->getAllCatNames();
    ...
    the code
    ...
    return $outHtml;
    ?>
    


    and the snippet below gets the following error ’Unknown(/path/webroot/options.php)’

    <?php 
    /*
    Type: Snippet
    Name: search_result_totals
    Description: Search Result Totals for Product Manager
    */
    
    $basePath = $modx->config['base_path'];
    include_once $basePath.'assets/modules/productmanager/config.inc.php';
    //global $pm_config;
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'options.php';
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'categories.php';
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'fields.php';
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'types.php';
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'products.php';
    include_once $basePath.$pm_config["path_module"].$pm_config["path_class"].'productdata.php';
    
    $cats = new Categories();
    $categories = $cats->getAllCatNames();
    
    $catWhere = Array();
    foreach($categories as $catid => $catname) {
    	if($_REQUEST["cat_".$catid] > 0) {
    		$catWhere[] = "( catid = ".$catid." AND optid = ".$_REQUEST["cat_".$catid]." )";
    	}
    }
    
    $where = implode(" AND ",$catWhere);
    if(empty($where)) {
    	$where = "1";
    }
    
    $sql = "SELECT DISTINCT(prodid) as productid FROM ".$pm_config["db_prefix"].$pm_config["dbt_cat_link"]." WHERE ".$where.";";
    $result = $modx->dbQuery($sql);
    $rowCount = $modx->recordCount($result);
    $start = $_REQUEST["strt"];
    $start = 1;
    $end = $start + 10;
    $retVal = "<p>Displaying {$start} - {$end} of {$rowCount} Products or Services</p>\n";
    return $retVal;
    ?>
    



    As you can see I am using a config array to hold the path to the correct folder, which should read ’/path/webroot/assets/modules/productmanager/classes/options.php’

    It seems to be a problem with calling runSnippet instead of letting MODx parse it through the document source as a [!blah!] style snippet.

    Has anyone got any ideas on how I can get around this problem?
      • 22303 MODX Staff
      • 10,725 Posts
      I don’t see where $pm_config is defined anywhere in this code...but regardless, have you tried passing the array as a snippet parameter? The array would have to be join()’d into a single string value with a separator. Otherwise, you should always refer to globals explicitly, i.e. $GLOBALS[’pm_config’]; but that is just my opinion on the subject of global variables in general.
        • 13507
        • 7 Posts
        the array $pm_config is defined in the included file ’assets/modules/productmanager/config.inc.php’

        I guess great minds think alike! I have just tested the option where I try to get the variable from $_GLOBALS, but no luck there sad

        FYI I tried to pass the array into the runSnippet call and it didn’t work (I tried array(’pm_config’ => $pm_config), and an imploded version )

        However;
        I have managed to get it working by changing the include_once to just include in the search_result_totals snippet.
        This isn’t really an elegant solution, more of a workaround undecided

        Thanks for your quick response, Im almost ready to move to version alpha 0.2 for the module I am building. Woop woop!

        Look out for the Product Manager module in the In Development forum in the next fortnight grin
          • 27376
          • 576 Posts
          Whenever you include a file in PHP, the included file inherits the variable scope of the file it was included in. So if you have two separate snippets that use a variable with the same name, the snippet you include in this manner will overwrite the previous snippet’s variable.

          More information about Variable Scope is available on php.net.

          What is the contents of the config file you included in this example?
            • 13507
            • 7 Posts
            The problem I was having was that the variable that was included previously wasn’t available to a snippet run through the runSnippet command sad
            confusingly it is available to a later (3rd) snippet that was running through a tag ( the [[]] or [!!] style snippet ).

            Re: Quote from: sirlancelot at Jul 12, 2007, 10:36 AM

            Whenever you include a file in PHP, the included file inherits the variable scope of the file it was included in. So if you have two separate snippets that use a variable with the same name, the snippet you include in this manner will overwrite the previous snippet’s variable.

            More information about Variable Scope is available on php.net.

            What is the contents of the config file you included in this example?

            Thanks sirlancelot, Its always good to be reminded about this stuff cool[me=jamie]runs off and reads[/me]

              • 22303 MODX Staff
              • 10,725 Posts
              This is because each snippet is executed in an eval(). This affects the scope as well, especially when nested within another eval(). Each nested instance is a new scope, as if it were a new function. This is again why I suggest to use $GLOBALS to reference these global variables, if you must use global variables. You could alternatively use the $modx->placeholders array instead of global variables, since you always have access to $modx and the placeholder functions.