【问题标题】:Joomla database array to json conversionJoomla 数据库数组到 json 转换
【发布时间】:2014-03-12 10:03:11
【问题描述】:

我正在尝试将我从 mysql 数据库中选择的数据转换为 json 格式。我正在使用 Joomla 3.2.1,以便我可以将它用于我的 iOS 应用程序。

我在 JResponse 附近收到语法错误意外的 Jresponse t_string 错误。

如果有人能指出我正确的方向,我将不胜感激。 谢谢。

 <?php
  defined ('_JEXEC') or die('');

  require_once JPATH_SITE.'/components/com_content/helpers/route.php';
  jimport('joomla.application.component.controller');
  jimport('joomla.appliction.component.model');


 $db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('order_id', 'store_name', 'invoice', 'firstname')));
$query->from($db->quoteName('#__mynicetable'));
$query->where($db->quoteName('order_id') );
$query->order('order_id ASC');
$db->setQuery($query);
$row=$db->loadRowList();
print_r($row);

$data =array ($row);
$document= JFactory::getDocument;

$document-> setMimetEncoding('application/json')
JResponse::setHeader('Content-Disposition', 'attachment;filename="'.$view-             >getName().'.json"');

echo json_encode($data);

【问题讨论】:

    标签: ios json joomla


    【解决方案1】:

    您的代码中有一些空白和缺少分号。尝试使用以下内容:

    $data = array($row);
    $app = JFactory::getApplication();
    $document = JFactory::getDocument();
    
    $document->setMimetEncoding('application/json');
    $app->setHeader('Content-Disposition', 'attachment;filename="my-scratchcomponent.json"');
    
    echo json_encode($data);
    

    【讨论】:

    • 谢谢。我已经尝试过您提交的代码,但它给了我致命错误:未定义的类常量'getDocument。
    • 好的,所以您正在使用的代码是在外部而不是在 Joomla 站点内使用的?如果您需要,您需要导入框架才能使用 Joomla API。要导入框架,请查看:stackoverflow.com/a/15042883/1362108
    • 我实际上已经从头开始创建一个组件。我已将此代码放在我的组件 my-scratchcomponent.php 中。它安装在我的 joomla 站点中。我没有在外部使用它。希望这能让它更清楚。
    • 啊,废话,对不起...将 getDocument 更改为 getDocument()。我已经更新了我的答案
    • 注意:未定义的变量视图致命错误:在非对象上调用成员函数 getName()。
    猜你喜欢
    • 1970-01-01
    • 2016-07-29
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2021-07-30
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多