我正在创建一个 joomla 模块,该模块会提取一篇文章,然后从文件夹中为其分配一个图像(无特定顺序)。
但它一直返回相同的图像:/... 其他一切都很好,我只需要它从每篇文章的文件夹中抓取不同的图像。
这是 mod_slider.php(仅包括必需品):
$a = explode(",", $params->get('id'));
foreach ($a as $k => $val) {
$args = $val;
$item[] = ModArticleSlider::getArticles($args,$params);
}
和 helper.php(包括整个类):
class ModArticleSlider {
public static function getArticles($args,$params){
$db = JFactory::getDBO();
$id = $args;
if($id > 0){
$query = "select * ";
$query .= "FROM #__content WHERE id =".$id." AND state=1 " ;
//echo $query;
$db->setQuery($query);
$item = $db->loadObject();
}
$folder = $params->get('folder');
if(JString::strpos($folder, JURI::base()) === 0)
{
$folder = str_replace(JURI::base(), '', $folder);
}
if(JString::strpos($folder, JPATH_SITE) === 0)
{
$folder = str_replace(JPATH_BASE, '', $folder);
}
$folder = str_replace('\\', '/', $folder);
$dir = JPATH_BASE.'/'.$folder;
$files = array();
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != '.' AND $file != '..' AND $file != 'index.html')
{
$files[] = $file;
}
}
}
closedir($handle);
$types = array_map('trim', explode(',', $params->get('type')));
$images = array();
foreach($files as $img){
if(!is_dir($dir.'/'.$img))
{
foreach($types as $type)
{
$muster = '@'.$type.'$@i';
if(preg_match($muster, $img))
{
$item->name = $img;
$item->folder = str_replace('\\', '/', $folder);
}
}
}
}
}
return $item;
}
}
然后是 index.php(仅包括必需品):
<?php foreach ($item as $items):?>
<img src="<?php echo $items->folder.'/'.$items->name ?>" />
<?php endforeach; ?>
非常感谢任何帮助。