NikJ92 Posted June 22, 2008 Share Posted June 22, 2008 http://www.phpfreaks.com/tutorial/basic-pagination/page2 I got the examples from the tutorial to function properly, but I'm having trouble using the code for my own purposes. How would I use it for say a Myspace layout site to split a bunch of Myspace layouts into different pages? Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/ Share on other sites More sharing options...
.josh Posted June 22, 2008 Share Posted June 22, 2008 What kind of trouble are you having? You're going to have to be more specific. Can you post some relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571730 Share on other sites More sharing options...
NikJ92 Posted June 22, 2008 Author Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571738 Share on other sites More sharing options...
.josh Posted June 22, 2008 Share Posted June 22, 2008 well what exactly is the problem? do you get a blank page? one of those broken link image things? Are your img tags inside the while loop? Again, it's kind of hard to help without you being more specific and showing some code... Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571739 Share on other sites More sharing options...
Daniel0 Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571740 Share on other sites More sharing options...
NikJ92 Posted June 22, 2008 Author Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571741 Share on other sites More sharing options...
Daniel0 Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571744 Share on other sites More sharing options...
NikJ92 Posted June 22, 2008 Author Share Posted June 22, 2008 Ah, that makes more sense. 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. >_< Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571767 Share on other sites More sharing options...
Daniel0 Posted June 22, 2008 Share Posted June 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111379-pagination/#findComment-571777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.