Jump to content

Pagination?


NikJ92

Recommended Posts

I don't understand how you're supposed to use it to split something like Myspace layouts into different pages using the pagination tutorial.

 

It's actually a preview picture of the layout linked to a live preview with the name of the layout at the top and a horizontal rule beneath it, and this is repeated for each layout. So something along the lines of:

 

<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>
<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>

 

For two layouts.

 

The code worked for the numbers in the examples, I just don't know how to make this work with those tags in the HTML above.

Link to comment
Share on other sites

Do you want to display X images per page? Where do you get the information for those images? Basically, to create pagination you'll need to know three things:

1) The total number of items (we'll call it numItems)

2) The items that should be on each page (we'll call it itemsPerPage)

3) The requested page (defaults to 1)

 

Then can say: numPages = numItems / itemsPerPage

Then you can check if the requested page is out of range. If it's a valid page then you can get the items within that range. E.g. if you have 10 items per page and you have 100 items then you'll have 10 pages. Then you just select the relevant items and display them to the user.

 

I hope that makes sense.

Link to comment
Share on other sites

Do you want to display X images per page? Where do you get the information for those images? Basically, to create pagination you'll need to know three things:

1) The total number of items (we'll call it numItems)

2) The items that should be on each page (we'll call it itemsPerPage)

3) The requested page (defaults to 1)

 

Then can say: numPages = numItems / itemsPerPage

Then you can check if the requested page is out of range. If it's a valid page then you can get the items within that range. E.g. if you have 10 items per page and you have 100 items then you'll have 10 pages. Then you just select the relevant items and display them to the user.

 

I hope that makes sense.

 

It does. :)

 

Yeah, basically. There'd be probably 15 images per page, and one of the layout pages (NFL) has 33, so that'd be about 3 pages. All of the image codes are posted on the nfl.php page, so would I be able to use pagination on my layout pages without using databases?

 

EDIT: Thought I'd mention I have read through the w3schools and tizag tutorials on PHP and most of MySQL and PHP on w3schools, although I only started PHP 4 days ago.

 

For the while loop, could looping through all the img, a, br, hr, etc. tags and putting them to an array work? Hope that makes sense.

Link to comment
Share on other sites

That would probably be a bit trickier. You don't necessarily need a database for creating pagination (our pagination inside the tutorials on the main site doesn't). You will have to extract each item and count how many there are. When you've done that then you can perform the steps outlined in my previous post.

 

Here is how our tutorial pagination works:

 

$pages = explode('[PAGEBREAK]', $content->body);
$pages = array_map('trim', $pages);

$numPages = count($pages);

if ($page < 1 || $page > $numPages) {
throw new PHPFreaks_Exception('Page out of range.');
}

$this->view->currentPage     = $page;
$this->view->numPages        = $numPages;
$this->view->currentPageBody = $pages[$page-1];

 

Page breaks are simply made by placing [PAGEBREAK] inside the body text of the tutorial.

Link to comment
Share on other sites

Ah, that makes more sense. ;D

 

That cleared much more up, but I'm still a bit confused.

 

So I'd extract each item and count how many of the layouts there are, store that number to a variable, set $itemsPerPage to 10 (since I want 10 layouts per page), then use numPages = numItems / itemsPerPage for the amount of pages (then probably the ceil() function to round the number up in case it's not an integer?), check if the page is in range?

 

Sorry, still new to scripting. >_<

Link to comment
Share on other sites

Hmm... providing all the links are formatted like you posted earlier, then you can do something like this:

 

<?php
$data = <<<EOF
<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>
<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>
<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>
<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>
<br><a href="http://site.com/layouts1.php?layoutpreview=Layout"><img src="img url"></a><br><hr>
EOF;

preg_match_all('`<br><a href="[^"]+"><img src="[^"]+"></a><br><hr>`mi', $data, $matches);

$items = $matches[0];

$numItems = count($matches);
$itemsPerPage = 2;
$numPages = ceil($numItems / $itemsPerPage);

// etc...
?>

 

Obviously you'll need to edit it to fit your needs.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.