SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Search functionality

    Hey All,

    I'm trying to create a simple app that will allow me to search, and return results based on the parameters enter in the search form. I'd imagine this is fairly simple to accomplish, but i'm not sure where to start. any direction would be much appreciated.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Probably the simplest way to accomplish this is with using Ruby as a CGI script. You should probably have a look at the CGI class documentation as a starting place. If you have a different setup for using Ruby on your web server right now, let us know.

    Hopefully that gets you rolling
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are you trying to search? A model you have a table for in your database? your filesystem? pdf files?

    If its a model reflected by a table in your db, its the easiest to do.. (and really the only one I'm experienced with)
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  4. #4
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I guess i should be more clear. I'm building a little web app that holds local housing listings. I've created a real basic admin to add listings, but i'm trying to build a search on the the site, which allows users to search by city. i have a listings table which holds the listing city, state, bedrooms, baths, etc. i have an admin_controller to allow for adding listings, and i have a listing_controller to list the inventory to the general public.

    The road block i've run into is being able to build the search functionality.

    I'm thinking i need to create an method in the listing.rb that will search listings by city, but the part that confuses me still is how to take parameters from a form, and passing them to the method that is going to do the search.

    any help would be much appreciated.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code ruby:
      #listing_controller,rb
      def search
        if request.post?
          search_city = params[:search][:city]
          @listings = Listing.find(:all, :conditions => ["city = ?", search_city])
        end
      end
     
      # view
      <form action="/listing/search" method="post">
        Search City: <%= text_field :search, :city %>
        <%= submit_tag "Search!" %>
      </form>
     
      <% unless @listings.nil? or @listings.empty? %>
        <% listings.each do |listing|
           <!-- display your listings however you want... -->
         <% end %>
      <% end %>

    Obviously, thats a simple example where they would have to type exactly what matches the city in the table row, but that should get you started.

    You can make it more wild carded if you use
    Code ruby:
      @listings = Listing.find(:all, :conditions => ["city LIKE ?", '%' + search_city + '%'])
    Let me know if you've got any other questions.
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  6. #6
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome! I'm going to give this a try. Thanks for the help!

  7. #7
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey pjleonhardt,

    Thanks again for all your help. I'm getting one little error that i'm hoping you can help me out with. I've seen this on some other stuff i've tried building, but still haven't been able to figure out what the problem is.

    I'm getting this error when i try to do a search using your code...

    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.-

    i added the action to the listing_controller.rb then i added the form field. i then c reated a new search.rhtml file which i'm hoping to display all the results.

    I'm assuming the error is a result of @listing being empty, but i could be wrong.

    any ideas?

    Thanks

  8. #8
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What line does it say the error is on? And please post the 2 lines above this line, that line and the 2 lines below it.
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  9. #9
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've been meaning to reply.... looks like i got it working, but thanks so much for your help.


Bookmarks

Posting Permissions

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