Vineet Sawant Posted June 22, 2013 Share Posted June 22, 2013 Hello all, I'm working on a project where the content of the website will change location wise. Content is basically the list of local theatres. If someone from New York visits the site, he/she can select city as NY and Broadway theatre will be shown first in the list. Also, since users have their accounts set up already, I want to make sure by default they are shown the content of their city, but they can change city anytime by using the select box fixed on top. I know this would be easier once I figure out how do I sort the content. What I've thought so far is, I can maintain a variable, something like a global variable called as $current_city. By default, it'll be like $current_city = $user->user_city. But when user selects other city, the $current_city variable should take the selected value. Thanks for your time. Link to comment Share on other sites More sharing options...
adrian Posted June 22, 2013 Share Posted June 22, 2013 Why not set $session->current_city = $user->user_city and then if they choose an alternative from the select box, change it using: $session->current_city = $input->get->selected_city or something like that, depending on how you process the select box. 2 Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 22, 2013 Author Share Posted June 22, 2013 Thanks Adrian, that sounds like a good solution. I'll need to learn processing forms Link to comment Share on other sites More sharing options...
adrian Posted June 22, 2013 Share Posted June 22, 2013 If you do it simply with a page refresh, rather than ajax, you can simply do something like the following at the top of the page: if($input->get->selected_city){ $session->current_city = $input->get->selected_city; } else{ $session->current_city = $user->user_city; } You can use the $session->current_city directly in your selector. You can choose get or post for the action for your form depending on whether you want the selected_city passed via the url (good for bookmarking) or not. 2 Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 22, 2013 Author Share Posted June 22, 2013 Alright, so I have a dumb question, selected_city is the select bid that holds the city list I suppose, right? On which page template do I add this custom select field? Link to comment Share on other sites More sharing options...
adrian Posted June 22, 2013 Share Posted June 22, 2013 I am not sure how you have things set up, but I would have a page select field that contains all the city names. I guess each theatre would have a city field assigned to it via this field. For generating your select dropdown you could do: echo '<select name="selected_city">'; foreach($pages->get("/cities/")->children() as $city){ echo '<option value="'.$city->name.'">'.$city->title.'</option> } echo '</select>'; Then for your selector for limiting theaters to the selected city you might have something like: $theaters = $pages->get("/theaters/")->children(city=$session->current_city); All written in the browser and untested, but should be close, but again it depends on how your theaters and cities fields/templates are already set up. 1 Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 22, 2013 Author Share Posted June 22, 2013 Ah, that's right, I can make a cities page and list cities under it as children. I never gave cities so much importance so I ended up just keeping a text field for Theatre's city. Will do as you suggested, the picture is quiet clear now, thanks a lot Adrian. Link to comment Share on other sites More sharing options...
adrian Posted June 22, 2013 Share Posted June 22, 2013 No problem. In these sort of cases, for the city field (the one in the theatre template), I like to check: "Allow new pages to be created from field?" at the bottom of the Input tab. This makes it easier for the person adding new theaters to the admin to add a new city if required. Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 22, 2013 Author Share Posted June 22, 2013 That's a nice suggestion but the cities will be only a few in the beginning as we'r allowing only a few selected theatres on this site. At most there will be only 7 cities. But still having add new button will be helpful in future. Thanks once again, I really appreciate your help, before posting the question I spent over 3hours trying to come up with a solution but couldn't do anything. I didn't even consider sessions. Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 22, 2013 Author Share Posted June 22, 2013 Then for your selector for limiting theaters to the selected city you might have something like: $theaters = $pages->get("/theaters/")->children(city=$session->current_city); All written in the browser and untested, but should be close, but again it depends on how your theaters and cities fields/templates are already set up. hey, I tried to implement above code but it gives me error: Fatal error: Exception: Unknown Selector operator: '=$' -- was your selector value properly escaped? Here's my code for theatres list page called as theatres. echo "<ul class='theatres_list row'>"; foreach($page->children('theatre_city=$session->current_city') as $child){ ... } echo "</ul>"; But if I manually add city name instead of $session->current_city, it works. What am I doing wrong? or is it like selectors do not take values from variables? Link to comment Share on other sites More sharing options...
adrian Posted June 22, 2013 Share Posted June 22, 2013 (edited) Sorry, my fault: foreach($page->children('theatre_city='.$session->current_city) as $child){ or foreach($page->children("theatre_city={$session->current_city}") as $child){ On another note, you might want to modify this line in your select creation to the following: echo '<option value="'.$city->name.'"' . $session->current_city == $city->name ? ' selected="selected"' : '' .'>'.$city->title.'</option> This will ensure that the select dropdown highlights the current selected city. Edited June 23, 2013 by adrian 1 Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 23, 2013 Author Share Posted June 23, 2013 Hello Adrian, I got the dropdown to select the currently selected city, as you had suggested. Following is the code I used: echo '<option value="'.$city->name.'"' .($session->current_city == $city->name ? 'selected': '' ).'>'.$city->title.'</option>'; There's just a little modification to the code you had given. Can you please check the following code for me and tell me if I'm doing it right way? $options = $fields->user_city->getInputfield($user)->getOptions(); $u_city = $options[$user->user_city]; $cities = $pages->get('name=cities')->children(); if($input->get->selected_city){ $session->current_city = $input->get->selected_city; } else{ if($user->isLoggedin()) { $session->current_city = $u_city; } }; echo "<form><select onchange='this.form.submit()' name='selected_city'><option> </option>"; foreach ($cities as $city) { echo '<option value="'.$city->name.'"' .($session->current_city == $city->title ? 'selected': '' ).'>'.$city->title.'</option>'; } echo "</select></form>"; Now when a user logs in, the select box correctly select's his/her default city. But it doesn't show the selected city if user changes it. I know for it to work, the $current_city variable must get value of the option tag not the name. How do I get the value? Sorry, I know I'm asking too much from you :\ Thanks for your help. Link to comment Share on other sites More sharing options...
adrian Posted June 23, 2013 Share Posted June 23, 2013 A few things: Is there some reason you can't simply use $user->user_city instead of the following. Remember that you can add custom fields to the user template. $options = $fields->user_city->getInputfield($user)->getOptions(); $u_city = $options[$user->user_city]; I always liked to use selected="selected" as this is XHTML compliant, but it seems that maybe just selected is now valid HTML5 The value of the option tag is whatever you set it to. If you set it to $city->name, then you want to use $city->name in the "selected" statement. Take a look at the source of the form on the rendered page to see what the value of the options is showing and echo the $session->current_city and see if they match. That should help you debug. 1 Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 23, 2013 Author Share Posted June 23, 2013 Hello Adrian, The problem was, I wanted the cities to be displayed like 'New York', rather than 'new-york'. With $user->user_city, initially I was only getting the array's index value(I guess that's what its called), like 2. In the backend, user_city was a select box using input like 1:=New York, 2:=Toronto, etc. So I used soma's advice on some other thread and tried to get the name of the city instead of the array index. The real problem I'm facing is, every time I add a custom field to user, such as user_city, I need to modify FrontendUserProfiles module to make it available to users and also to allow users to be able to add & update their city. The reason is, I'm completely dependent on the module which somehow I feel is making it less flexible for me to do any kind of modifications to user related data. I'm starting over with user management. I will be creating users through PW's core user module and will link a page to every user, this page can have all the rest of information such as user's city. Thus I can easily add the "cities" pagefield which we created, to user's profile. This way, cities available for users to select will be exactly the ones that are available for theatres. This will also help if in future we'r going to add more cities. So I guess that'll solve all my current problems would be solved. Thanks a lot for your help. Link to comment Share on other sites More sharing options...
adrian Posted June 23, 2013 Share Posted June 23, 2013 If $user->user_city is return a number, then surely $user->user_city->title will give you New York etc and $user->user_city->name will give you new-york. You should use name for all cases where you are storing the city and checking it against another variable. Then use title to display it. However I think you know this already. Not sure if your user's city field is a single or multiple, but you should make it single, otherwise you might need to use $user->user_city->first()->title to get the title. I haven't used the FrontendUserProfiles module (I have done a custom setup), but I wouldn't worry about modifying it to suit your needs. Perhaps rename it so there is no chance of it being overwritten from the modules manager if you do an update. Or you could convert it so it is no longer a module - take the code and use it I don't think you need the linked page for the user's data - I still think the best way is to add those additional fields to the user template - that will make accessing this info much easier - eg: $user->user_city 1 Link to comment Share on other sites More sharing options...
Vineet Sawant Posted June 23, 2013 Author Share Posted June 23, 2013 If $user->user_city is return a number, then surely $user->user_city->title will give you New York etc and $user->user_city->name will give you new-york. I don't think it works that way if the user_city custom_field is not a pagefield. I'm using a select field type for storing city list, not cities pagefield which I was using for theatres. I don't think you need the linked page for the user's data - I still think the best way is to add those additional fields to the user template - that will make accessing this info much easier - eg: $user->user_city I completely agree with that. Thus, I think just like you, having a custom setup would be better. I might need your help in setting up a custom login/registration & profile editing set up. But I'll start by trying it on my own, if I feel I just can't do it, I hope you'll be there to help me out. I really do appreciate your help a lot. Thanks again. 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