Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Words search engine

    Hello forum,

    I'm not skilled to write javascript, I can only read/edit it.
    I'm working in a movie rental store, and my boss asked me to build an online app where the user can enter a specific title, and then the app will confirm if we got the title in store or not.

    I got a xml document with 40.000 titles.

    Does anyone know a downloadable script wich makes this posible?

    Thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You will need server-side coding with a database for this. Don't forget that when a purchase/rental is made the stock level must be adjusted accordingly.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello Phillip M,
    Thank you for your reply!
    I was thinking about a js document where the titles are entered, then when the user search a title, it will search the js file if the title is in there. And that the script will simply answer with: 'Yes we got the movie!' or 'No, we don't have the movie'.
    Its not necessary that the user sees if the movie is rented or not.

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Don't duplicate your *cough* "database" *cough*. It doubles the effort to keep it up to date. In fact, I would advise you to rethink the data design – such big XML documents are very unhandy. Why don't you use an actual database? It would make it much easier to query it (and to maintain it).

    For example, you could just send an Ajax request to retrieve the document in Javascript. But with such a big file that's a no-go. It's also a really bad idea to load a file with 40,000 entries (whether it's XML or a "js file") every single time a user lands on your page. It increases both loading time and server traffic.
    Last edited by Airblader; 12-01-2013 at 02:37 PM.

  • #5
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello Airblader,
    The xml document is 29mb, but i filtered it in dreamweaver, and removed all the unused text like; description, vhs, release date ect ect, and now the file is 500kb. Every week we get like 7-10 new movie titles, wich isnt time consuming to add to the js file. So i would rather stick to a 500kb js file.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    If you really want to serve every user 0.5 MB data of which he won't need 99%, then I would still recommend to not duplicate the database. Just make an Ajax call and retrieve the XML "master" document – you can read and handle it in Javascript just as well.

    Though you could think about some improvements without moving away from that datastructure: Version the file, create an easy way to query the current version on the server and store the database locally in the user's browser (i.e. localStorage) and only retrieve the file again if the version has changed). Just as an additional idea.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I think you have a problem if the user (customer) cannot spell properly. E.g. asks for "Kramar vs Kramar".

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    To be fair, that problem would exist even when using a real database*. There are easy ways to overcome this – but you can make it as complicated as you want it to (afterall that is part of Google's success). So far I don't get the feeling that he wants a very feature-heavy application, but rather a simple search without any kind of "intelligence".

    *) Unless MySQL & Co come with built-in feature for that these days(?)

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    you can open the file in notepad and press [ctrl]+[F] to see if you have the title...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #10
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by rnd me View Post
    you can open the file in notepad and press [ctrl]+[F] to see if you have the title...


    Imagine this as the actual backend implementation: a virtual machine that will do exactly that. It would be amazing. As dumb as it can get, but amazing.

    But make sure to search for "'>' + title + '</'", otherwise "Taxi" will give a false-positive if you have "Taxi, Taxi" instead.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    for only 40,000 single-string items, a client-side search will execute much faster than a server-side search, especially if any kind of a database is involved.

    you can use ajax to ship the xml to the client, but i would first transform the xml into JSON and gzip the result. that should turn 500kb of xml into 20-160kb of json, depending on tags and name repetition. a couple hundred kb is not very much anymore. the huffington post push 1.5MB of data to the client on the first visit to thier homepage...


    just so we can appreciate the user experience difference, here's a quick example of searching an xml chuck client-side.

    we can instantly filter the list in 1-7 ms, whereas a server round-trip will take dozens of times longer even if the search is cached.

    loading half of a megabyte of data (or less) in order to use such a search might take a second or two longer to "boot up" than a "thin client" would. but, there's a reason that personal computers became more popular than dummy network terminals: it's about the poor user and his experience. it's a MUCH different experience to have the info in your palm than on a remote server. a rich client app has no worries about lag, congestion, etc; you have the searching machine in the user's hands.

    If we have to wait half a second for each search result, that extra two seconds is paid-off after four searches. but, since the info is at-hand we can use filtering and instant feeback to save valuable human keystrokes during the search. if a system is slow, it's a crummy experience to search every keystroke, so most developers make the user wait until done typing or worst, make them type a bunch and then click a button.

    with an instant client search, you can have found what you need to know before a server-based system's user can even click "submit".


    feel free to kick the tires in a live demo



    Code:
    <html>
    
    <label>Load Movie File (or use demo)
    	<input id=file type=file accept='*.xml,application/xml' />
    </label>
    
    <br />
    
    <label>Search movie inventory
    	<input oninput="this.style.backgroundColor=hasMovie(this.value)?'#fbb':'#bfb';" />
    </label>
    
    <hr />
    
    <ol id=list></ol>
    
    <script>
    
    MOVIE_TAG_NAME = "film"; // customize this to fit the xml data:
    
    movies=self.movies || [];
    
    
    function hasMovie(title){
      return movies
        .filter( /./.test, new RegExp(title, "i") )
        .slice(0, 50)
        .map(render).length > 0;
    }
    
    
    function render(item, index){
      if(!index){  list.innerHTML=""; }
      list.appendChild(document.createElement("li")).innerHTML=item;
    }
    
    
    function loadMovies(strXML){
      var shell=document.createElement("movies"); //make a dom wrapper
      shell.innerHTML=strXML; //parse tags as DOM structure
      movies=[].slice.call(shell.getElementsByTagName(MOVIE_TAG_NAME))
         .map(function(node){  return node.textContent }); // extract text from tag
      hasMovie(""); // show first 50 movies
    }
    
    
    //loads a movie XML once a file is choosen
    file.onchange=function(e){
      fr=new FileReader();
      fr.onload=loadMovies;
      fr.readAsText(file.files[0]);
    };
    
    
    
    
    
    
    //load some demo movie data
    loadMovies("<movies>\
    	<film>Citizen Kane (1941)</film>\
    	<film>Dr. Strangelove Or How I Learned to Stop Worrying and Love the Bomb (1964)</film>\
    	<film>North by Northwest (1959)</film>\
    	<film>Rear Window (1954)</film>\
    	<film>The Third Man (1949)</film>\
    	<film>Laura (1944)</film>\
    	<film>Repulsion (1965)</film>\
    	<film>Seven Samurai (Shichinin no Samurai) (1954)</film>\
    	<film>All About Eve (1950)</film>\
    	<film>On the Waterfront (1954)</film>\
    	<film>Modern Times (1936)</film>\
    	<film>The 400 Blows (Les Quatre cents coups) (1959)</film>\
    	<film>Rashômon (Rashomon) (In the Woods) (1951)</film>\
    	<film>Singin' in the Rain (1952)</film>\
    	<film>The Last Picture Show (1971)</film>\
    	<film>The Philadelphia Story (1940)</film>\
    	<film>12 Angry Men (Twelve Angry Men) (1957)</film>\
    	<film>Cool Hand Luke (1967)</film>\
    	<film>Battleship Potemkin (1925)</film>\
    	<film>The Adventures of Robin Hood (1938)</film>\
    	<film>Rebecca (1940)</film>\
    	<film>Anatomy of a Murder (1959)</film>\
    	<film>Frankenstein (1931)</film>\
    	<film>The Gold Rush (1925)</film>\
    	<film>The Grapes of Wrath (1940)</film>\
    	<film>The Treasure of the Sierra Madre (1948)</film>\
    	<film>The Bride of Frankenstein (1935)</film>\
    	<film>The Searchers (1956)</film>\
    	<film>The Leopard (1963)</film>\
    	<film>Rio Bravo (1959)</film>\
    	<film>The Birth of a Nation (1915)</film>\
    	<film>Das Cabinet des Dr. Caligari. (The Cabinet of Dr. Caligari) (1920)</film>\
    	<film>Kind Hearts and Coronets (1949)</film>\
    	<film>The African Queen (1952)</film>\
    	<film>Top Hat (1935)</film>\
    	<film>Stagecoach (1939)</film>\
    	<film>The Invisible Man (1933)</film>\
    	<film>Purple Noon (Plein soleil) (1960)</film>\
    	<film>Quai des Orfevres (Quay of the Goldsmiths) (Jenny Lamour) (1947)</film>\
    	<film>Shadow of a Doubt (1943)</film>\
    	<film>The Killers (1946)</film>\
    	<film>The Odd Couple (1968)</film>\
    	<film>Meet Me In St. Louis (1944)</film>\
    	<film>Scarface (1932)</film>\
    	<film>Sullivan's Travels (1941)</film>\
    	<film>The Lost Weekend (1945)</film>\
    	<film>Un condamné à mort s'est échappé ou Le vent souffle où il veut (A Man Escaped) (1956)</film>\
    	<film>Sleeper (1973)</film>\
    	<film>L'Atalante (1934)</film>\
    	<film>Marty (1955)</film>\
    	<film>My Man Godfrey (1936)</film>\
    	<film>The Lady Eve (1941)</film>\
    	<film>White Heat (1949)</film>\
    	<film>Suspicion (1941)</film>\
    	<film>The Lavender Hill Mob (1951)</film>\
    	<film>Adam's Rib (1949)</film>\
    	<film>Black Narcissus (1947)</film>\
    	<film>The Big Heat (1953)</film>\
    	<film>The Fallen Idol (1948)</film>\
    	<film>The Public Enemy (1931)</film>\
    	<film>Great Expectations (1946)</film>\
    	<film>The Shop Around the Corner (1940)</film>\
    	<film>Them! (1954)</film>\
    	<film>Witness for the Prosecution (1957)</film>\
    	<film>Alfie (1966)</film>\
    	<film>Captain Blood (1935)</film>\
    	<film>Day of Wrath (Vredens Dag)(Day of Anger) (1943)</film>\
    	<film>Henry V (The Chronicle History of King Henry the Fift with His Battell Fought at Agincourt in France (1944)</film>\
    	<film>Kes (1969)</film>\
    	<film>Odd Man Out (1947)</film>\
    	<film>A Room With A View (1986)</film>\
    	<film>My Darling Clementine (1946)</film>\
    	<film>National Velvet (2000)</film>\
    	<film>The Ipcress File (1965)</film>\
    	<film>Holiday (1938)</film>\
    	<film>The Hidden Fortress (kakushi-toride No San-akunin) (1958)</film>\
    	<film>Holiday Inn (1942)</film>\
    	<film>How the Grinch Stole Christmas (1965)</film>\
    	<film>The Bank Dick (1940)</film>\
    	<film>The Palm Beach Story (1942)</film>\
    	<film>To Have and Have Not (1944)</film>\
    	<film>Angels with Dirty Faces (1938)</film>\
    	<film>Cat Ballou (1965)</film>\
    	<film>I Am a Fugitive From a Chain Gang (1932)</film>\
    	<film>Murder on the Orient Express (1974)</film>\
    	<film>Swing Time (1936)</film>\
    	<film>The Ladykillers (1955)</film>\
    	<film>Young Mr. Lincoln (1939)</film>\
    	<film>Metropolis (1927)</film>\
    	<film>The Wizard of Oz (1939)</film>\
    	<film>A Hard Day's Night (1964)</film>\
    	<film>Lawrence of Arabia (1962)</film>\
    	<film>Rosemary's Baby (1968)</film>\
    	<film>Chinatown (1974)</film>\
    	<film>Vertigo (1958)</film>\
    	<film>Sunset Boulevard (1950)</film>\
    	<film>King Kong (1933)</film>\
    	<film>A Streetcar Named Desire (1951)</film>\
    	<film>The Manchurian Candidate (1962)</film>\
    	<film>Mean Streets (1973)</film>\
    </movies>");
    
    
    </script>
    </html>
    Last edited by rnd me; 12-01-2013 at 08:03 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #12
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    If you load the XML document upon pageload, the user certainly won't notice the time it takes because by the time he starts typing a movie (hell, before he even sees the textbox) the file will be loaded.
    But if you really do fear it takes too long, you could at least create the "inlined" version of the document by having PHP read the master document. I just think it's a terrible design to duplicate the data.

    I also suggested caching the file locally so that you don't have to query the server (but just the local cache, which can, again, be done upon pageload).

  • #13
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    just for fun, here is a version i made using a notepad find-and-replace on the xml data to turn the movie title tags into OPTION tags.

    this version uses just html, no javascript, to suggest movies and turn green if a known movie is entered:

    Code:
    <html>
    <form>
    
    <style>:invalid{background-color:#fbb;} :valid{background-color:#bfb;} </style>
    
    <label> Search for a movie title <input list=movies size=99  pattern='.*\)' required id=term type=search></label>
    
    <datalist id=movies>
    <option>Citizen Kane (1941)</option>	<option>Dr. Strangelove Or How I Learned to Stop Worrying and Love the Bomb (1964)</option>	<option>North by Northwest (1959)</option>	<option>Rear Window (1954)</option>	<option>The Third Man (1949)</option>	<option>Laura (1944)</option>	<option>Repulsion (1965)</option>	<option>Seven Samurai (Shichinin no Samurai) (1954)</option>	<option>All About Eve (1950)</option>	<option>On the Waterfront (1954)</option>	<option>Modern Times (1936)</option>	<option>The 400 Blows (Les Quatre cents coups) (1959)</option>	<option>Rashômon (Rashomon) (In the Woods) (1951)</option>	<option>Singin' in the Rain (1952)</option>	<option>The Last Picture Show (1971)</option>	<option>The Philadelphia Story (1940)</option>	<option>12 Angry Men (Twelve Angry Men) (1957)</option>	<option>Cool Hand Luke (1967)</option>	<option>Battleship Potemkin (1925)</option>	<option>The Adventures of Robin Hood (1938)</option>	<option>Rebecca (1940)</option>	<option>Anatomy of a Murder (1959)</option>	<option>Frankenstein (1931)</option>	<option>The Gold Rush (1925)</option>	<option>The Grapes of Wrath (1940)</option>	<option>The Treasure of the Sierra Madre (1948)</option>	<option>The Bride of Frankenstein (1935)</option>	<option>The Searchers (1956)</option>	<option>The Leopard (1963)</option>	<option>Rio Bravo (1959)</option>	<option>The Birth of a Nation (1915)</option>	<option>Das Cabinet des Dr. Caligari. (The Cabinet of Dr. Caligari) (1920)</option>	<option>Kind Hearts and Coronets (1949)</option>	<option>The African Queen (1952)</option>	<option>Top Hat (1935)</option>	<option>Stagecoach (1939)</option>	<option>The Invisible Man (1933)</option>	<option>Purple Noon (Plein soleil) (1960)</option>	<option>Quai des Orfevres (Quay of the Goldsmiths) (Jenny Lamour) (1947)</option>	<option>Shadow of a Doubt (1943)</option>	<option>The Killers (1946)</option>	<option>The Odd Couple (1968)</option>	<option>Meet Me In St. Louis (1944)</option>	<option>Scarface (1932)</option>	<option>Sullivan's Travels (1941)</option>	<option>The Lost Weekend (1945)</option>	<option>Un condamné à mort s'est échappé ou Le vent souffle où il veut (A Man Escaped) (1956)</option>	<option>Sleeper (1973)</option>	<option>L'Atalante (1934)</option>	<option>Marty (1955)</option>	<option>My Man Godfrey (1936)</option>	<option>The Lady Eve (1941)</option>	<option>White Heat (1949)</option>	<option>Suspicion (1941)</option>	<option>The Lavender Hill Mob (1951)</option>	<option>Adam's Rib (1949)</option>	<option>Black Narcissus (1947)</option>	<option>The Big Heat (1953)</option>	<option>The Fallen Idol (1948)</option>	<option>The Public Enemy (1931)</option>	<option>Great Expectations (1946)</option>	<option>The Shop Around the Corner (1940)</option>	<option>Them! (1954)</option>	<option>Witness for the Prosecution (1957)</option>	<option>Alfie (1966)</option>	<option>Captain Blood (1935)</option>	<option>Day of Wrath (Vredens Dag)(Day of Anger) (1943)</option>	<option>Henry V (The Chronicle History of King Henry the Fift with His Battell Fought at Agincourt in France (1944)</option>	<option>Kes (1969)</option>	<option>Odd Man Out (1947)</option>	<option>A Room With A View (1986)</option>	<option>My Darling Clementine (1946)</option>	<option>National Velvet (2000)</option>	<option>The Ipcress File (1965)</option>	<option>Holiday (1938)</option>	<option>The Hidden Fortress (kakushi-toride No San-akunin) (1958)</option>	<option>Holiday Inn (1942)</option>	<option>How the Grinch Stole Christmas (1965)</option>	<option>The Bank Dick (1940)</option>	<option>The Palm Beach Story (1942)</option>	<option>To Have and Have Not (1944)</option>	<option>Angels with Dirty Faces (1938)</option>	<option>Cat Ballou (1965)</option>	<option>I Am a Fugitive From a Chain Gang (1932)</option>	<option>Murder on the Orient Express (1974)</option>	<option>Swing Time (1936)</option>	<option>The Ladykillers (1955)</option>	<option>Young Mr. Lincoln (1939)</option>	<option>Metropolis (1927)</option>	<option>The Wizard of Oz (1939)</option>	<option>A Hard Day's Night (1964)</option>	<option>Lawrence of Arabia (1962)</option>	<option>Rosemary's Baby (1968)</option>	<option>Chinatown (1974)</option>	<option>Vertigo (1958)</option>	<option>Sunset Boulevard (1950)</option>	<option>King Kong (1933)</option>	<option>A Streetcar Named Desire (1951)</option>	<option>The Manchurian Candidate (1962)</option>	<option>Mean Streets (1973)</option>
    </datalist>
    </form>
    
    </html>
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Nobody past Philip's first response has bothered to address the problem of "stock level". Yeah, there might be 40,000 titles available. But after you sell the last copy of "Hunger Games", how can you sell another one? Same applies to rental. You allow the user to look up the title, but how will you or he know if the title is actually in stock at the store to rent.

    This whole thing, including doing it with XML, reads like a homework assignment, to me. I can't imagine basing a REAL store on something built in this manner.

    Airblader: Yes, MySQL can do Soundex searches. You will get more results than you want, but you will find most misspellings.
    http://dev.mysql.com/doc/refman/5.5/...nction_soundex
    The genealogy site RootsWeb.com uses MySQL and Soundex to find spelling variations in surnames with a lot of success.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Airblader (12-01-2013)

  • #15
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    @ Old Pedant

    The OP was very clear on this:

    Quote Originally Posted by handy44 View Post
    Its not necessary that the user sees if the movie is rented or not.
    Thanks for the link on soundex. Wasn't aware of that (I only get to work with that awful Oracle 10 crap…)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •