如何从 Joomla 中的 json 编码数据库 table 列值中提取单个值?

How to extract a single value from a json encoded database table column value in Joomla?

我正在尝试通过 PHP/Sql 查询从 Joomla 获取图像 url。是一个多维数组或对象,但我无法获取第二个维度。

那是什么数据类型?我必须以某种方式转换它吗?我已经尝试了 "loadAssocList()" 和 "loadObjectList()",但它们都给我奇怪的数据。

如何将其转换为多维数组,或者如何更好地访问 $r[images][intro_image] 值?为什么斜杠在那里转义?

Joomla 数据库查询,如文档中所示:

$db = JFactory::getDbo();
$id = 9; //example
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->where('catid="'.$id.'"');

$db->setQuery((string)$query);
$res = $db->loadAssocList();
?>
<?php foreach($res as $r): ?>
<pre>
<?php print_r($r); ?>
</pre>
<?php endforeach; ?>

PHP 响应数组:

[fulltext] => 
    [state] => 1
    [catid] => 9
    [created] => 2018-09-10 20:45:29
    [created_by] => 165
    [created_by_alias] => 
    [modified] => 2018-09-14 08:28:52
    [modified_by] => 165
    [checked_out] => 165
    [checked_out_time] => 2018-09-14 08:32:10
    [publish_up] => 2018-09-10 20:45:29
    [publish_down] => 0000-00-00 00:00:00
    [images] => {"image_intro":"images\/thumb_2017-28-04-Taspo.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/thumb_2017-28-04-Taspo.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
    [urls] => {"urla":"\/images\/\/Presseartikel\/2017-28-04-Taspo-optimiert.pdf","urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
    [attribs] => {"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}
    [version] => 5

我需要从数组的这一部分读取数据:

{"image_intro":"images\/thumb_2017-28-04-Taspo.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"images\/thumb_2017-28-04-Taspo.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}

图像采用 JSON 格式,因此您首先需要解码该数据。

$images = json_decode($r['images']);
print_r($images);

然后您可以使用以下代码访问 image_intro -

$images->image_intro;

我有几点要说。

由于您只想访问所选行的 image_intro 值,因此作为最佳实践,您应该准确查询该值。

  1. 指定 images 作为 #__content table 的目标列,如果该变量由不安全的资源(例如,用户提交的数据)。

  2. loadResult() 是访问结果集中(单列和单行)值的最佳调用。

  3. 建立错误捕获 (try{}catch{}) 和回显检查点以进行调试是一个好习惯。请注意不要向最终用户显示 php 的实际错误消息,因为坏人可能会利用这种高价值反馈做出邪恶的事情。

  4. 结果集中返回的值实际上是一个JSON对象。要通过其键明智地访问值,您必须首先将该值解码为 php 数组或对象。 json_decode() 是此任务的正确调用。如果您在没有第二个参数的情况下向函数提供 json 值,将生成一个对象数组,并使用 ->image_intro 语法访问 image_intro 值。如果使用 true 作为第二个参数,将生成一个数组,并且应使用 ["image_into"] 语法。

代码:

$id = 9;  // assumed to be insecure, so casting with (int) is done in snippet
try {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
                ->select("images")
                ->from("#__content")
                ->where("catid = " . (int)$id);
    echo $query->dump();
    $db->setQuery($query);
    if (!$result = $db->loadResult()) {  // declare $result and check for falsey value
        echo "Sorry no row found @ id = " , (int)$id;
    } else {
        echo "Image Intro = " , json_decode($result)->image_intro;
    }
} catch (Exception $e) {
    echo  "Syntax Error: " , $e->getMessage();  // never show getMessage() details to the public
}

上面的代码片段将显示构建的查询和所需的输出:(它将是您的 数据库前缀)

SELECT images FROM lmnop_content WHERE catid = 9

Image Intro = images/thumb_2017-28-04-Taspo.jpg


p.s。当您需要特定于 Joomla 的支持时,请 post 您的问题 Joomla Stack Exchange。这是 Joomla 团队希望您 post 以便更好地支持社区的地方。

您可能会注意到,也可能不会注意到,在您网站的 管理员 部分,有一个 帮助 选项卡和倒数第二个项目在下拉列表中是 Stack Exchange <-- 这会将您直接传送到 JSE。