如何从另一个文件获取变量的值到函数php中?

时间:2018-08-18 14:35:12

标签: php joomla3.0 joomla-extensions

我有一个Joomla模块
Default.php 文件中,调用一个函数,并从 xml文件中获得一个变量。
但是找不到变量函数。
$ mod_circle_display1 不被函数读取;
函数直接看到 else ,函数的输出为 column1 (即:$ mod_circle_displayres3)。

我的Default.php

require_once __DIR__ . '/function.php';
$check = CheckDisplay();
 ?>

 <div class="<?php echo CheckDisplay()?> <?php echo $mod_circle_move1;?>"
 style="<?php echo $mod_circle_display1;?>">

 <div class="<?php echo CheckDisplay()?> <?php echo $mod_circle_move2;?>"
 style="<?php echo $mod_circle_display2;?>">

 <div class="<?php echo CheckDisplay()?> <?php echo $mod_circle_move3;?>"
 style="<?php echo $mod_circle_display3;?>">

 <div class="<?php echo CheckDisplay()?> <?php echo $mod_circle_move4;?>"
 style="<?php echo $mod_circle_display4;?>">


我的Function.php

         function CheckDisplay ()
{ 
       /*global $mod_circle_display1; is not worked*/
       /*global $mod_circle_display2; is not worked*/
       /*global $mod_circle_display3; is not worked*/
       /*global $mod_circle_display4; is not worked*/
        $mod_circle_display1;
        $mod_circle_display2;
        $mod_circle_display3;
        $mod_circle_display4;

        $mod_circle_displayon = "display:none !important";
        $mod_circle_displayoff = "padding:0";

        $mod_circle_displayres1 = "column4";
        $mod_circle_displayres2 = "column2";
        $mod_circle_displayres3 = "column1";

     if (($mod_circle_display1 == $mod_circle_displayoff)AND($mod_circle_display2 == $mod_circle_displayoff)AND
         ($mod_circle_display3 == $mod_circle_displayoff)AND($mod_circle_display4 == $mod_circle_displayoff))
        {
           return $mod_circle_displayres1;
        }
 elseif (($mod_circle_display1 == $mod_circle_displayoff)AND($mod_circle_display2 == $mod_circle_displayon)AND
         ($mod_circle_display3 == $mod_circle_displayoff)AND($mod_circle_display4 == $mod_circle_displayon))
        {
           return $mod_circle_displayres2;
        }
  else { return $mod_circle_displayres3; }


我的mod_circle.xml

<field name="mod_circle_display1"
 type="list" 
 default="padding:0" 
 label="MOD_CIRCLE_DISPLAY1" 
 description="MOD_CIRCLE_DISPLAY1">
 <option value="display:none !important">Yes</option>
 <option value="padding:0">No</option>
</field>
and ...


我的mod_circle.php

defined( '_JEXEC' ) or die( 'Restricted access' );

require_once __DIR__ . '/helper.php';

$mod_circle_background          = $params->get('mod_circle_background');
$mod_circle_widthrl             = $params->get('mod_circle_widthrl');
$mod_circle_display1            = $params->get('mod_circle_display1');
$mod_circle_image1              = $params->get('mod_circle_image1');
$mod_circle_opacity1            = $params->get('mod_circle_opacity1');
$mod_circle_width1              = $params->get('mod_circle_width1');
$mod_circle_height1             = $params->get('mod_circle_height1');
$mod_circle_margin1             = $params->get('mod_circle_margin1');
$mod_circle_radius1             = $params->get('mod_circle_radius1');
$mod_circle_circle1             = $params->get('mod_circle_circle1');
$mod_circle_circletime1         = $params->get('mod_circle_circletime1');
and
    $mod_circle_display2            = $params->get('mod_circle_display2');
Etc.
    $mod_circle_display3            = $params->get('mod_circle_display3');
Etc.
    $mod_circle_display4            = $params->get('mod_circle_display4');
Etc.

1 个答案:

答案 0 :(得分:1)

首先,您必须低估就您的PHP脚本而言,$ mod_circle_display1不存在。

它可能存在于XML中,但在您的PHP中不存在

您需要首先将XML文件中的所需值解析为php变量($ mod_circle_display1),然后将$ mod_circle_display1设置为全局变量(请参见本教程:https://www.w3schools.com/php/php_xml_simplexml_read.asp

您甚至可以从函数内部将XML文件读取到$ mod_circle_display1中,因此无需将$ mod_circle_display1设置为全局变量