【问题标题】:How to extract a single value from a json encoded database table column value in Joomla?如何从 Joomla 中的 json 编码数据库表列值中提取单个值?
【发布时间】:2019-02-19 02:56:36
【问题描述】:

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

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

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

文档中的 Joomla DB 查询:

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

【问题讨论】:

    标签: php arrays json joomla resultset


    【解决方案1】:

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

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

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

    $images->image_intro;
    

    【讨论】:

    • wtf 他们不是简单地使用多维数组吗? :(
    【解决方案2】:

    我有几点要说。

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

    1. #__content 表中指定images 作为目标列,如果该变量由不安全的资源(例如,用户提交的数据)提供,则将您的$id 值转换为整数。

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

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

    4. 结果集中返回的值实际上是一个 JSON 对象。要通过其键明智地访问其中的值,您必须首先将值解码为 php 数组或对象。 json_decode() 是此任务的正确调用。如果您在没有第二个参数的情况下为函数提供 json 值,则会生成一个对象数组,并使用 -&gt;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
    }
    

    上面的 sn-p 将显示构建的查询和所需的输出:(它将是 您的 db 前缀)

    从 lmnop_content WHERE catid = 9 中选择图像

    图片介绍 = images/thumb_2017-28-04-Taspo.jpg


    附言当您需要特定于 Joomla 的支持时,请在Joomla Stack Exchange 发布您的问题。这是 Joomla 团队希望您发布的地方,以便更好地支持社区。​​p>

    您可能会也可能不会注意到,在您网站的 administrator 部分,有一个 Help 选项卡,下拉列表中的倒数第二项是 Stack Exchange

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 2018-02-08
      • 2022-07-02
      • 1970-01-01
      相关资源
      最近更新 更多