4

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

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

我的查询在这里:

// 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()函数?

4

1 回答 1

4

终于有人已经为此提出了一个很好的解决方案。

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
于 2013-08-24T20:49:02.160 回答