Chris Cummings
Chris Cummings

Reputation: 1548

Stop PHP Pagination for certain results

I have a page that has a list of Youtube videos. I use an array as below then later in the page I use some other functions to display the title and screen grab of the video down the page. This way I just have to add a new vidoe to the list below and it grabs all the correct info from YouTube:

//exampleX is a youtube video code
$videolist = array(
"example1",
"example2",
"example3",
"example4",
"example5"
);

Over time this list has grown to the point where I need to add some automatic pagination for the videos as the list continues to grow. I've done that once before so I think I can handle that part.

However the customer has some videos that need to stay together on the same page no matter what. I'm not sure how to handle that part.

Any thoughts on this would be appreciated.

Thanks, Chris

Upvotes: 1

Views: 212

Answers (3)

Michael Berkowski
Michael Berkowski

Reputation: 270637

As a simple extension of what you already have without needing too much modification, consider making your array of videos multidimensional, with a key that indicates it should be kept together. When paginating them, if you encounter one having keeptogether => TRUE at the end of your page's list, rather than breaking, continue looping output until you reach the next keeptogether => FALSE. (I would also build in the ability to omit the keeptogether key as equivalent to a FALSE value).

$videolist = array(
  array("title" => "example1", "keeptogether" => FALSE),  
  array("title" => "example2", "keeptogether" => FALSE),
  array("title" => "example3", "keeptogether" => TRUE),
  array("title" => "example4", "keeptogether" => TRUE),
  array("title" => "example5" "keeptogether" => FALSE)
);

If you need it to work more elegantly when a keeptogether => TRUE occurs at the end of a page and it should instead break to the next page before continuing (so the current page doesn't get too long), consider a simple algorithm like:

# Pseudocode:
# Encountering a keeptogether == TRUE when 2 from the end of a normal page length:
if current_page_counter < (num_per_page - 2) and keeptogether == TRUE
  break_to_next_page
else
  loop_until keeptogether == FALSE

Upvotes: 2

Scutterman
Scutterman

Reputation: 387

Have you considered using a multi-dimensional array to store the videos? Each element of the $videolist array would contain an array of videos. Most of these would only have one element, but ones which need to be grouped together would be in the same array, like so:

$videolist = array(
    array("example1"),
    array("example2"),
    array("example3", "example4"),
    array("example5")
);

Then, when displaying the videos, you can do something like this:

$Offset = 1;
$PerPage = 2;
$Count = 0; $Displayed = 0;
foreach ($videolist as $videoarray) {
    foreach ($videoarray as $video) {
        $Count++;
        if ($Count <= $Offset) continue;

        $Displayed++;
        // Display Video
    }

    if ($Displayed >= $PerPage) break;
}

This way, the page doesn't end until the end of a current group.

You can also change it, so you do a count() of $videoarray before iterating over it and break if it would push the total over $PerPage

Upvotes: 0

Los Frijoles
Los Frijoles

Reputation: 4821

Perhaps in your list you can sort them by groups and paginate by group. Most groups would only have one video, but those ones which need videos together would have multiple videos. Like so:

$videolist = array(
    "group1" => array("example1")
    "group2" => array("example2", "example3"),
    "group3" => array("example4"),
    "group4" => array("example5"),
);

So instead of paginating by video, you paginate by group and usually a group has one video, but it could have more than one.

If you use a database for storing these as well you can replicate this structure there.

Upvotes: 1

Related Questions