I'am using dataTables plugin for my table, and it has an json data from the database like :
{"sEcho":0,
"iTotalRecords":1,
"iTotalDisplayRecords":1,
"aaData":[["contentFieldA","contentFieldB","contentFieldC"],
["contentFieldA","contentFieldB","contentFieldC"]],
"sColumns":"fieldA,fieldB,fieldC"}
for some reason I need to remove the "aaData" and "sColumns" value on the first index (so "contentFieldA" and "fieldA" should be removed). and the json data will be :
{"sEcho":0,
"iTotalRecords":1,
"iTotalDisplayRecords":1,
"aaData":[["contentFieldB","contentFieldC"],
["contentFieldB","contentFieldC"]],
"sColumns":"fieldB,fieldC"}
Can anyone help me.. Thanks
If you have :
var test = {"sEcho":0,
"iTotalRecords":1,
"iTotalDisplayRecords":1,
"aaData":[["contentFieldA","contentFieldB","contentFieldC"],
["contentFieldA","contentFieldB","contentFieldC"]],
"sColumns":"fieldA,fieldB,fieldC"};
aaData is an array of arrays, so remove first index of each subarray like this :
for (var i=0;i<test['aaData'].length;i++) {
test['aaData'][i].splice(0,1);
}
deleting first index from sColumns is a little bit harder, since it is a string :
test['sColumns']=test['sColumns'].split(',').splice(1,2).join();
Notice that splice(1,2) is hardcoded, if you have 4 columns it should be splice(1,3), 5 columns splice(1,4) and so on.
Running those two cleanups will give the desired result.
Since it is in JSON, you can use regular expression and remove what you want:
PHP: http://au1.php.net/preg_replace
JS: http://www.w3schools.com/jsref/jsref_replace.asp
Related
In the current version of my project, I can only get a cell value using getCell("A4")->getValue(), but there's a problem, sometime I want to delete or add a column, it will disrupt other values, so I have to sort my data again.
I want to know if there is a function, that can give me cell using using a number as a column insted of a letter, exemple: "A4" from $title['first'][4], and "B5" from $title['second'][5]
image of exemple
Maybe my expression is not clear enough,I hope you can get my point/question, thanks!
$colunmn=1; //The colunmn you want to start the cicle
$columnMax=5; //The colunmn you want to end the cicle
while($coluna < $columnMax){
$colunmString = PHPExcel_Cell::stringFromColumnIndex($colunmn); //Convert the nÂș index into a collunm string ex: 1->'A', 2->'B', 27->'AA', 28->'AB'
getCell($colunmString. "2")->getValue(); //I used "2" as a line exemple you can change it
$colunmString++;
}
You can adapt that code to your needs.
Just use the function PHPExcel_Cell::stringFromColumnIndex( int ) to get the column string with an int index.
This is example what I mean:
I wanna grab result from this url web1.com/do.php?id=45944
Example output:
"pk":"bn564vc3b5yvct5byvc45bv","1b":129,"isvalid":true,"referrer":true,"mobile":true
Then, I will show data result on other site web2.com/show.php
But I just wanna see data value "pk", "1b" and "isvalid". I don't need "referrer" and "mobile" data.
So, when I access web2.com/show.php, it just show data like this:
bn564vc3b5yvct5byvc45bv 129 true
file_get_contents web1.com/do.php?id=45944
Grab this result "pk":"bn564vc3b5yvct5byvc45bv","1b":129,"isvalid":true,"referrer":true,"mobile":true
Filter and show value "pk", "1b" and "isvalid" on web2.com/show.php
So, can you help me with simple php code/script?
Sorry if you don't understand because my english.
Is this source providing JSON-formatted data? That is, with { }'s around it?
If so, use PHP's built in json_decode function. (Man page at http://php.net/manual/en/function.json-decode.php) It will parse the JSON data for you and return an associative array. For example
$your_JSON_data = '{"pk":"bn564vc3b5yvct5byvc45bv","1b":129,"isvalid":true,"referrer":true,"mobile":true}';
$your_array = json_decode($your_JSON_data);
echo $your_array['pk'] . ' ';
echo $your_array['1b'] . ' ';
echo $your_array['isvalid'];
If, for some reason, there are no JSON-style curly braces around your data, you can append them.... for example,
$proper_JSON = "{$bad_json_data}";
however, if that's the case, I'd wonder why it wasn't properly formatted in the first place. In that case it may be safer to use the PHP explode function (http://php.net/manual/en/function.explode.php)
========== EDIT: ==========
Based on the below question, and below answer to use JSON. I'm rephrasing the question.
How can I take data from boto dynamo and jsonify it?
Right now I have something like this:
adv = #my advertiser
ads = self.swfTable.scan(advertiser__eq = adv)
arr=[]
for a in ads:
arr.append(a)
str = []
for i in arr:
str += [json.dumps(fields) for fields in i]
if str is not []:
json.dumps([ str.to_json() for ad in str ])
How do I turn this into a nice JSON dump or otherwise send it to my php?
========== Original Question: ==========
Forgive me I'm new to PHP.
So I have a stringified array of objects.
Ex:
Array [{cat,bat},{mat,hat}] -> ["cat","bat","mat","hat"] (let's call this aList below)
If I know each object pair will have a length of two. Is the only way to reform this Array by parsing the string? Is there any clever PHP way to do this?
I'm trying to move data from python to PHP in this case and sending a printed array seemed like the best / most universal way for me to write the api connection.
Here is my solution in pseudocode:
aList = file_get_contents(myUrl)
splitList = aList.split(",") # is there anyway to exclude "," from being also noticed? ex "app,le","blueberry" should only split 1x?
objects=[]
newObject{}
for int i =0;i<splitList.len; i++
if i%2
newObject.append(splitList[i])
objects.append(newObject)
newObject = {}
else:
newObject.append{list[i]}
Is there are way to do this in fewer lines / more efficiently? Also as mentioned above: is there anyway to exclude "," from being also noticed? ex "app,le","blueberry" should only split 1x?
You really should consider cross-language serialization, like JSON or MessagePack. As an example, see docs for PHP's json_decode and Python's json.
I am reading csv file in php Having 2 columns in it. what i want is when i enter any character from first column from csv in text box then i want related values of second column from same csv file to be come in drop down list. i am quite new to use ajax not get any such type of example.
Can anybody help me?
Thanks..
Here's an example you're looking for: http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/
Not quite sure what you want to do, but if you are using jquery, then ajax is very simple. You can use:
var sChar = 'c';
$.get('yourPhpFile.php', { sCharacter:sChar }, function(sData){
alert(sData);
});
This way you call yourPhpFile.php, sCharacter is available in yourPhpFile.php as $_GET parameter, and sData holds the output.
See http://api.jquery.com/jQuery.get/
IP.Nexus puts custom fields into a table all muddled up into one row and some how pulls the correct data out.
basically my custom fields row looks like so:
a:2:{i:2;s:4:"Test";i:3;s:10:"Accounting";}
How do i select what item to pull as i would like to out put just one item not the whole row, is it with delimiters or string splitter, as i am novice with this part of mySQL
Cause i would like to echo out just the advertising on its own and also vice versa for the testing.
Thanks in advance
It's a serialized array using serialize() function so you can get its contents using unserialize($YourString);.
But I see that you string is corrupted for deserialization, because s:4: in s:4:"advertising" says that after s:4: 4 character long string is expected that advertising is not.
The data in the database is in serialized(json) form.You can turn it into simple array using unserialize.
For example;if $mystr is your string
write
$mystring=unserialize($mystring)
You will get normal array of this string
You can do
foreach($mystring as $str){
$ads[]=$str['advertising'];
}
and the resulting array $ads will be the ads you like in simple array form
Since this looks as JSon, you can use a PHP JSon module to parse each row from MySQL.
<?php
(...)
while (...) {
$obj = json_decode($row);
// do something with $obj
}