Jamie Gordon Posted July 4, 2013 Share Posted July 4, 2013 Hi guys, having a small issue and I know how helpful you lot can be so I thought I would chance my arm again! I have a search form on my home page in which the user selects a country and the hotels from that country are displayed on the search page. This is my code: <?php if( isset($_POST['mysearchlocation']) ) { $country = $pages->find("template=hotel, hotel_country=$country, limit=3"); $pagination = $country->renderPager(); foreach($country as $resort) { echo "<li class='result-item star$resort->star_rating'>"; $firstimage = $resort->images->first(); echo "<img src='$firstimage->url'>"; echo "<div class='result_panel'><a>"; if ($resort->favourite) { echo "<h2 class='flagged'>$resort->hotel_name</h2><p>"; } else { echo "<h2>$resort->hotel_name</h2><p>"; } //get area & country name $parents = $resort->parents->slice(2); foreach($parents as $parent) { echo "<a href='$parent->url'>$parent->title</a>,"; } echo "</p></a>"; echo "<img class='star_rating' src='../img/icons/stars_$resort->star_rating.png' >"; echo $resort->hotel_facilities; echo "</div>"; echo "<a class='btn more_info_btn' href='$resort->url'>More info<span class='icon'> ></span></a>"; echo "</li>"; } } echo $pagination; ?> the issue that I am having is that when I select the next page of results, for example page 2, my post variable is obviously lost. Has anyone any ideas on how to work around this because I'm drawing a blank at the moment! Any help would be greatly appreciated, Jamie Link to comment Share on other sites More sharing options...
DaveP Posted July 4, 2013 Share Posted July 4, 2013 From your code, you don't seem to be using that $post variable anywhere other than in the if(), but I'm assuming that you use it to set $country. (Correct me if I'm wrong.) Suggestions- Use $input->post["mysearchlocation"] rather than accessing $post directly (not essential) Run the result through $sanitizer (essential) Save the result in $session and access it from there on subsequent page loads. 2 Link to comment Share on other sites More sharing options...
kongondo Posted July 4, 2013 Share Posted July 4, 2013 +1 for DaveP's suggestion. See this also about input variable: http://processwire.com/api/variables/input/ How does the blog profile do it, btw? I'm looking at the code but can't see how it renders pagination on search results...Sessions? 1 Link to comment Share on other sites More sharing options...
Jamie Gordon Posted July 4, 2013 Author Share Posted July 4, 2013 I knew there was a reason why I loved you guys! Apologies about the code formatting, strangely it only jumps out of shape when I have pasted it into here, odd. Anyway I'll give your suggestion a go! Link to comment Share on other sites More sharing options...
Wanze Posted July 4, 2013 Share Posted July 4, 2013 Simplest solution: Use GET To make it automagically work with the pager, you need to add the variable to Pw's whitelist. Pw does append your get variables to the paging. if ($input->get->mysearchlocation) { $county = $sanitizer->selectorValue($input->get->mysearchlocation); $input->whitelist('country', $country); // Do your search and render the pager } 2 Link to comment Share on other sites More sharing options...
Jamie Gordon Posted July 4, 2013 Author Share Posted July 4, 2013 I still don't seem to be getting any further, even with the help of your suggestions now First off, the $input->get->mysearchlocation method doesn't not work at all to display the selected country, it just returns an empty value which is strange, using $input->post seems to work fine. I have ammended my code to look like this - <?php $userselection = $sanitizer->selectorValue($input->get->mysearchlocation); $session->country = $userselection; if($session->country) { $country = $pages->find("template=hotel, hotel_country=$country, limit=3"); $pagination = $country->renderPager(); foreach($country as $resort) { echo "<li class='result-item star$resort->star_rating'>"; $firstimage = $resort->images->first(); echo "<img src='$firstimage->url'>"; echo "<div class='result_panel'><a>"; if ($resort->favourite) { echo "<h2 class='flagged'>$resort->hotel_name</h2><p>"; } else { echo "<h2>$resort->hotel_name</h2><p>"; } //get area & country name $parents = $resort->parents->slice(2); foreach($parents as $parent) { echo "<a href='$parent->url'>$parent->title</a>,"; } echo "</p></a>"; echo "<img class='star_rating' src='../img/icons/stars_$resort->star_rating.png' >"; echo $resort->hotel_facilities; echo "</div>"; echo "<a class='btn more_info_btn' href='$resort->url'>More info<span class='icon'> ></span></a>"; echo "</li>"; } echo $pagination; } else { echo "the value has not been found"; } ?> This displays the first three results fine and the pagination bar shows up with multiple pages, however when I click one of these pages it returns a page with empty results, because the $session value for some reason has not been stored correctly, so it has nothing to load. I have also tried Wanze's method, that again brought up empty results for the second page. Any ideas? Link to comment Share on other sites More sharing options...
Wanze Posted July 4, 2013 Share Posted July 4, 2013 You did change your form method to GET? Link to comment Share on other sites More sharing options...
Jamie Gordon Posted July 5, 2013 Author Share Posted July 5, 2013 Yep, I changed the opening tags on my form to <form action="<?php echo $pages->get(1042)->url; ?>" method="get" name="search_form" class="search_form"> but the value doesn't seem to be held when I load the second paginated page. This is so bizzare! Link to comment Share on other sites More sharing options...
Wanze Posted July 5, 2013 Share Posted July 5, 2013 Can you post your whole code? form and rendering with pagination. I know it works because I did it several times this way, so there must be somewhere a problem in your code Link to comment Share on other sites More sharing options...
Jamie Gordon Posted July 5, 2013 Author Share Posted July 5, 2013 Home page - search form <form action="<?php echo $pages->get(1042)->url; ?>" method="get" name="search_form" class="search_form"> <input type="hidden" name="mysearchlocation"> A one week trip <div id="search_country" class="dropdown_wrapper"> <div class="dropdown"> <ul> <li><a class="flag prt" onclick="document.search_form.mysearchlocation.value='portugal'; return false;" href="#">Portugal</a></li> </ul> </div> </div> <button type="submit" id="search_btn_reg" class="btn btn_search float_right">Search<span class="icon"> ></span></button> </form> and then this is the code for my search results page <?php $country = $sanitizer->selectorValue($input->get->mysearchlocation); $input->whitelist('country', $country); ?> <?php if ($input->get->mysearchlocation) { $input->whitelist($test, $country); // Do your search and render the pager $country = $pages->find("template=hotel, hotel_country=$country, limit=3"); $pagination = $country->renderPager(); foreach($country as $resort) { echo "<li class='result-item star$resort->star_rating'>"; $firstimage = $resort->images->first(); echo "<img src='$firstimage->url'>"; echo "<div class='result_panel'><a>"; if ($resort->favourite) { echo "<h2 class='flagged'>$resort->hotel_name</h2><p>"; } else { echo "<h2>$resort->hotel_name</h2><p>"; } //get area & country name $parents = $resort->parents->slice(2); foreach($parents as $parent) { echo "<a href='$parent->url'>$parent->title</a>,"; } echo "</p></a>"; echo "<img class='star_rating' src='../img/icons/stars_$resort->star_rating.png' >"; echo $resort->hotel_facilities; echo "</div>"; echo "<a class='btn more_info_btn' href='$resort->url'>More info<span class='icon'> ></span></a>"; echo "</li>"; } } echo $pagination; ?> I understand that I am doing something wrong to hold the variable when it loads the next pagination page, just not sure what that is. Link to comment Share on other sites More sharing options...
Wanze Posted July 5, 2013 Share Posted July 5, 2013 1) Are your sure that the value 'portugal' is sent over get? So your URL is xxx?mysearchlocation=portugal 2) Some corrections to the code: // Do we have a get variable? if ($input->get->mysearchlocation) { // Sanitize it $mySearchLocation = $sanitizer->selectorValue($input->get->mysearchlocation); // This is not valid => $input->whitelist($test, $country); // Add it to the whitelist... with the same name as the get var! $input->whitelist('mysearchlocation', $mySearchLocation); // Do your search and render the pager.. i name the variable $resorts instead of $country $resorts = $pages->find("template=hotel, hotel_country=$mySearchLocation, limit=3"); $pagination = $resorts->renderPager(); foreach ($resorts as $resort) { // ... } } See also $input->whitelist() in the docs: http://processwire.com/api/variables/input/ 3 Link to comment Share on other sites More sharing options...
Jamie Gordon Posted July 5, 2013 Author Share Posted July 5, 2013 That works perfectly Wanze thank you for the help, I really appreciate it! My value for 'portugal' was being sent correctly so that I got it in my URL, I wasn't using the implementation of $input->whitelist correctly to store it. Again thank you for all your help 1 Link to comment Share on other sites More sharing options...
diogo Posted December 22, 2013 Share Posted December 22, 2013 Everyone trying to sanitize Portugal... 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted December 22, 2013 Share Posted December 22, 2013 Not that I like it... (for the people) 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