How to encode large amount of data as json - php

I need to be able to pass a json from php to the client so javascript can parse the code and return it. Currently, the way I have always done this is:
<?php
$mysql_query = $mysqli->query(QUERY GOES HERE);
$array - array();
while($row = $mysql_query->fetch_assoc()){
array_push($array, $row);
}
$json =json_encode($array);
?>
<!-- javascript -->
<script>
var json = <?php echo $json;?>;
//...
</script>
<!--- rest of html --->
This usually works. However, the query returns more than 100,000 rows, and php is currently running out of memory on creating the entire array. I have seen some people say to use ajax. Is this the only way? And if so, how exactly would I go about implementing it? Or is there a more efficient method of encoding the mysql data into json without ajax?
Thank you

Since you said you can also send your array in chunks, meaning you can process your data while it's sorted out in several ways, i consider doing this:
Fetch the database data in small chunks (memory wise), in normal php arrays.
Doing it such a way that you unset the previously allocated chunk to free memory.
JSON encode your partial data into an HTML element.
Like this:
<input id="json-1" type="hidden" data-json='<?php echo json_encode($chunk[0]); ?>' />
<input id="json-2" type="hidden" data-json='<?php echo json_encode($chunk[1]); ?>' />
<input id="json-3" type="hidden" data-json='<?php echo json_encode($chunk[2]); ?>' />
Yes, it looks ugly but consider some pros:
There's no giant javascript JSON object, hogging the client's memory
You can parse the data in a progressive way, thus consuming only a portion of memory
javascript processing
$(document).ready(function() {
$("input[id^='json-']").each(function() {
var json = $(this).data("json");
// process the chunkied 'json'
// remove the memory allocated (since you dont want anymore the huge data, probably)
$(this).removeData(this,"json")
});
});

If you want to pass something from php to javascript you will have to use ajax as php is server side and javascript is client side.
Having an array of 100,000 rows is a bit big and could cause problems. Consider splitting the array into smaller chunks or limiting the query.
You can increase phps memory limit but it would be better to optimize the array first.

You don't need to handle so large amount of data.
Separate into pages.

Related

return a large amount of html (with a couple of variables) via ajax?

I have an ajax function search for keywords in a big database. The php being called simply says "no" if there's nothing, but if there are found records, it goes ahead and creates all of the HTML and returns the html, so that AJAX only needs to put the returned text into the html of a div. My problem is that I'd like to pass along a couple of variables, like the number of records found, etc.
So if I tried to put it in a statement that javascript could eval, I'm afraid that not only is all of the html potentially big enough to cause some sort of variable problems, but it also has a lot of single and double quotes, etc, that could unexpectedly end the variable before it's supposed to. See the following
// (I know I don't have a single quote after data and that will break it. This is just an example
echo "{ status: 'success', total: '".count($relevance)."' data: ";
foreach ($relevance as $re) {
// tons of html is printed here
}
echo " }";
So the question is, how do I most effectively send back a whole gang of html code, along with some variables that can be easily eval'ed by JS?
Use json_encode This will eliminate any errors you might have in trying to create your own json.
$returnArray = array(
'status'=>'success',
'total' => count($relevance),
'data' => ''
);
foreach ($relevance as $re) {
$returnArray['data'] .= $re; // + all long html code
}
echo json_encode($returnArray);
You can encode the data as JSON and then put it in a <script> block that's got a non-JavaScript type. Give the script a class too so your code can look for it easily. You can then get the ".innerHTML" of the <script> element and decode the JSON. Then just add the <script> to the rest of the HTML you're returning.
edit No use #Neal's answer instead; it's a less goofy idea. I've done what I've described but usually that's because for some other (framework) reason it's not easy (or just inconvenient) to get directly at the
response data. Also, I generally generate pages via JSP, so it's much easier to drop JSON into a page than to get the page contents into Java.
To elaborate, a <script> block like this:
<script type='text/json' class='some-data-for-you'>
{ "hello": "world" }
</script>
will be ignored by the browser because the "type" won't be recognized as code. Then your JavaScript code can just look for <script> elements with class "some-data-for-you" in the returned content and then parse the ".innerHTML" with a JSON parser.

Parsing MySQL into JavaScript Arrays through PHP and jQuery

Ultimate Goal: I want to take data I keep in a MySQL Database and put it into an Array of Arrays in JavaScript, so it can be manipulated client side.
So far, I have been able to pull data from my database with this code:
<?php
...
$num=1;
$q = "SELECT blah1, blah2, blah3 WHERE blah4=$num";
$sth = mysqli_query ($database, $q);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
?>
(from: JSON encode MySQL results)
This is where I am stuck, because I am having trouble getting this data into JavaScript.
One solution I can find is Parsing a Separate PHP Array in Javascript:
<script>
var jsonarray = <?php echo json_encode($array); ?>;
// now you can use jsonarray in your javascript
</script>
But the problem with this implementation is that it would spit out all of the database content I query onto the source-code of the page. If I have to do this, I might as well skip the database and just keep all of the content in javascript.
I am thinking there must be a way with jQuery/AJAX to pass the value of $num to a PHP script, grab this data and put it into a JavaScript Array without having to output all of it to the page.
Any assistance would be appreciated.
Thank you!
This solution you posted:
<script>
var jsonarray = <?php echo json_encode($array); ?>;
// now you can use jsonarray in your javascript
</script>
Is actually a very good approach. Using AJAX is drastically slower (because of network latency).
Unless you really need AJAX for some reason, you should avoid using it. It will add a noticeable split second of load time to the page, often for no benefit at all.
Above all when structuring your page, you want to try and reduce the number of individual network requests between the browser and the server. The less requests the faster your page will be. This is especially true for javascript and ajax, because they are unpredictable and browsers find it very difficult to optimise any part of the page where it's being used.
We're talking about one quarter of a second compared to one millionth of a second, for exactly the same end result.
Making an AJAX call with jQuery is easy enough. Take a look at the documentation: http://api.jquery.com/jQuery.get/
You can also do this without AJAX
var jsonarray = eval(<?php echo json_encode($array); ?>);

Parsing a Separate PHP Array in Javascript

So, as sort of an exercise in learning, I am trying to port a site I was working on for someone, written in PHP, MySQL and CSS, over to Ajax (Specifically, really, just adding Javascript so that the page is dynamic instead of needing to constantly reload).
Unfortunately, I have some scripts I'd like to keep as PHP (for instance, a script to update a database given an array of victims) because I need it to interact with a database, and this means that the array must stay as PHP (I think).
So, given this file,
<?php
$victims = array(
// Animals
"chickens",
"horses",
"cows",
// Supernatural
"werewolves",
"zombies",
"vampires",
"phantoms",
// Human
"U.S. Congressmen",
"performance artists",
// Inanimate, non-mechanical
"pieces of fencepost",
"barnhouses",
// Mechanical
"robots",
"cyborgs"
);
?>
is there a way to reach into that file using Javascript an extract a value?
Or, come to think of it, would it be better to store the list of victims as an XML file so both the PHP and Javascript can read it? Or even, how would I go about doing that?
Read up JSON and check out json_encode() for PHP5.
You can convert a PHP array into JSON, this is a string. Then print that string out into your page and use it with your Javascript
<script>
var jsonarray = <?php echo json_encode($array); ?>;
// now you can use jsonarray in your javascript
</script>
This is especially useful when returning data for an Ajax request
<script>
var jsonarray = <?php echo json_encode(array("status" => "success", "message" => "Something was completed OK")); ?>;
</script>

re-fetch request in php and javascript

here it goes;
echo "<a href='#' class='thumb'><img class='thumb-img' value = ".$row->aid." onclick='getVote(".$row->aid.", \"".$row->atitle."\")' src='images/roadies/th".$row->aid.".jpg' /> </a>";
the above function sends the "$row->aid" value to a javascript function through ajax.
in the javascript however, i want to make a function that needs the ++value of the $row->aid variable. i want the php to get the new value and then pass it again to javascript.
how do i do it without a page reload?
to make things more clear, i just need to get the next incremented value of the php variable. i want php to get the next ++ value from the DB and pass it back to JS.
please help me do this. ;))
Maybe I'm misunderstanding,
But can't you just have a PHP script that returns a string, or a JSON object containing the value(s) you need, and then have the javascript function grab it from the server and update the properties in the link/image?
Also, typically you would get $row from mysql_fetch_assoc() or a similar function -- by calling the server again, re-querying the database, and iterating over mysql_fetch_assoc the necessary number of times, you're probably wasting time. Wouldn't it be easier for the PHP script to grab all of the rows, store them in JSON objects, and then (rather than using AJAX) having the javascript iterate through the JSON object and update the properties on each click?
update:
JSON is a text-based way to represent an object in JavaScript. Think of it like a multi-demensional array. http://en.wikipedia.org/wiki/JSON
You'll have a PHP script write a JSON object to a variable, which you would echo into a <script> tag like echo "var rows = {$JSON_obj};";. In javascript you would start with rows[1] and then each time the link is clicked, move to the next rows[i] or whatever.
jQuery is your friend here. It'll make AJAX (for wherever else you use it) and changing HTML elements much easier. I moved to jQuery and have never, ever looked back to the old ways. http://jquery.com/
I don't have an example on hand right now, but it's a pretty simple concept. I'm sure you'll be able to code this up with little effort.
you can write it to the standard output in PHP via echo $value; and finish PHP script there. javascript will then fetch the value and return it in the AJAX processing function.

What's the best way to send JavaScript array to PHP script using GET?

I have an interactive web application powered by jQuery where users can manipulate visual objects on the screen. When done, the "state" of JavaScript objects should be sent to PHP to store into database. I'd prefer to use GET for this, but solution that uses POST to submit the data is also viable.
I'm currently thinking to serialize all JS objects using something like base64 encoding and just use something like:
var str = encode_objects();
document.location = 'result.php?result='+str;
However, something tells me there has to be some, more elegant, way than writing my own base64 encoding function in JavaScript. Isn't there something already built into JavaScript that can do the job?
Update: decoding in PHP is not the problem. I know how to decode base64, JSON, whatever in PHP. The problem is how to encode data on JavaScript side.
AJAX is out of the question. It has to be a clean GET or POST with page reload.
json_decode() should serve you well here.
If you simply append a string containing the JSON representation of your javascript object to your HTTP request, you can json_decode() it in PHP and have a PHP object ready to go.
Edit: I should mention that this page has a link to a Javascript JSON stringifier, which will convert your JS objects into the necessary JSON.
first of all, if you're updating the objects state, it should be a POST. try to keep all your GET idempotent.
second, even if you don't want to do any AJAX, JSON is still your friend. the easiest way would be to serialize your objects into JSON and send the resulting string in the POST dataload. there are many ways to decode JSON in PHP, and you're all set.
You can use this serialize function, and then unserialize it on the PHP end.
GET or POST will probably depend on the size of your data : there is a limit on the amout of data you can pass through GET (something like 2000 bytes ; more, depending on the browser).
There is also an other thing to take into account : if you are modifying data, you should use POST ; not GET, which is used to... get... data.
About the format, I would really not go for anything like a custom function doing any kind of stuff like base64 : I would definitly go for JSON, which is a native Javascript notation.
You can for instance have a look at :
wikipedia
http://www.json.org/
There are many JS libraries that can generate JSON (probably every JS Framework can ; and there are standlone libraries like this one) ; and since PHP 5.2 there are functions in PHP to encode/decode it (see json_encode and json_decode)
If it's just a simple flat array you don't need to do anything fancy, as PHP has a built in feature to parse array syntax from GET/POST variable names. Rough example below.
Javascript side:
// Do the parameter-building however you want, I picked the short/messy way
var arrayvalues = [1, 2, 'a'];
var querystring = "var[]=" + arrayvalues.join("&var[]=");
querystring += "&var[something]=abcdef";
// querystring is now "var[]=1&var[]=2&var[]=a&var[something]=abcdef"
PHP side:
var_dump($_POST);
// Remember to validate the data properly!
if ( is_array($_POST['var']) ) {
count($_POST['var']);
echo $_POST['var']['something'];
array_map('do_something_interesting', $_POST['var']);
}
I've used Jquery/JSON.
var jsonArray = $.toJSON(jsArray);
Then sent it via JQuery/Ajax

Categories