如何在函数()中使用来自其他php脚本的php变量
我搜索了stackoverflow和其他一些网站,尝试了几个小时的新代码组合,但我放弃了。。 我有两个php文件,一个名为getimages.php,另一个名为mod_slidepluslight.php。我使用Joomla作为CMS,并使用lightbox制作了一个幻灯片模块,现在我想通过.xml文件中设置的模块参数从Joomla内的文件夹中检索图像。我通过使用以下代码实现了这一点:如何在函数()中使用来自其他php脚本的php变量,php,variables,joomla,include,scope,Php,Variables,Joomla,Include,Scope,我搜索了stackoverflow和其他一些网站,尝试了几个小时的新代码组合,但我放弃了。。 我有两个php文件,一个名为getimages.php,另一个名为mod_slidepluslight.php。我使用Joomla作为CMS,并使用lightbox制作了一个幻灯片模块,现在我想通过.xml文件中设置的模块参数从Joomla内的文件夹中检索图像。我通过使用以下代码实现了这一点: $imagePath = $params->get('imagePath', 'banners');
$imagePath = $params->get('imagePath', 'banners');
现在,当我试图声明这个变量并在代码中使用它时,它不会做任何事情
function returnimages($relPath = "/KVD/images/") {
$dirname = $_SERVER['DOCUMENT_ROOT'] . $relPath . $imagePath;
$imagePath应添加在/KVD/images/…/之后或现在的位置。getimages.php的整个代码如下所示:
Header("content-type: application/x-javascript");
$imagePath = $params->get('imagePath', 'banners/');
function returnimages($relPath = "/KVD/images/") {
$dirname = $_SERVER['DOCUMENT_ROOT'] . $relPath . $imagePath;
$files = array();
$curimage = 0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if (preg_match('/\.(jpg|jpeg|gif|png)$/', $file)){
print_r ('galleryarray['.$curimage.']="'. $relPath . $file .'";');
$curimage++;
}
}
closedir($handle);
}
return($files);
}
print 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
谢谢,Koen。您在函数内调用$imagePath,但$imagePath超出了函数范围!您可以将$imagePath作为参数发送给函数请记住变量的作用域:
returnimages()
函数将无法访问$imagePath变量,除非您将其全球化或通过函数传递
只需在函数代码顶部添加:
global $imagePath;
你需要考虑一种不同的方法。当创建将使用另一个脚本的模块时,该另一个脚本应该是一个助手 看看:
如果您将getimages.php的内容作为助手提供,如上面的链接所述,您将能够在该脚本中使用您的参数。嘿,谢谢您的回答,我现在将设置为这种方式,但它仍然不起作用$imagePath=$params->get('imagePath','banner/');函数returnimages($relPath=“/KVD/images/”{global$imagePath;$dirname=$\u SERVER['DOCUMENT\u ROOT'].$relPath.$imagePath;感谢您的回答,您的意思是我应该在函数中添加$imagePath=…..吗?这是一个可能的解决方案。另一个函数定义如下:函数returnimages($relPath=“/KVD/images/”,$imagePath),然后在校准时返回图像(null,$imagePath)