Question

I am developing a new module which will show up the featured items on the slider.

I have successfully get the data in my module but for intro image there is problem.

My query is here:

// 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;
  //.
  //.
  //.
}

As you know image paths are kept in images column in contents table like the following form:

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

I want to know how to extract the intro image path from this data. Is there a common function/method for this or should I go with PHP's explode() function?

Was it helpful?

Solution

Finally someone has already made a good solution for this.

There is a good function of PHP, json_decode().

It converts that string (which I have learned later that it is JSON) into a key-value array. So all the data gets reachable:

$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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top