1

I am trying to convert the data that I selected from mysql database to json format. I am using Joomla 3.2.1 so that I can use it for my iOS application.

I am getting syntax error unexpected Jresponse t_string error near JResponse.

I would appreciate if anyone can point me in the right direction. thank you.

 <?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);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Gokhan Dilek
  • 4,314
  • 4
  • 21
  • 24

1 Answers1

1

You have some gaps in your code and a missing semi-colon. Try using the following:

$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);
Lodder
  • 19,758
  • 10
  • 59
  • 100
  • Thank you.I have tried the code that you have submitted but it gave me Fatal error: undefined class constant 'getDocument . – Gokhan Dilek Mar 12 '14 at 10:26
  • Ok, so the code you're using is being used externally rather than within the Joomla site? If you, you need to import the framework to be able to use the Joomla API. To import the framework, have a look at this: http://stackoverflow.com/a/15042883/1362108 – Lodder Mar 12 '14 at 10:29
  • I have actually started creating a component from scratch. I have placed this code inside my component my-scratchcomponent.php. It is installed in my joomla site. I am not using it externally. Hopefully this makes it more clear. – Gokhan Dilek Mar 12 '14 at 10:33
  • Ahh crap, sorry...change `getDocument` to `getDocument()`. I have updated my answer – Lodder Mar 12 '14 at 10:35
  • Notice:Undefined variable view Fatal error: Call to a member function getName() on a non-object. – Gokhan Dilek Mar 12 '14 at 10:39
  • Ok this is due to you not following MVC standards for you component. If you had done so, then this error wouldn't occur as it would automatically get the view name. You will have to manually input the file name as shown above. I've also made a few modifications to the code to use more up to date standards. – Lodder Mar 12 '14 at 10:58
  • it is now working except that .json file has the html code inside. Is it possible to only have the database output in this file? – Gokhan Dilek Mar 12 '14 at 11:08