2013-08-24 59 views
2

我正在开发一个新的模块,它将显示滑块上的特色项目。如何从Joomla的db中获取文章的图像路径

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

我的查询是在这里:

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

回答

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