SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    iterating through nested result set twice

    Sadly, my ruby ignorance has gotten the best of me. I'm trying to turn a locater function into a marketing tool. I generate a list of zip codes (with their distances from the given zip). I pass that list via find_by_all to my sites model and grab all of the sites in that list.

    Here's what I want to accomplish:
    • display all sites within 20 to 30 miles - done
    • display preferred sites that may be outside that radius - done
    • count the number of results - I think done
    • if count is less than 2, then display sites from wider radius until I have at least 2 - I'm losing it here


    Here's my code ... it works up to the last part:
    Code:
    <% if @results %>
       Here are the sites we found:
          <table>
          	<tr>
          		<th>Zip</th>
          		<th>Distance</th>
          		<th>Site</th>
          	</tr>
          	<% @sitecount = 1%> # initialize my counter
    
          	#parse through each zipcode I pulled
            <% @results.each do |zip| %> 
    
              #for each zipcode, iterate through my list of sites to find a site in that zip
              <% @sites.each do |site| %> 
                <% if site.zip == zip.zip && (zip.distance_to_search_zip('miles') <= 30 || zip.distance_to_search_zip('miles') <= site.referral_radius) %>
                  <% @dealercount += 1 %>
                  <tr>
                    <td><%= zip.zip %></td> 
                    <td><%= sprintf("%.2f", zip.distance_to_search_zip('miles')) %> </td> 
                    <td><%= site.name %></td>
                  </tr>
                <% end %>
                
              <% end %>
              
            <% end %>
            
           #this is what I can't get to work
            <% @results.each do |zip| unless @sitecount >= 2 %>
              <% @sites.each do |site| %>
                <% if site.zip == zip.zip && zip.distance_to_search_zip('miles') > 30 %>
                  <% @sitecount += 1 %>
                  <tr>
                    <td><%= zip.zip %></td> 
                    <td><%= sprintf("%.2f", zip.distance_to_search_zip('miles')) %> </td> 
                    <td><%= site.name %></td>
                  </tr>
                <% end %>
              <% end %>
            <% end %>           
          </table>
    <% end %>
    I thought that after I went through my initial parsing of the zips, I could check the number of results and parse through it a second time, grabbing sites from a wider radius. My logic here is obvioulsy flawed.

    Any suggestions on how I should approach this? (other than moving it to the model/controller/wherever ... I'm trying to get the logic down first ... then I'll recode it where it should be).

    Thanks!!!

    zaed

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.

    Code:
          <&#37; while @sitecount <= 2 %>
            <% @results.each do |zip| %>
              <% @sites.each do |site| %>
                <% if site.zip == zip.zip && zip.distance_to_search_zip('miles') > 30 %>
                  <% @sitecount += 1 %>
                  <tr>
                    <td><%= zip.zip %></td> 
                    <td><%= sprintf("%.2f", zip.distance_to_search_zip('miles')) %></td> 
                    <td><%= site.name %></td>
                  </tr>
                <% end %>
              <% end %>
            <% end %> 
          <% end %>
    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.

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks pj. Moving that logic to a while loop prevents the thing from bombing completely. Now I just have to figure out why it isn't giving me the results I want.

    I'm thinking about refactoring it by putting the initial 'results.each do' in a loop with its own counter. The first time through, it would run as is, counting my sites as well. Then if it runs through the results once to completion AND the sitecount is less than I want, run through the 'results.each do' a second time, this time passing in an argument to replace the second halp of my 'if' statement that widens out my radius.

    Yell if you have any ideas. Thanks for your help.

    zaed

    ps - nice signature ... I never thought of frustration as a way of life ...


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
  •