jQuery Autocomplete Remote - php

I'm using this http://jqueryui.com/demos/autocomplete/#multiple-remote on my website, I can see this code 'searches' search.php does anyone know what format search.php should be in and what it should look like?
Thanks,

From the page you linked, it shows you the expected data format. (pasted below) At it's simplest, you can have a print statement in the search.php file that simply echoes back some hard coded contents. A more elaborate solution is to have your search.php pull real time from a database and then format the data as expected.
Expected data format
The data from local data, a url or a callback can come in two variants:
An Array of Strings:
[ "Choice1", "Choice2" ]
An Array of Objects with label and value properties:
[ { label: "Choice1", value: "value1" }, ... ]
So, just to get off the ground and see it working, use this line for search.php and build from there by customizing your choices or connecting to a db etc.
print '[ "Choice1", "Choice2" ]';

Related

How to index document pages that can be retrieved later into elasticsearch

I am indexing PDF documents in elasticsearch without using the official plugin, I am using a PHP library to parse PDF content into plain text. This PHP library allows me to get the document content by pages so I would like my search page would retrieve some highlight similar to:
[Page 1] ... Highlighted text from the search ... [Page 4] ... Highlighted text from page 4 that match with the search ...
The mapping they gave to me is like this, I just converted text from string to array:
properties: {
highlight:{
text: [ "Page1Content...", "Page2Content...", "Page3Content...", ...],
other_fields: {}
},
other_fields: {}
}
But I cannot find a way to get the array indexes when getting the highlighted content, it get lost in the way.
Are nested / objects the only way to know the page number when I search? I don't know if array keys are lost when highlighting too. I thought in something like or similar:
highlight : {
text: {
"Page1" : "Page1Content",
"Page2": "Page2Content",
....
},
other_fields: {}
}
Thanks in advance.

How to properly count json contents?

This is my json
[
{"id":"1736375","first_name":"fname1","force_first_name":"ffname1","last_name":"lname1","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},
{"id":"1607011","first_name":"fname2","force_first_name":"ffname2","last_name":"lname2","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},
{"id":"1607012","first_name":"fname3","force_first_name":"ffname3","last_name":"lname3","thumb_path":"","path":"img\/profiles\/generic\/gray.png"}
]
I am trying to count the number of sets inside [ ]
I tried using this
echo count(json_decode($people, true));
I get zero (0) result.
How can I properly count it.
Thanks
=== EDIT FOR THE SAKE OF FUTURE VIEWER ===
it is the json that is malformed as stated by several comments, the code i wrote above is how I see it but the real content of the json was this
string(3)"
[
{"id":"1736375","first_name":"fname1","force_first_name":"ffname1","last_name":"lname1","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},
{"id":"1607011","first_name":"fname2","force_first_name":"ffname2","last_name":"lname2","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},
{"id":"1607012","first_name":"fname3","force_first_name":"ffname3","last_name":"lname3","thumb_path":"","path":"img\/profiles\/generic\/gray.png"}
]"
as pointed out by #dontpanic, a string will always return 1 which is what I get. I reported the problem to the developer and luckily they corrected the json response and now it is working OK.
Thanks to all who made an effort to comment leading to the discovery of the problem.
Using this exact code I get the desired output of 3, as well as the other comments above. I'd recommend debugging the $people variable to ensure that it is remaining a json object all the way through until your echo statement, as it is quite possible it is either being malformed or changed all together, therefore giving you unexpected results.
<?php
$people = <<<EOD
[
{"id":"1736375","first_name":"fname1","force_first_name":"ffname1","last_name":"lname1","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},
{"id":"1607011","first_name":"fname2","force_first_name":"ffname2","last_name":"lname2","thumb_path":"","path":"img\/profiles\/generic\/gray.png"},
{"id":"1607012","first_name":"fname3","force_first_name":"ffname3","last_name":"lname3","thumb_path":"","path":"img\/profiles\/generic\/gray.png"}
]
EOD;
echo count(json_decode($people, true));
Try:
echo count(json_decode(stripslashes($people), true));

how to get values of an array that is inside an object?

i've got an object that contains two elements, the first is a string, the second one is an array, i need to return only the array.
here is the object :
{
"return_code": 0,
"response": [
{
"tid": "30",
"categorie": "Fish"
},
{
"tid": "31",
"categorie": "Birds"
}
]
}
I want to return the "response". Any help please ?
Thank you.
Here's the basic idea based on new comments:
Decode the JSON string you're getting using json_decode in PHP. This will give you a PHP object.
Create an empty list to contain the elements you want in the result.
For each element in the response property of the decoded JSON object, add response.categorie to your list container.
Return the now full list
Your data looks like JSON, so I will propose a solution in jq ("Json Query") - a language that is readily available on most modern computing platforms -- see https://stedolan.github.io/jq/
The originally posted task was to find the array. In jq, this can be accomplished using the program: .response
Subsequently, the task was revised to extracting the "categorie" values.
This can be accomplished using the program: .response[] | .categorie
Here is an example, assuming your input is in a file named "input.json":
$ jq '.response[] | .categorie' input.json
"Fish"
"Birds"
Or, if you want to gather these values as a JSON array:
$ jq - '[.response[] | .categorie]' input.json
["Fish","Birds"]

JSON incomplete variable

for some reason I have a problem getting a JSON input into PHP. Basically, I am importing a variable from a url-encoded JSON, the bit of code I have problem with looks like this:
"nearest_area": [
{
"country": [
{"value": "Czech Republic"}
],
"region": [
{"value": "Moravskoslezsky Kraj" }
]
}
]
When I import and JSON_decode it in PHP, I used the exact same way of getting the two variables. For country I used
data->nearest_area[0]->country[0]->value;
and I got Czech Republic, for the other one I used region instead of country, but for some reason instead of Moravskoslezsky Kraj, I am always getting just the first word - "Moravskoslezsky".
The only reason I could think of what could be causing the problem is encoding. In fact, in Czech, the actual name of the region ("kraj") is "Moravskoslezský". I used the UTF8 decoding procedure and indeed I am getting the proper "ý" at the end instead of "y", but then it just skips the rest....
Any ideas what could be wrong?
OK, sorry I have figured it out, there was a problem with encoding the actual url.

json_decode fails

I'm fairly new to json, and I'm having an issue with json_decode. I think I know why, but I haven't been able to sort out how to fix it.
Basically, I have a URL that supplies json info. I grab it using cURL, and return it as a PHP variable, and that's working just fine. I can print_r out all the info I want. However, when I use json_decode($json, true), it returns NULL.
I THINK it's because, technically, what's being returned is not a string, but more like an object - and I can't sort out how to grab the contents of that object.
For example, when I return the json stuff as a php variable:
print_r($json);
The output returned looks like so (I won't do it exactly, because it's HUGE, so I'll show you the layout to keep it simple)
MyThing.returnedItems({MyThing.returnedItems({
"projects":[{
"completed":"2010-12-21",
"status":"finished",
"favorited":0,
"started":"2010-12-20",
"percentage":78,
"permalink":"slug to post",
"size":"One size",
"thumbnail":{"src":"full path to full size image",
"medium":"full path to thumbnail"},
"name":"Some title here",
"notes":"description here",
"url":"URL to page",
"comments":0},
So you can see it's like a nested array. I don't mind that, but I'd like to be able to access all the key/value pairs of these arrays as PHP variables. But it seems because of the "MyThing.returnedItems()" surrounding it, it doesn't see it as a string to decode, so I get a NULL value every time.
Anyone know what I'm missing here? Once I figure out how to grab the stuff inside there, I think I've got it (simple foreach or whatnot to get the rest of the variables as needed), but I just can't seem to get in there.
This is valid JSON
{
"item1": [
{
"something": [],
"something else": "some value"
}
],
"another fun thing": [
{
"more fun": "fun value 1",
"even more!": "fun value 2"
}
],
"item2": {
"another thing": "another value"
}
}
This is not!
MyThing.returnedItems({
"item1":[{"something:[],
"something else": "some value"},
"another fun thing": [{"more fun": "fun value 1",
"even more!": "fun value 2"}]
],
"item2":{"another thing": "another value"}
})
Its a javascript method call
Okay, I just wanted to add that you all REALLY helped me out. Especially MaX, because by knowing the "official term" of what was happening, I had better searches and ended up finding some really interesting code that eventually landed me my solution. However, I did discover the reason why I was having my json wrapped in that weird function method call: the URL that the site gave me to access their API actually had that call in it, so it was returned wrapped in it. In other words, the json file URL I had was like so:
somesite.com/apicall.json?key=1234567890&callback=MyThing&version=0...
so as soon as I removed that "callback" section - BAM the json was no longer wrapped, and a whole new world of foreach functions opened up to me. So, even though the solution ended up being a REALLY stupid oversight on my part, I wanted to thank you all for your input, because I learned a whole lot of stuff I wasn't planning to today XD
Oh, and the code that actually ended up working (after I got rid of the callback part of the URL, because json_decode was still returning NULL on me) was this:
$data = json_decode(file_get_contents($json), true);
print_r($data); // array of everything available for me to mess with!
Thanks again for your help, everyone :) I REALLY appreciate it!

Categories