Thread: Search Field

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24

    Search Field


    If a search field can be made using Python, can you help me out with making one?

    I want this one page where you can look at book reviews. I want the search field to allow you to search by title, author, or genre. If your search didn't have a match, I want it to display

    Sorry, <the text that was in the search field> could not be found. Then have the search field below so that the user can search for something else.

    If there is a match, I want to display all the results.



    Example:
    |Title| Learning Python

    Results:

    Learning Python <= (URL to the book reviews page for this book)
    by Mark Lutz and David Ascher
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Well i can tell you that its definatly possiable , i'm guessin were talking about a web based search engine, but you should check out this great Python powered search engine..

    Gonna need some more info though chief.. what form are you storing this data in i.e. MySQL, pickle, flat file etc. What other features are you wanting.. as much info as you can, would really help

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    35
    Rep Power
    11
    What are you searching against? A database, textfile, XML packet, or using a search from another site, such as Amazon.com or BN.com.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    I want to store this data in what ever is most simple and efficient. I have MySQL, but I don't really want to use it. But if we have to, I will.

    Well here's how I want it to look:

    <drop down box>
    (Options: title, author, and genre)

    Then have a search field right next to the drop down box.

    Then have a submit button right next to the search field.

    If you search for "Learning Python", it will return

    Learning Python (URL to the reviews for this book)
    by Mark Lutz and David Ascher

    If you search for "Mark Lutz" or "David Ascher", it will return all books written by him. Such as:

    Learning Python (URL to the reviews for this book)
    by Mark Lutz and David Ascher

    If you search for "Python", which is the genre this book will be in, it will return all books in the Python genre. Giving the title first which is also the URL to the reviews for that title. Below that will be the author(s).

    I want it to be in alphabetical/numerical order.

    Was that descriptive enough?

    ============================

    EDIT:

    oxygenthief: What ever would work.

    The book reviews are made by people who visit my web site and the book reviews are at my web site.
    Last edited by MasterChief; November 30th, 2003 at 03:42 PM.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Well, obviously this would ber quite easy in SQL, if you dont wana use MySQL then SQLite would be a great choice (small and fast). If not then we're gonna have to this about how were gonna put this data in a flat file or dictionary/list/tuple...

    http://pysqlite.sourceforge.net/

    You should also know that using a flat file, or pickle will NOT be as fast as using an SQL database but not overly slow , it does make adding to the book list a little harder..

    The layout bit will be easy since its just html so i'm gonna leave that up to you! But you might also want to have a 'show all' in that drop down of yours

    So which way do you want to take this?

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    I choose MySQL. I'm just scared of it. Bad experiences. But if you say that it will be easy, I trust you.

    Yeah, the layout will be really simple.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok so first we need to setup our Database , you should read thought his tutorial..

    http://www.devshed.com/Server_Side/P...SQL/page1.html

    Just of the top of my head but i'd think your gonna need a table with each of these cols.. id, title, author, genere, link

    id being the Primary key

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    I got MySQLdb. I installed it and was able to import it with no problems. I have not used it yet.

    I finished making the layout. Here is the code for that:

    Code:
    <form action="">
    <p>
    <select>
    <option value="title" selected="selected">title</option>
    <option value="author">author</option>
    <option value="genre">genre</option>
    </select>
    <input type="text" name="search" size="100" />
    <input type="submit" value="Submit" />
    </p>
    </form>
    (I use XHTML 1.0 Strict)

    I finished setting up the database. Here is what I did:

    mysql> CREATE DATABASE books;
    Query OK, 1 row affected (0.13 sec)

    mysql> USE books;
    Database changed

    mysql> CREATE TABLE books
    (id varchar(255),
    title varchar(255),
    author varchar(255),
    genre varchar(255),
    link varchar(255));

    Query OK, 0 rows affected (0.19 sec)

    What do we do now?
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok i'm with ya , one thing i have to point out is here - your id should be auto incremented and your primary key..

    Just setting up my copy of your database now and i'll give you a sample script that returns the results from the database

    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    How should the "books" table look?

    mysql> CREATE TABLE books
    (id ?????????????????,
    primary key ?????????,
    title varchar(255),
    author varchar(255),
    genre varchar(255),
    link varchar(255));
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I set the table up as...

    CREATE TABLE books (
    id INT(4) NOT NULL AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    author VARCHAR(255) NOT NULL,
    genre VARCHAR(255) NOT NULL,
    link VARCHAR(255),
    PRIMARY KEY(id)
    );

    Mark.
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    All right. I dropped the table I had and create the exact same table you have.
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, i have attached what i have so far (not finished and will need a little tweeking to do what you wanted..)

    Note: i havn't had the time to test this; mainly because i've been very busy with other things but not having any sample data to search against doesnt help much .

    Also, the SQL query i'm using is also quite strict, so the the string must be matched exactly within a field for it to be returned.. this still doesnt doesn't have any error capture or layout, all things you should be able to add without much trouble!

    Mark.

    Edit: if the search is too strict for you we can try MySQLs REGEXP
    Attached Files
    Last edited by netytan; December 1st, 2003 at 07:28 PM.
    programming language development: www.netytan.com Hula

  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    Thanks a lot! Greatly appreciated!

    You made a typo. I corrected it but I wanted to let you know so that you can correct yours if you want.

    It was: MySQLdb.conenct

    It should be: MySQLdb.connect

    So how do I use this Python script on my web site? Where do I put it?
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    well, this unfinished version has the functionality of a fish outa water so i wouldn't suggest you use it for now , however when its done you should put it in your current directory (if you can run CGI there) and make a secont xhtml page with the form on it and set its action to books.py.

    Thanks lol, typing too fast again curesed i tell you!

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo