json_encode(): Sending both a Table and Variables from PHP to JavaScript - javascript

From my PHP script I have to send to JavaScript two pieces of data in one AJAX return call:
relatively large array with app. 200 rows and 10 columns
set of variables providing additional information about that array
Both pieces of information should be returned from PHP to JavaScript in one JavaScript return call.
A standard way of sending arrays from PHP is by using json_encode(). This function however accepts only one parameter, either a string or an array.
How to, please, send both the table and variables from PHP to JavaScript then? If json_encode() wont handle this task, is there any other reliable way?
I have tried combining the variable set and the table into one array, and passing that array to JavaScript. This worked, however, when I wanted to get lenght of such array, where table and variables were combined, it simply did not work. Which is the reason, why I came to consult this problem here.
Thank you very much.

You can package the table array and the variable set array in to one single array:
$package = Array($dbarray, $vars);
$json_data = json_encode($package);

Related

Force serial mySQL insertions (or rebuild a table a better way)

I'm new to mySQL transactions and I'm working with some code that I inherited, but the question is more fundamental capability related than code specific.
I have a js array that I want to insert into a mySQL table. The
table size will never exceed 200 entries, most often fewer than 100.
Presently, I'm looping through the js array and sending individual
AJAX requests for each array element to an existing PHP file that
processes them as they arrive and does the sql INSERT call for each
element. async is set to 'false' in the js AJAX call.
Everything is working successfully – no INSERTS fail and no data is
lost so this isn't a code failure question – but when I later get the table
from the database and load it into an array, ordered by updated (added), the sequence of the entries is wrong, and that's an issue.
So my question to the mySQL experts is this: is there a simple handshake function I'm not seeing that will ensure that mySQL insertions are processed in order?
If the answer is no, I will need to add a sequence number to each request and add a column for it in the table. Also, if I'm going to make changes to the inherited code anyway, I'm open to pointers on what to look into for a more efficient solution.
Thanks.
As Paul Spiegel has said it's better to use an auto_increment if all you want is a repeatable sequence of events.
I am assuming that when you say 'order by updated (added)' then that is a DateTime field or some such on the table. If your inserts happen fast enough then there will be multiple rows on the same insert date/time (the field only goes to One Second resolution) and MySQL doesn't guarantee you will always get the rows back in the same order when that happens.
You could insert all your array at once:
INSERT statements that use VALUES syntax can insert multiple rows. To
do this, include multiple lists of comma-separated column values, with
lists enclosed within parentheses and separated by commas. Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Read more here.

Passing a PHP array via Javascript to another PHP script

I may be going about this the hard way, as I modeled my code after an older article: populating triple dropdown list but I have found I don't need to query the database, because all the information is already neatly stored for me in a PHP array used earlier in the script.
I have a PHP array already created ($events):
<?php foreach($events as $event) {
Now I have to pass the events array to another file for processing:
<select name="search_month" onChange="getMonth('filter-leaderboard.php?month='+this.value)">
I'm just not sure how to pass it via Javascript.
How do I go about doing that?
Thanks!
Looking at your question, it appears you're trying to get the month using AJAX?
If so, it'd probably be better to post the array as the body rather than doing a get request.
You can use the json_encode function (http://au2.php.net/json_encode) to convert the array into a JSON object so you can work with it in JavaScript.

best way to relate a variable and its ID

I am getting results from a database with a simple php while loop, one of the pieces of information is a number that links to another table where the value is stored, I can think of plenty of ways to get this information linked and display the text related to the value but I want to know the fastest way to do it as I have a huge set of results so every bit of speed will make a difference. Is an array fastest, javascript? any advice you can give me would be great.
The schema would look something like this
col_table
colID(autonumber) colName(str) colState(int) colDate(date)
state_table
stateID(int) stateType(str)
I want to select the correct state type based on the colState matching a stateID and output the stateType while preserving the stateID for so I can edit the field and update the database using the number.
Using MySQL will be faster.
If you have to get through a PHP loop to read your results and make each time a new MySQL request, your script will take longer.
You can increase speed on MySQL by creating the right kind/amount of index, choosing wisely what is store in each field.
The later you parse content, the longer it will take. If you go for js, you will have to read a DB, loop trough it in PHP and do it again in JS, and making more request again ...
A join can be a good solution. A view can be even more easier to treat. Yuo can also consider caching results
Use a timer in php and try trial and error method. Use the time returned by the timer to evaluate speed and efficiency.
you should prepare your data on server side it is faster.
Whether you choose your server or database with a fast query it depends. If you have complex object graphs then the processing of results from db in order to create associations would be time consuming so an ORM is the way to go, otherwise as is your case with a simple join i would simply retrieve all data from db.
If you use php for rendering as well then render it using php no js.
If you use js for your ui then prepare data on server side and publish it via a REST webservice in json,i.e. usind json_encode functions of php, then retrieve it from js and output.

Get array from separate PHP file and store into jQuery array

I've searched around and wasn't able to find a clear path for accomplishing this.
I'm familiar with using json_encode() and json_decode() for converting php arrays to and from json, however I'm stumped about the below scenario:
I have a user interactive page where they need to access and use data from one of dozens of php array files. I have a php file (get-array.php) that determines which array to send back based on what the user has been selecting.
Normally I would just use .load() to load php results into a div, but I need the array back and stored in a jQuery array. What would be the best method for doing this?
Many thanks!
Use http://api.jquery.com/jQuery.ajax/ or the shorthand method, http://api.jquery.com/jQuery.get/ to retrieve JSON.
Or even better, this shorthand method is made just for this: http://api.jquery.com/jQuery.getJSON/
When you have your PHP array a d are ready to send it, you can use the JSON encode method of PHP to prepare it as JSON: http://php.net/manual/en/function.json-encode.php

Traverse array list in Javascript file

In my Struts 2 based application, I have an ArrayList. How can I send this ArrayList to a Javascript file so that I can compare list item with element of jsp and show some message based on validation? How can I use the array list data in Javascript file.
I do not want use JSON.
JSON comes to mind - not sure of this example so here is one more
you can use <logic:iterate> and iterate through your List, here is example
Your question makes it seem like you want to send the ArrayList to the client directly, this means it will not be processed by a JSP. The easiest way to do this is with JSON, then XML, then perhaps some custom built format which you'd need to parse with JS.
But since you then say that you want to compare a value from the action class to a value in the JSP this means you'll want the comparison done when composing the view on the server and not on the client. For this see the struts2 tag documentation the <s:iterate> and <s:if> tags will let you do this.
As your question stands the two ideas are irreconcilable without some guess work.

Categories

Resources