【问题标题】:Get single parameter from joomla database从 joomla 数据库中获取单个参数
【发布时间】:2014-05-14 18:13:44
【问题描述】:

我正在尝试检索文章组件之外的文章的图片介绍。 我正在使用这个查询:

$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”参数?

【问题讨论】:

    标签: mysql database joomla field


    【解决方案1】:

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

    $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; ?>" />
    

    【讨论】:

      【解决方案2】:

      我只需获取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;
      

      文章 URL 也可以用同样的方式处理……

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        • 2017-05-04
        • 2012-11-04
        • 2013-05-06
        • 2017-12-28
        • 1970-01-01
        相关资源
        最近更新 更多