• home
  • forum
  • my
  • kt
  • download
  • Search with PHP

    Author: 2007-06-14 11:05:43 From:

    When this tutorial is finished, you'll hopefully have more of an idea about how to combine PHP and Flash into one seamless package and create search engine of sorts that will look through a text file and return the results of that search to the Flash movie.

    Now, if you don't know PHP, I've included the source for the small script that does the actual searching with the tutorial, so tht shouldn't be a problem. One thing that you will need before you start this tutorial is a working machine that has PHP installed and working with the web server. Without this, the .php file will be useless, as will much of this tutorial. So, without further ado, let's get started.

    As you can see above, the format is pretty simple. There's a box for a search term (yes, right now it only accepts one term), a results box that tells you how many results that there were that matched your results, and the "Results" box that shows the actual results. The arrow on the side allows you to scroll through the results if there are more than five (it only shows five at a time in the box). When you put a term in and hit the Submit button, the Flash movie sends a message to the PHP script with the term. The PHP script then does the work of looking through the file that it's told to, and returns the result to the Flash movie.

    First off, lets talk about the PHP script. Now, for those of you not into the whole programming thing, this might be a little difficult to understand, but I know that there are loads more that know what they re doing, so I'll try to be brief about it. First thought, the script:

    $foo=file("stuff.txt");
    $number=count($foo);
    $a=0;
    $count=0;
    $nothing="";
    for($i=0;$i<=$number;$i++){
     $blah=strstr($foo[$i],$input);
     if($blah){
     $next=str_replace(" ","+",$foo[$i]);
     $next=chop($next);
     $nothing.="blah".$a."=".$next."&";
     $a=$a+1;
     $count=$count+1;
     }
    }
    printf($nothing."count=".$count);
    
    Okay, now let me explain some of the things that it does here. First off, it opens the file "stuff.txt" so that it can read and write from it. Then, the for{} loop goes through and finds the lines that have the search term in them and records those in an array. The script finishes up by printing out the info to the Flash movie in a format that it can read and use. Now, that wasn't so bad, was it? Now, once you have this page all up and working, the real fun begins. On to the Flash movie! 
    Now, to start with, you can make this movie all on one layer of you really want to, but you might want to make it on more than one just so that you can easily edit things. So, first off, we need to make the text boxes so that we can output the information from the search and get the search term and all. You'll need to make these three: 
    • Input - for the input of a search term
    • Number - so that you can see how many results there are
    • Newoutput - for the output of the text from the search results
    Then from there, you can make the submit button look however you want, mine's just a nice generic one. Then, put the text in place to tell you what you're looking at, and you're almost there. Next comes the real fun - the ActionScript. 
    Now, behind the search button, we need to make a script that has this in it: 
    on (release, keyPress "") {
     if(count){
     count=count;
     }else{
     count=0;
     }
     c = 5;
     text.newoutput = "";
     loadVariablesNum ("http://bender.phpdeveloper.org/flash/flash.php?input="+input, 0, "POST");
     if(count){
     for (a=0; a<5; a++) {
     this = "blah"+a;
     that = eval(this);
     newa=a+1;
     text.newoutput = text.newoutput+newa+" - "+that+"\n\n";
     }
     number = count;
     }else{
     gotoAndPlay (4);
     }
    }
    
    This script will take that search term you put in and send it off to the PHP script waiting on the server. In this case, the address is "http://bender.phpdeveloper.org/flash/flash.php", and the extra part (="+input) will append the search term to the end of it and pass it to the script. The for{} loop on this side gets the info back from the script (all of the "blah" variables) and puts them in the window, then puts the number that it got back in the "Results Found:" window. 

    The next script belongs behind the arrow button that goes on the side and lets you advance through the selections. Here's the script for that:


    on (release) {
     text.newoutput="";
     b=c+5;
     for (a=c; a<b; a<count; a++) {
     this = "blah"+a;
     that = eval(this);
     find=that.indexOf(input);
     newa=a+1;
     text.newoutput = text.newoutput+newa+" - "+that+"\n\n";
     c=c+1;
     }
    }
    

    This script basically does the same thing as the one before it with one major difference - it only advances through the choices already in the script, and doesn't re-fetch the next results. It just reads the variables already chached in the movie. This saves time in the long run and makes it easier on your internet connection.

    The last piece of this puzzle is the actual file that you search. In this case, the file specified in the PHP script is stuff.txt You can change it to whatever you want, but be sure that it's divided up into lines (by newlines) or else you're going to get one really long rearch result. Most documents will have the newlines already there, you just have to copy it over. But on occasion, you'll need to go in and separate it out into lines.

    Well, that's pretty much it! If you've gotten all of this stuff put together, then you shouldn't have any trouble gettting this working together and be happily searching text files in no time! Best of luck!

    » Level Intermediate

    Added: : 2001-01-09
    Rating: 5.99 Votes: 38
    Hits: 1988
    » Author
    No author details available.
    » Download
    Download the files used in this tutorial.
    Download (34 kb)

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot