Jump to content


Help! Ajax + Live Search


34 replies to this topic

#1 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 05 January 2006 - 11:04 AM

I am trying to development a live search result snippet, but right now i'm just starting with the basics, so instead of making it a snippet it's located in it's own .php file (this may be the only way it works).

Anwyays, Basically I have javascript that sends data to "/livesearch.php"
http://www.ordorica.org/livesearch.php

with the search string $lookup;

so the javascript does this:

http://www.ordorica.org/livesearch.php?lookup=XXX

where XXX is some search string.

then I do a $_GET['lookup']; in my livesearch.php to grab the string and attempt to use it to search through the etomite database.

my livesearch.php right now is heavily based off of FlexFormSearch and the Normal search snippet.

However, I get this error because I can't access the etomite database i'm guessing?

Fatal error: Call to a member function on a non-object in /www/o/ordorica/htdocs/livesearch.php on line 251

Line 251:
$snippetRs = $etomite->dbQuery($snippetSql);

Please help me out. I just started learning php and I don't know much about databases either. Anyways, if I can figure this out I would like to make a live search snippet.

Here is my demo test page:
http://www.ordorica.org/index.php?id=106

I at least know that the search string makes it into the system because I have printed an echo of it out which updates nicely as you type. The search bar is in the top left of the page.

Sorry, it may not look or run well, only tested with IE and Firefox. I just started today and this is extremely alpha!

Thanks

Attached Files


Edited by hechacker1, 05 January 2006 - 11:11 AM.


#2 breezer

    Likes Etomite Forums!

  • Member
  • PipPip
  • 308 posts

Posted 05 January 2006 - 12:13 PM

This is caused by not being able to access the required function...since all eto snippets you based your search on are parsed by the index.php file, thats where the relevent functions are located. You may have to copy all the functions needed by your search script and add them to your search script...

#3 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 05 January 2006 - 07:00 PM

thanks! Right before I went to bed last night that was the last thing I thought about. I'll have to try today to make it work. That is why I tried making it into a snippet instead, but then I realized that I have no way to call a certain piece of code within the index.php with my javascript.. Or is there?

Edited by hechacker1, 05 January 2006 - 07:00 PM.


#4 breezer

    Likes Etomite Forums!

  • Member
  • PipPip
  • 308 posts

Posted 05 January 2006 - 07:22 PM

There are a few ways to pull things into etomite, personally I use different variations of the following snippet:

include('forum/SSI.php');

ob_start();
ssi_login($redirect_to = 'http://www.frsbuilders.net/rc3/', $output_method = 'echo');
$output = ob_get_contents();
ob_end_clean();

return $output;

what this snippet does is include the SSI.php file, then grabs the function ssi_login() and echoes the login form. Perhaps you can use the framework to pull the page you want to include into etomite...

You may also try the GrabPage snippet in the snippet library, I have been meaning to test it out but have not had a chance yet.

#5 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 05 January 2006 - 07:29 PM

hechacker1, on Jan 5 2006, 03:00 PM, said:

thanks! Right before I went to bed last night that was the last thing I thought about. I'll have to try today to make it work. That is why I tried making it into a snippet instead, but then I realized that I have no way to call a certain piece of code within the index.php with my javascript.. Or is there?
Yes, using AJAX methods you can make a subsequent call to index.php, and even call a snippet or an API function... The trick is getting the data returned properly so that your Javascript can get access to it if required... This could be done using a cookie, for example, as both PHP and Javascript have access to the same cookies... The big question is whether you really need to use AJAX or just us a snippet... I have several javascripts which allow repopulating specific page elements based on their id if you haven't found or developed them already yourself...

#6 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 05 January 2006 - 10:22 PM

Ralph (rad14701), on Jan 5 2006, 11:29 AM, said:

Yes, using AJAX methods you can make a subsequent call to index.php, and even call a snippet or an API function... The trick is getting the data returned properly so that your Javascript can get access to it if required... This could be done using a cookie, for example, as both PHP and Javascript have access to the same cookies... The big question is whether you really need to use AJAX or just us a snippet... I have several javascripts which allow repopulating specific page elements based on their id if you haven't found or developed them already yourself...

Well my current template uses javascript to insert the search results into the page with a specific <div> id.

For example, in the "content" section of my html I have
<div id="LSResult" style="display: none;"><div id="LSShadow"></div></div>

and it stays hidden until data is passed to it from the search result.

What I need is a string of <div> formatting with the search results contained within. Then that will be echoed out to the correct location in the page.

I am guessing from the above example (thanks breezer) that I could achieve the effect with a snippet call that basically calls my outside livesearch.php.

Livesearch.php's ultimate goal is to take the search string, perform the search, and return a <div> layout of links and results. I also want to show the first few hundred characters of the page in question, similar to the news parsers that etomite has.

I would be greatful Ralph if you could provide examples showing me how to make this work with a snippet, instead of an external php file.

My goal is to be able to provide a live search on any page simply by typing in the search bar. It would provide a nice element of user interactivity.

Anyways, thanks so much for helping me with my first real attempt to make some kind of snippet. Now I have to just get my basic livesearch working.

#7 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 05 January 2006 - 11:23 PM

Here's how I would go about solving your dilema... Create a [!NewSnippet!] that does all of the fetching of the results and formatting for display just as the NewsListing family of snippets do now, and as your external script was intended to handle... Then I'd create an empty template that contains only the [*content*] tag... Next, I'd create a new document with only the [!NewSnippet!] call in it and set that document to use the almost empty template that you just created... You should then be able to call for the new document using either the documents alias or index.php?id=id and then populate your target div with the results... This method will allow you to take advantage of all of Etomites API functions as well as any other snippets or chunks... I hope this makes some sense... I haven't tested the concept but have been kicking it around for a few weeks and I'm relatively sure that it should work... It sounds as though you already have the AJAX functions for requesting and displaying the resulting HTML... B)

#8 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 06 January 2006 - 12:00 AM

Ralph (rad14701), on Jan 5 2006, 03:23 PM, said:

Here's how I would go about solving your dilema... Create a [!NewSnippet!] that does all of the fetching of the results and formatting for display just as the NewsListing family of snippets do now, and as your external script was intended to handle... Then I'd create an empty template that contains only the [*content*] tag... Next, I'd create a new document with only the [!NewSnippet!] call in it and set that document to use the almost empty template that you just created... You should then be able to call for the new document using either the documents alias or index.php?id=id and then populate your target div with the results... This method will allow you to take advantage of all of Etomites API functions as well as any other snippets or chunks... I hope this makes some sense... I haven't tested the concept but have been kicking it around for a few weeks and I'm relatively sure that it should work... It sounds as though you already have the AJAX functions for requesting and displaying the resulting HTML... B)

I did what you said.. and it does work, but I can't seem to get the page to update still.

Just for example the snippet call [!livesearch!] :

return $_GET['lookup'];

and that is called from a blank page id=107 with only [*content*] :

http://ordorica.org/index.php?id=107?lookup=aasdf

The desired result is that "aasdf" should be printed on the page. But it does not.

Does it have to do with etomite's caching? I can hard code a message into the snippet, and it shows up fine. But passing the "lookup" value does not seem to work.

Edited by hechacker1, 06 January 2006 - 12:01 AM.


#9 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 06 January 2006 - 12:43 AM

When using snippets you are not required to use $_GET to retrieve variables passed in the snippet call... Using return $lookup; should work... B)

#10 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 06 January 2006 - 03:54 AM

Ralph (rad14701), on Jan 5 2006, 04:43 PM, said:

When using snippets you are not required to use $_GET to retrieve variables passed in the snippet call... Using return $lookup; should work... B)

Unfortunately it doesn't. Maybe it's just my implementation, but here is the important part of the javascript that enables the AJAX.

liveSearchReq.open("GET", "/livesearch.php?lookup=" + document.forms.searchform.q.value + liveSearchParams2);

You see, livesearch.php?lookup= is expected as the place to feed the search string. I can easily change it to an etomite id page like you suggested:

liveSearchReq.open("GET", "/index.php?id=107?lookup=" + document.forms.searchform.q.value + liveSearchParams2);

But, when I do that the live search no longer works. For some reason I cannot pass the $lookup string into the etomite page with the second example. The Javascript grabs the document.forms.searchform.q.value (which in this case is the search string) and concatenates it with the provided URL. Then it gets called and the results are placed into the <div> on the content page.

I tried just using $lookup but it doesn't equal anything, it has no value. And from within the etomite snippet a $_GET['lookup'] doesn't give me the value either.

Basically I have gotten my implementation working with a separate php file because I can easily pass in the 'lookup' value with the javascript; the etomite snippet and parser are not in the way.

my early version is here:

http://www.ordorica.org/index.php?id=106

It works already using the seperate livesearch.php and javascript. I still need to implement styling, link fixing, and also fetching of the story content, but the headlines and titles show up.

Again, i'd like to remind you I'm a newb at this stuff. I really rather get this into a snippet if I can because it would elminate me having to open the database a second time to access it from the external file.

thanks

Edited by hechacker1, 06 January 2006 - 03:55 AM.


#11 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 06 January 2006 - 04:35 AM

I'll see if I can get around to playing with some code by sometime this weekend... I'm sure I can get it working without much fuss once I get time to work on it without interruption...

#12 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 06 January 2006 - 05:12 AM

Ralph (rad14701), on Jan 5 2006, 08:35 PM, said:

I'll see if I can get around to playing with some code by sometime this weekend... I'm sure I can get it working without much fuss once I get time to work on it without interruption...

thanks, your help is much appreciated.

If anybody else cares to help I am waiting and willing to learn.

#13 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 07 January 2006 - 07:13 AM

I think I have made some kind of progress.

If I make a simple snippet with the code:
return $_GET['id'];

and I make a url like:

http://ordorica.org/index.php?id=110?lookup=abcda

I get this result:

110?lookup=abcda

So I am getting the page id, and also the ?lookup= part is attached as part of the 'id' field.

Knowing this, how can I strip the search string from the end of the lookup=
?

EDIT: I did some research and came up with a possible solution?

$getID = $_GET['id'];

$strip = explode("=", $getID);

echo $strip[1];

It seems to return the input, abeit, slower than going directly through php.

Edited by hechacker1, 07 January 2006 - 07:19 AM.


#14 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 07 January 2006 - 07:30 AM

w00t! my last "solution" worked. But even without the overhead of the extra etomite class for the external php file, the search results are slower because now the search result has to be filtered through the [*content*] and [!livesearch!] snippet. Before they were just directly processed by the external livesearch.php

hmm.. however though, Simply removing the <?php and ?> tags has provided a working snippet.

maybe there is a faster way to do this?


EDIT: borrowing from FormFlexSearch and NewsListingRevised I have finally created an output similar to the NewsListingRevised output, with <fieldset>, message title, link in the title, the first few hundred words of the content, and even the author and creation date. All configurable of course! There are still many variables I need to put up front so that the user doesn't have to dig through the php.

It seems to be working great in an external php file, I have yet to test it out in a snippet.

Edited by hechacker1, 07 January 2006 - 11:17 AM.


#15 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 07 January 2006 - 03:42 PM

Sounds like you're making good progress... I look forward to seeing the working results in action...

#16 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 07 January 2006 - 08:11 PM

Ralph (rad14701), on Jan 7 2006, 07:42 AM, said:

Sounds like you're making good progress... I look forward to seeing the working results in action...

my website already has it implemented. I'm just doing optimization right now because the search results are a little slow when all the content is brought up. If only the title's are needed then it's blazing fast.

#17 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 08 January 2006 - 10:19 AM

well, i am done optimizing the code and making it very user configurable. Howerver, before I release it there are a few issues:

1. When used from an external file I need to include the entire etomite class and create a new etomite object. For example:

include "manager/includes/config.inc.php";
  $etomite = new etomite;

Is there any way to not have to include the entire class? It adds a lot of code, even though the code is never really downloaded by the user (it just processes the output on the server, the user only sees the <div> output.)

2. As a snippet the code does work fine, but it is slower than the external php solution due to the reasons posted earlier in the thread. Is there any way I can speed up the passing of information to the snippet?

3. The content that gets included with search results is stripped to a specific size depending on a variable. But in the case where a document opens a new <table> or starts an <img> if the content is stripped before the tags complete, the search results layout goes crazy. Is there any way to contain each search result to it's own section without affecting the other results? i.e. some kind of container html? This is only a problem with some code. The older portion of my site that wasn't XHTML 1.1 compliant causes the problems. The new portion that is compliant seems to have no problems. FYI

EDIT: I think i just came up with a solution to my own problem for number 3. What I need to do is strip the content to size, then remove any <tags> and just show the text only. Is there any easy php solution to this?

for an example, click here:
http://ordorica.org/livesearch.php?lookup=a

I plan to release it as both an external php file (because it is faster) and as a snippet. The javascript that goes with it only needs 1 variable changed depending on the method.

btw (reminding myself to fix the user lookup) because I hard coded it to say my name.

Edited by hechacker1, 08 January 2006 - 10:31 AM.


#18 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 08 January 2006 - 03:34 PM

Answers:

1.) & 2.) You could pull the required functions out of the API and use them as local functions within your srcipt as long as you make any needed adjustments to variable names and also include any supporting functions...

3.) Use the PHP strip_tags() function...

#19 hechacker1

    Etomite Forum Fan

  • Member
  • Pip
  • 59 posts

Posted 08 January 2006 - 08:52 PM

Ralph (rad14701), on Jan 8 2006, 07:34 AM, said:

Answers:

1.) & 2.) You could pull the required functions out of the API and use them as local functions within your srcipt as long as you make any needed adjustments to variable names and also include any supporting functions...

3.) Use the PHP strip_tags() function...

thank you Ralph! I knew there was an easy solution.. i was just too tired to look it up at 3 am. :blush:

I already implemented the strip_tags function and it is working beautifully. But now I need to make it configurable so that the user can decide what tags to keep; which will probably only be the <img> and <p> tag.

i will continue development tonight, after I move back to UCSD to start a new quarter of classes.

#20 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 08 January 2006 - 09:15 PM

hechacker1, on Jan 8 2006, 04:52 PM, said:

thank you Ralph! I knew there was an easy solution.. i was just too tired to look it up at 3 am.  :blush:

I already implemented the strip_tags function and it is working beautifully. But now I need to make it configurable so that the user can decide what tags to keep; which will probably only be the <img> and <p> tag.

i will continue development tonight, after I move back to UCSD to start a new quarter of classes.
I seem to recall using a snippet call parameter tag, ?striptags=true|false, in one of my snippets which then triggered a conditional clause within the code...

Just removing <img> and <p> tags you would require a regular expression in order to be implemented properly... B)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users