Reputation: 301
I'm trying to retrieve the image intro of an article outside the article component. I'm using this query:
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('images')
->from('#__content')
->where('id = 151');
$db->setQuery($query);
$image = $db->loadResult();
echo $image;
The problem is the database field, where are stored many parameters, and the result of my query is this:
{"image_intro":"images\/myimage.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/myimage.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
How can i retrieve only the "image_intro" parameter?
Upvotes: 0
Views: 745
Reputation: 9330
I would simply get the ContentModelArticle
and then load the specific article ID.
/* Lets say the article ID = 151 */
$id = 151;
/* Get an instance of the generic articles model
@var ContentModelArticle $model */
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$myArticle = $model->getItem($id);
/* That way you have access to all the use params as well as the image you're after */
$images = json_decode($myArticle->images);
$image_intro = $images->image_intro;
Article URLs can also be handled the same way…
$urls = json_decode($myArticle->urls);
Or Article params…
$params = $myArticle->params;
$page_heading = $params->get('page_heading');
Other useful params…
$params->get('show_publish_date');
$params->get('show_create_date');
$params->get('show_hits');
$params->get('show_category');
$params->get('show_parent_category');
$params->get('show_author');
Upvotes: 1
Reputation: 19743
Firstly, don't forget to escape in your database query. I've made some changes to it for you:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('images'))
->from($db->quoteName('#__content'))
->where($db->quoteName('id') . ' = '. $db->quote('151'));
$db->setQuery($query);
$image = $db->loadResult();
You then need to json_decode the result like so
$image_intro = json_decode($image);
$path = $image_intro->image_intro;
If you use echo $path
, you will get the following output:
images/image_name.jpg
You can then display the image like so:
<img src="<?php echo JUri::root() . $path; ?>" />
Upvotes: 2