joer80 Posted June 19, 2014 Share Posted June 19, 2014 I have a states drop down that I want to populate and when I find all of my locations, I return duplicates. ie. Texas, Texas, Florida, Florida, etc. This is my code: $Locations = $pages->find('template=Location'); //loop through our locations foreach($Locations as $Location) { if($Location->Location_State){ $StatesHTML .= '<option>' . $Location->Location_State . '</option>'; } } What is the best way to do this the PW way? I thought I saw a way to populate an array on the forums, but then wouldnt I need to do 2 loops? One to load the array, and one to loop through it, instead of just pulling distinct? Thanks! Link to comment Share on other sites More sharing options...
Macrura Posted June 19, 2014 Share Posted June 19, 2014 you might need to make an array for your options and then use array_unique on the options list..let me know if you need code example; running the 2 loops should work fine, i think that is the processwire way, because you are pulling a value from a page so there is no way of knowing which are duplicates until after you put them all in that array http://www.php.net//manual/en/function.array-unique.php Link to comment Share on other sites More sharing options...
joer80 Posted June 19, 2014 Author Share Posted June 19, 2014 Ah.. Well its good to know I am not missing something. Just did not know if there was a way to do it and I was missing it. I think in this case, this might be the faster and cleaner way: $result = wire('db')->query("SELECT * FROM field_location_state Group by data"); //Distinct while($row = $result->fetch_row()){ $StatesHTML .= '<option>' . $row[1] . '</option>'; } Link to comment Share on other sites More sharing options...
adrian Posted June 20, 2014 Share Posted June 20, 2014 What about $Locations->unique(); http://cheatsheet.processwire.com/?filter=unique 2 Link to comment Share on other sites More sharing options...
videokid Posted June 20, 2014 Share Posted June 20, 2014 yes, what about....? could you elaborate on this a bit more... looks I'm gonna learn something new.... Link to comment Share on other sites More sharing options...
adrian Posted June 20, 2014 Share Posted June 20, 2014 Not much more to say really From the docs: $a->unique() Return a new WireArray that is the same as $a but with no duplicate values. So you can get an array of pages using any PW selector and then remove any duplicates using unique() 1 Link to comment Share on other sites More sharing options...
joer80 Posted June 20, 2014 Author Share Posted June 20, 2014 If the page ids are different, and the State names are the same, how do you tell it to de duplicate based on the state name and ignore the page id information? Link to comment Share on other sites More sharing options...
videokid Posted June 20, 2014 Share Posted June 20, 2014 Not much more to say really From the docs: $a->unique() Return a new WireArray that is the same as $a but with no duplicate values. So you can get an array of pages using any PW selector and then remove any duplicates using unique() Aha OK... makes sense, I guess I didn't think it thru, but thanks for the reply! Link to comment Share on other sites More sharing options...
adrian Posted June 20, 2014 Share Posted June 20, 2014 Good point - sorry I didn't really think through your scenario completely I don't think there is a way for unique() to work for you in this case. I am curious how the states are referenced to the locations. Do you have a page field of states that is selected from when defining locations, or are they entered into a text field? If the former, then you already have a list of distinct states to pull from. I feel like this might be a page/template structure question, rather than a need to strip duplicates afterwards, but I think we need more info on how locations are defined to be sure. Once hack you can do is populate a simple array of state names ($state_names) as you iterate through your locations loop and then do: if(!in_array('nevada', $state_names)){ $StatesHTML .= '<option>' . $Location->Location_State . '</option>'; } Link to comment Share on other sites More sharing options...
joer80 Posted June 20, 2014 Author Share Posted June 20, 2014 My structure is Home Locations Locations -> Statename Locations -> Statename -> City Name -> Location Name Locations -> Statename -> City Name -> Location Name -> Inventory Locations -> Statename -> City Name -> Location Name -> Inventory -> Inventory Listing Then on my inventory page, I want a drop down that shows all states that my website covers. (Inventory can only be in one location and state at a time.) The Inventory Listing does not have a field for state, but the Location template does. I guess I do not even need a state name on the location template because that is my tree! I put it in there though because I only have 30 locations, and some will not need a city parent because we only have one location in that state. In that case I have the Location under the state instead of the city. (That way the navigation that is built from the tree does not have a near empty page.) If I do it the group by method and add a new Location with a new state, it is going to automatically add it to the drop down as a possible way to filter the results. Hope that helps! Link to comment Share on other sites More sharing options...
adrian Posted June 20, 2014 Share Posted June 20, 2014 Well how about a separate query like: $locations = $pages->get("/locations/"); foreach($locations->children() as $state) $StatesHTML .= '<option value=".$state->name.'>' . $state->title . '</option>'; } Maybe this is still getting duplicates, but I would think from your page structure, there should only be one Statename for each state, with multiple Location Names children below each Statename 1 Link to comment Share on other sites More sharing options...
joer80 Posted June 20, 2014 Author Share Posted June 20, 2014 I apologize! I had some incorrect information in my post #10 and fixed it. Can you tell me if this solution still applies? Link to comment Share on other sites More sharing options...
joer80 Posted June 20, 2014 Author Share Posted June 20, 2014 I see what you did! Ha. You are getting the children of the top level locations page, which is always a state, instead of reading the state field on the locations template! That would work.... It looks like I was making this harder than it had to be! There was totally a process wire way to do this. I am going to try and see if I can get by with out that statename field on the location as well. I don't think the optional city name will matter. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now