如何在Joomla中从db获取文章的介绍图像路径

时间:2013-08-24 18:58:07

标签: php mysql joomla joomla3.1

我正在开发一个新模块,它将显示滑块上的特色项目。

我已成功获取模块中的数据但是对于介绍图像存在问题。

我的查询在这里:

// Get a db connection.
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

$query
        ->select(array('f.content_id', 'c.id', 'c.title', 'c.alias', 'c.images'))
        ->from('#__content AS c')
        ->join('INNER', '#__content_frontpage AS f ON (c.id = f.content_id)')
        ->where("c.language = '" . JFactory::getLanguage()->getTag() . "' AND c.state=1")
        ->order('f.ordering ASC');

// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects.
$results = $db->loadObjectList();

foreach ($results as $r)
{
  $imagePath = $r->images;
  //.
  //.
  //.
}

如您所知,图像路径保存在内容表的images列中,如下所示:

{
 "image_intro":"images\/products\/201191420496.jpg",
 "float_intro":"",
 "image_intro_alt":"",
 "image_intro_caption":"",
 "image_fulltext":"",
 "float_fulltext":"",
 "image_fulltext_alt":"",
 "image_fulltext_caption":""
}

我想知道如何从这些数据中提取前奏图像路径。是否有一个共同的功能/方法,或者我应该使用PHP的explode()函数?

1 个答案:

答案 0 :(得分:3)

最后有人已经为此做了一个很好的解决方案。

PHP有一个很好的功能,json_decode()

它将该字符串(我后来才知道它是JSON)转换为键值数组。所以所有数据都可以访问:

$pictures = json_decode('{"image_intro":"images\/products\/201191420496.jpg",
                          "float_intro":"",
                          "image_intro_alt":"",
                          "image_intro_caption":"",
                          "image_fulltext":"",
                          "float_fulltext":"",
                          "image_fulltext_alt":"",
                          "image_fulltext_caption":""
                         }',
                         true);

echo $pictures['image_intro']; //gives images/products/201191420496.jpg