|
 |
|
 |
 |
|
03-08-2008, 05:37 PM
|
#16 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
k i'll continue not to help you. json_encode( $array );
__________________
testing 1 2 3
|
|
|
03-08-2008, 06:36 PM
|
#17 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
haha, not helping always helps. i don't see how json_encode will help me. what did you have in mind?
__________________
|
|
|
03-08-2008, 07:21 PM
|
#18 (permalink)
|
|
Senior Contributor
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
|
What he's suggesting is to encode the returning array (from the database query) as JSON and have the JS code parse that. You can use eval() to take the JSON string from the XHR result and 'objectify' it (that is, turn it into an object JS can use) or use the very handy (and more secure) JSON parser (which I use). It should be trivial to run through each element in the object to populate the 'potential matches list'.
If the PHP JSON functions aren't available to you, it's easy enough to create a JSON string and return it, e.g.
PHP Code:
// for example, the string sent to the PHP script to check is 'do'
// let's say this is a list of potential matches
$array= array(
'dodge ball', 'dogs', 'dog parks', 'douglas adams'
);
// begin the JSON object string and result list
$json= '{ list: [';
// run through each element in the array to populate the list
foreach ( $array AS $k => $v ) {
$json.= "'{$v}'";
$json.= ($k < sizeof($array) -1) ? ',' : NULL;
}
// end
$json.= ']}';
// output & exit for argument's sake
echo $json;
exit();
The resulting JSON object (indented by me for clarity):
Code:
{
list: [
'dodge ball',
'dog parks',
'dogs',
'douglas adams'
]
}
__________________
|
|
|
03-08-2008, 07:31 PM
|
#19 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
will json work for md arrays?
this is what ive been working on so far.
Admin
it only works for the upper left text input, and its onchange set to do the search. and while on the subject, in firefox onchange only gets triggered if you hit enter, or leave focus on the box. i want it to get triggered literally onchange when the user types a character
__________________
|
|
|
03-08-2008, 07:44 PM
|
#20 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
HOLY SMOKES, blast from the past  How's life BDL? Good to see you around.
yes, json works in multi-dimensional arrays.
i.e.
PHP Code:
<?php
$array['foo']['bar'] = 'moo';
echo json_encode($array); ?>
to access moo in javascript, it would be something like: data.foo.bar
to add on to what bdl is saying, it wasn't available in php 4.x, and maybe not even utnil 5.2 (i'm not looking at the man page for it so i'm not sure)
still though, if you didn't have access to the later versions of php, there are scripts you can find that define a json_encode() function to use. that is if you didn't feel like building it manually.
__________________
testing 1 2 3
|
|
|
03-08-2008, 07:52 PM
|
#21 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
i think i'm going to stick with what i've got the first half of the script is like 3/4 done. redoing parts of it, just seems kind of counter-productive.
__________________
|
|
|
03-08-2008, 08:40 PM
|
#22 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
so writing code that is easier to maintain, not to mention the learning aspect, is counter-productive?
think back to when you were writing flat files for all your data and how much easier it was when you spent a small bit of time understanding mysql.
if the objective is just to get your site working, and you're almost there, then i suppose it doesn't matter.
__________________
testing 1 2 3
|
|
|
03-09-2008, 09:25 AM
|
#23 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
i don't think the requirements of this section will change or need to be expanded with time. but for the time being, i think i see my manager in only 2 days, i'd like to have a much better presentation than i had the last time(all dynamically created pages, but no css, and no way to update the tables. it was rather pathetic)
__________________
|
|
|
03-09-2008, 05:49 PM
|
#24 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
while taking on this feat, its amazing how much javascript and php can work hand in hand. i never really saw any really strong connection between the two languages before
__________________
|
|
|
03-09-2008, 07:13 PM
|
#25 (permalink)
|
|
Senior Contributor
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
|
Hey- what's up Mike.
falsepride> You might be interested to know that several PHP extensions (including some PEAR or other library classes) allow PHP to interact with most other environments. Using the JSON extension really isn't specifically a way for PHP to interact with JavaScript, it's just a shortcut to encode or decode a JSON string, which really could be used as a lightweight protocol to interact with anything, much the way XML is used.
I for one, am glad to have found JSON as an alternative to XML, as I can't stand dealing with it (as a web service, anyway).
__________________
|
|
|
03-10-2008, 11:01 AM
|
#26 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
i never really used xml, or understand it. and my reference was to xmlhttprequest, not JSON at all. i was amazed that one could pass variables between the two(using xmlhttprequest) without forcing end user to load more pages.
__________________
|
|
|
03-10-2008, 03:22 PM
|
#27 (permalink)
|
|
Senior Contributor
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
|
XMLHttpRequest is named, well, for the ability to retrieve XML via a web service (afaik). But you can obviously transfer plain text, which is what JSON is at that point. It's just text until your JS code 'objectifies it'.
Take a look at the XHR API. Note there are two native response mechanisms, responseText and responseXML. If you want to use plain old XHR code, you'll return the JSON string with the former of those two and then use one of the two methods I outlined in an earlier post.
Otherwise, if you're using a JS framework like jQuery (which I'm fond of) or prototype, they both offer methods to actually take JSON and handle it so the object comes out the other end.
__________________
|
|
|
03-10-2008, 05:41 PM
|
#28 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
i've actually already read the wikipedia page on it. so far i've been returning a plain text. returning a definition of a javascript array, then evaling the array so it becomes one in my javascript code. for now im going to use that, then when everything else is done, experiment using the JSON php function
__________________
|
|
|
03-13-2008, 03:11 PM
|
#29 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 192
|
i started re writing my script. having javascript cache the returns from the sql table to reduce strain on the server from having to search the table every time the user types a new character. i wrote this function to search a multi dimensional array, for a string. return an md array containing all the matches, or return -1 if no matches are found.(i picked -1 because this function is meant to sorta mimic .indexOf)
Code:
function searcharray(searchkey, searcharray)
{
var indices = [];
var luck = false;
for(var i = 0; i < searcharray.length; i++)
{
for(var i2 = 0; i2 < searcharray[i].length; i2++)
{
if(searchkey == searcharry[i][i2].substr(0, searchkey.length))
{
document.write('what the ?!');
luck = true;
indices.push(searcharray[i]);
}
}
}
if(luck == false)
{
document.write('this just is true');
indices = -1;
document.write(indices);
}
document.write(luck);
return indices;
}
every time i run the script, neither of the document.writes executes. i must have a simple error staring me in the face somewhere. any help?
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:17 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|