Joomla database array to json conversion - ios

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

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

Related

Passing URL parameter to link on page

I am trying to grab a parameter from a webpage and insert it into a URL link on that same page but am having problems with the syntax.
So, for example, the webpage is www.website.com?src=mm
Currently the code on the page that does not pull in the parameter is
<?php echo "<A HREF='http://www.website2.com?offer=AAt&sub1=422'><B>Click Here</B></A><BR>" ?>
I would like to include that "mm" parameter at the end of the URL so the final URL is:
http://www.website2.com?offer=AA&sub1=422&sub2=mm
I tried the following but does not work:
<?php echo "<B>Click Here</B><BR>" ?>
Any ideas on how to get this to work? Thanks
Your code doesn't even compile:
Parse error: syntax error, unexpected 'http' (T_STRING), expecting ',' or ';' in /var/www/html/ImagePT/test.php on line 1
it has to be
<?php echo '<B>Click Here</B><BR>'; ?>
but since I'm just in the mood to give you some further advice:
You don't have to write HTML in uppercase, it's rather unusual (not impossible, but you don't see it very often) - then this script is horrible, when the $_GET['src'] variable is undefinied, therefore I'd check if it is set and then modifiy the URL accordingly. So my advice would be to use the following:
<?php
if(isset($_GET['src']))
{
echo '<b>Click Here</b></br>';
}
else
{
echo '<b>Click Here</b></br>';
}
?>

Fetch Data from Quickbook

Hello guys I'm new to quickbooks. There is some data in quickbooks which I'm trying to retrieve but quickbooks is returning nothing. I can figure out what is it that I'm doing wrong. I'm trying to retrieve data like this
QUICKBOOKS. PHP
$serviceType = IntuitServicesType::QBO;
$realmId = $this->CI->session->userdata('realmId');
$token = unserialize($this->CI->session->userdata('token'));
$requestValidator = new OAuthRequestValidator($token["oauth_token"],
$token["oauth_token_secret"],
$this->CI->config->item('OAUTH_CONSUMER_KEY'),
$this->CI->config->item('OAUTH_CONSUMER_SECRET'));
$serviceContext = new ServiceContext($realmId, $serviceType, $requestValidator);
$dataService = new DataService($serviceContext);
$entities = $dataService->Query("SELECT * FROM SalesReceipt");
error_log(print_r($entities , true));
can somebody tell me what is it that I'm doing wrong. Thanks
The first thing you should do in a case like this, is turn on logging, and post the logs.
There is not much anyone can tell you here without seeing your logs.
With that said, it's worth nothing that this error:
instead of dataservice I tried using QuickBooks_IPP_Services_SalesReceipt but it's giving me a error salesrecipt class is not defined
Is because that class is from a totally separate, unrelated PHP code library. If you want to use that class, then what you should do is:
Get code from here: https://github.com/consolibyte/quickbooks-php
Follow the quick-start guide linked on that page.
You should end up with code that looks like this:
.
$SalesReceiptService = new QuickBooks_IPP_Service_SalesReceipt();
$salesreceipts = $SalesReceiptService->query($Context, $realm, "SELECT * FROM SalesReceipt STARTPOSITION 1 MAXRESULTS 10");
foreach ($salesreceipts as $SalesReceipt)
{
print('Receipt # ' . $SalesReceipt->getDocNumber() . ' has a total of $' . $SalesReceipt->getTotalAmt() . "\n");
}
If you still have trouble, you can get the HTTP request/response like this:
print($IPP->lastError($Context));
print("\n\n\n\n");
print('Request [' . $IPP->lastRequest() . ']');
print("\n\n\n\n");
print('Response [' . $IPP->lastResponse() . ']');
More examples can be found here:
https://github.com/consolibyte/quickbooks-php/tree/master/docs/partner_platform/example_app_ipp_v3

Zend framework 2 CSV data as an array or string

I am still very new to Zend and running into some issues on exporting my data to a CSV.
I found a great resource that explains the headers and download part here however I am running into issues when trying to export the actual data.
If I create a variable like $content = "test" the export works fine using the code above.
However when I duplicate my indexAction code, make some changes, and bring it into my downloadAction, I am getting issues that I believe are due to my content being returned as an Object rather than an array or string.
My Module is grabbing the SQL by using:
public function fetchAllMembers($order = null , $order_by = null, $selectwhere = null) {
$session = new SessionContainer('logggedin_user');
$sql = new Sql($this->adapter);
$select = new Select();
$select->from(array('u' => 'tbl_all_data'));
if ($selectwhere != null){
$select->where($selectwhere);
}
$select->order($order_by . ' ' . $order);
$selectString = $sql->getSqlStringForSqlObject($select);
$results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
$results->buffer();
return $results;
}
and my Controller is calling that SQL by using:
$content = $modulesTable->fetchAllMembers($order, $order_by, $where);
Any help would be greatly appreciated, and I don't need anyone to write the code for me just help with pointoing me in the right direction.
$this->adapter->query returns a Zend\Db\ResultSet object. So you need to call $results = $results->toArray(); to send an array.
Also you need to loop through the array and echo it out in your view file.
Results, returned by adapter are ResultSet type. I guess you need to call at least
current()
method to grab some data. And they will be of array type, so, again you need to do something with them.
toArray() is often used to quickly get data.
More sophisticated way to get data, is to use next() method with current():
$firstThing = $result->current();
$result->next();
$result->next();
$thirdThing = $result->current();
It's just an example, but it can be useful in some cases.

enter a specific object value

i'm using zendframwork 2 , i implement the album exemple in the official documentation
http://framework.zend.com/manual/2.1/en/user-guide/overview.html#the-tutorial-application (so in my model folder i have Album.php and AlbumTable.php) and all works fine , i just want to make a small modification :
i want to have acces to the third element in album . in the index.phtml view (originally i have this code )
<?php foreach ($albums as $album) : ?>
<?php echo $this->escapeHtml($album->title);?>
<?php echo $this->escapeHtml($album->artist);?>
<?php endforeach; ?>
i tried things like
<?php echo $this->escapeHtml($album->title[3]);?>
<?php echo $this->escapeHtml($album[3]->title);?>
but i always get this error
( ! ) Fatal error: Cannot use object of type Auth\Model\Album as array in C:\wamp\www\zf2-album\module\Auth\view\auth\auth\index.phtml on line 14
any help please ?
thanks every one
Do you need ONLY the third one? Then write your Query to be more efficient ;) For all other cases, since you are working with a Zend\Db\ResultSet\ResultSet, which uses Iterator you have different options.
The first one would be to still iterate through the fieldset like
while ($albums->key() != 3) {
$albums->next();
}
$album = $albums->current();
The alternative would be to simply convert the ResultSet into an array
$myAlbums = $albums->toArray();
$album = $myAlbums[3];
Depending on how big your ResultSet is and how many entries you really need, either Solution may be faster for you. Guess you have to test that one ;)

nicedit - is it safe and is it affected by the site's css?

I'm considering using nicedit (http://nicedit.com/) for my site.
I assume that nicedit simply creates simple html using the buttons, and that html gets sent when the user saves it.
Is it recommended? Is someone still working on it?
Assuming I'm later displaying this HTML in my site somewhere, isn't it dangerous due to the user being able to plant malicious javascript? If not, how does nicedit prevents this?
Also, when I display this HTML later, will it be affected by my css? If so, how can I prevent this?
Thanks.
This is what I use it works like a charm for cleaning out the content of the nicedit instance before chucking into the database
function cleanFromEditor($text) {
//try to decode html before we clean it then we submit to database
$text = stripslashes(html_entity_decode($text));
//clean out tags that we don't want in the text
$text = strip_tags($text,'<p><div><strong><em><ul><ol><li><u><blockquote><br><sub><img><a><h1><h2><h3><span><b>');
//conversion elements
$conversion = array(
'<br>'=>'<br />',
'<b>'=>'<strong>',
'</b>'=>'</strong>',
'<i>'=>'<em>',
'</i>'=>'</em>'
);
//clean up the old html with new
foreach($conversion as $old=>$new){
$text = str_replace($old, $new, $text);
}
return htmlentities(mysql_real_escape_string($text));
}
It doesn't appear to be maintained anymore. But I have used it for purposes where I needed just a simple/lightweight WYSIWYG editor. If you are looking for something that gets constant core updates or additional features I wouldn't count on it. I finally broke down and wrote a lot of my own features like tables and YouTube videos.
Yes, a hacker could use it to post an client and/or server exploit on your site. But this is a threat you can face with any editor. You need to filter the code for two methods.
You need to prevent SQL injection by sanitizing your post variables. I always put this at the beginning of my scripts to clean them and call them with $input['whateveryouarepassing']instead of $_POST['whateveryouarepassing']. Edit the $mysqli->real_escape_string() parts to work with your database object. Use MySQLi or PDO with prepared statements to help harden the attack.
$input = array();
if(isset($_POST)) {
foreach ($_POST as $key => $value) {
if (#get_magic_quotes_gpc()) {
$key = stripslashes($key);
$value = stripslashes($value);
}
$key = $mysqli->real_escape_string($key);
$value = $mysqli->real_escape_string($value);
$input[$key] = $value;
}
}
Then I like to clean it with this function I put together over the years with various methods of cleaning out bad code. Use HTML Purifier instead if you can set it up. If not, here is this bad boy. Call it with cleanHTML($input['whateveryouarepassing']);.
function cleanHTML($string) {
$string = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $string);
$string = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $string);
$string = html_entity_decode($string, ENT_COMPAT, "UTF-8");
$string = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $string);
$string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...', $string);
$string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2novbscript...', $string);
$string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*-moz-binding[\x00-\x20]*:#Uu', '$1=$2nomozbinding...', $string);
$string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $string);
$string = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])style[^>]*>#iUu', "$1>", $string);
$string = preg_replace('#</*\w+:\w[^>]*>#i', "", $string);
$string = preg_replace('/^<\?php(.*)(\?>)?$/s', '$1', $string);
$string = preg_replace('#</*(applet|meta|xml|blink|link|script|embed|object|frame|iframe|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $string);
return $string;
}
The HTML will be affected by your CSS when editing and displayed. You will need code additional CSS rules if this is an issue. If the issue is when editing move to a iframe based editor and to prevent the css display the html content in an iframe.
If you want another suggestion elRTE is my goto editor these days. A little more advanced but totally worth it once you get to know the code base and API. You will face the same issues as above as will any editor. Except the CSS during editing since elRTE is framebased and you can specify stylesheets. elRTE Homepage
Edit: I posted this assuming you were using PHP. Apologies if not.

Resources