Jump to content

Paging not fully working with date range search results


formulate
 Share

Recommended Posts

I have a search form on the following page:

http://www.mtwashingtonaccommodation.com/list/

Everything works as expected. Paging works (just browsing, no search) and if I search by criteria such as "Hot Tub", etc. everything is all good.

When I specify an end-date, I get the correct results on page 1. As soon as I go to page 2 the pager seems to increase my results count by some random amount of results that should not be included as they don't meet the end-date criteria. I haven't been able to figure out any rhyme or reason to this arbitrary number nor the more major issue of why the pager seems to mess up after page 1 when there's an end date specified.

Essentially, on pages after 1, the pager disregards my end-date and randomly adds any results it wants.

Yes I have URL numbering on, white listing my search variables, etc. The search and paging works great for everything *but* my end-date.

My code is admittedly a bit chaotic and there's probably better ways of doing things, but essentially I'm doing the following:

Use $input->get to grab the variables from the search form.

White list the variables.

Get all units that match the search criteria (ie: hot tub, etc.)

If no end date specified then render the output with paging (this works fine)

If there's an end date then go through all the bookings and get only available units then render the output with paging (this only works for page 1 of the results)

I'll post my crazy code in a separate post just to keep it out of this one. Maybe some fresh eyeballs and some higher level understanding of what's going on will help me out.

Thanks!

Link to comment
Share on other sites

Here's the code:

// bunch of stuff happens first to get search variables and build a selector to filter units

// get all units using matching unitSelector & ensure owner is active
$active = $users->find("accountstatus='Active'");
$unitsAll = $pages->find("owner=$active,$unitSelector");
$results = $pages->find("sort=unitRateWeekdayBase,owner=$active,limit=$limit,$unitSelector");

// get dates and convert to unix time stamp
$frontStartDate = $input->get->startDate;
$frontEndDate = $input->get->endDate;

$input->whitelist('endDate', $input->get->endDate);
$input->whitelist('startDate', $input->get->startDate);

$frontStartDate = strtotime($frontStartDate);
$frontEndDate = strtotime($frontEndDate);

// if end date specified, further filter results by date
if($frontEndDate){

    $unitSelector2 = "template=unit";
    $unitList = '';

    foreach($unitsAll as $unit){

        $dateSelector = "bookingEndDate>=$frontStartDate, (bookingEndDate<=$frontEndDate), (bookingStartDate<=$frontEndDate)";

        // check if unit has any bookings using dateSelector
        $tmp = $pages->find("template=booking,unitID=$unit->id,limit=1,$dateSelector");

        // if no bookings, add the unit to a list that will be used for a new selector
        if(count($tmp) == 0){
            $unitList .= $unit->id.'|';
        }

    }

    // add date filtered units to new unit selector
    $unitSelector2 .= ",id={$unitList}";

    // generate results based on units filtered by date using the new unit selector
    $results = $pages->find("limit=$limit,$unitSelector2");

}

// generate output

if(count($results) == 0){

    $out = '<h2>No Availability</h2>';
    $out .= '<p>Please revise your search options.</p>';

}
else{

    $out .= "<h2>{$results->getTotal()} Available</h2>";

    foreach($results as $unit){
        $building = wire("pages")->get("$unit->buildingID");
        $out .= unitData($unit,$building);
    }

    $out .= $results->renderPager();

}
Link to comment
Share on other sites

I'm currently short on time, but I noticed my date selection code in there and I've seen a better selector for that from ryan some time ago:

// select anything overlapping the range of $start - $end
$selector = "enddate>=$start, (enddate<=$end), (startdate<=$end)";
  • Like 2
Link to comment
Share on other sites

I'm currently short on time, but I noticed my date selection code in there and I've seen a better selector for that from ryan some time ago:

// select anything overlapping the range of $start - $end
$selector = "enddate>=$start, (enddate<=$end), (startdate<=$end)";

While this didn't solve the problem, I really appreciate the more elegant date selector. I will update my code post above to reflect the new selector.

Now, to get this silly paging to work!

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...