从joomla数据库获取单个参数

时间:2014-05-14 18:13:45

标签: mysql database joomla field

我试图检索文章组件外部文章的图像介绍。 我正在使用此查询:

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('images')
 ->from('#__content')
 ->where('id = 151');
$db->setQuery($query);
$image = $db->loadResult();
echo $image;

问题是数据库字段,其中存储了许多参数,我的查询结果如下:

{"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":""}

我如何只检索" image_intro"参数β

2 个答案:

答案 0 :(得分:2)

首先,不要忘记在数据库查询中转义。我为你做了一些改动:

$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();

然后你需要像这样对结果进行json_decode

$image_intro = json_decode($image);
$path = $image_intro->image_intro;

如果您使用echo $path,您将获得以下输出:

images/image_name.jpg

然后您可以像这样显示图像:

<img src="<?php echo JUri::root() . $path; ?>" />

答案 1 :(得分:1)

我只需获取ContentModelArticle,然后加载特定的文章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;

文章网址也可以用同样的方式处理......

$urls    = json_decode($myArticle->urls);

或文章参数......

$params  = $myArticle->params;
$page_heading = $params->get('page_heading');

其他有用的参数...

 $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');