Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > HTML, XML, Javascript, AJAX
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-08-2008, 05:37 PM   #16 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
k i'll continue not to help you. json_encode( $array );
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-08-2008, 06:36 PM   #17 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
haha, not helping always helps. i don't see how json_encode will help me. what did you have in mind?
__________________
falsepride is offline   Reply With Quote
Old 03-08-2008, 07:21 PM   #18 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
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' ] }
__________________
bdl is offline   Reply With Quote
Old 03-08-2008, 07:31 PM   #19 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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
__________________
falsepride is offline   Reply With Quote
Old 03-08-2008, 07:44 PM   #20 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 03-08-2008, 07:52 PM   #21 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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.
__________________
falsepride is offline   Reply With Quote
Old 03-08-2008, 08:40 PM   #22 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 03-09-2008, 09:25 AM   #23 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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)
__________________
falsepride is offline   Reply With Quote
Old 03-09-2008, 05:49 PM   #24 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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
__________________
falsepride is offline   Reply With Quote
Old 03-09-2008, 07:13 PM   #25 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
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).
__________________
bdl is offline   Reply With Quote
Old 03-10-2008, 11:01 AM   #26 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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.
__________________
falsepride is offline   Reply With Quote
Old 03-10-2008, 03:22 PM   #27 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
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.
__________________
bdl is offline   Reply With Quote
Old 03-10-2008, 05:41 PM   #28 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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
__________________
falsepride is offline   Reply With Quote
Old 03-13-2008, 03:11 PM   #29 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
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?
__________________
falsepride is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form questions JDE HTML, XML, Javascript, AJAX 2 10-29-2005 01:52 AM
Child Form to Parent Form WmFenn MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2005 08:10 AM
Passing Values from Popup Form to Main Form chrislopezz PHP 7 03-28-2005 12:45 PM
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 03:17 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle