Jump to content

Need some pointers about pagination


FridayRain

Recommended Posts

I'm working on a website that is basically my online writing portfolio. I'll have pieces of poetry, fiction, nonfiction, and more for display. I'd like the longer pieces, usually fiction and nonfiction, to be paginated. I'm having trouble figuring just how to do it. I want it to be automatic, either as soon as I insert a piece into the database or after querying the database to display the piece. Also, I coded an input form so I can insert pieces into the database with ease. This is working well. I've also got a basic query that will bring up the piece and its title.

 

My question is which way to handle pagination. When I use my form to insert a piece, can I use PHP to automatically insert a pagebreak indicator every 1500 or so characters that ends on a period? Or is there a somewhat simple way to split a piece over multiple pages during the query?

 

 

Basic query to display piece:

<?php

import_request_variables('G', 'url_');

if ((isset($_GET[piece])) && ($_GET[piece] != "")) {

require("db/config.php");
require("db/opendb.php");

    $query = "SELECT text, title, year FROM nonfiction WHERE id=". mysql_escape_string($_GET[piece]);
    $result = mysql_query($query);
    $content = mysql_fetch_assoc($result);

require("db/closedb.php");

    echo "<h3>$content[title]</h3><br />";
    echo "<p>$content[text]</p><br />";
    echo "<span class=\"textcopy\">© $content[year]</span> <span class=\"courier\">FridayRain</span>";
    } else {
    echo "<span class=\"choose\">Please choose a title.</span>";
    }
?>

Link to comment
Share on other sites

1. Count the number of total results. Using COUNT in MySQL will be fastest, but mysql_num_rows will work.

2. If the result count is more than you want in a page, do a LIMIT query. That way you only get the first 40 or so results.

3. Have your "Next" link, link to the same page, with an added GET paramter to tell it to offset the results by 40.

 

That's the best I can explain it without writing an essay. It's best to learn by going through a tutorial.

 

http://www.phpfreaks.com/tutorials/43/0.php

 

Link to comment
Share on other sites

Thank you for replying, but my query isn't about returning many results from the database. I'm pulling just one piece of writing to display on the page after the user clicks on a title. I just need to split it over multiple pages so I don't have a vertical scrollbar a mile long.

Link to comment
Share on other sites

Oh, sorry. But it's a similar sort of idea. Rather than passing a page number in GET, you pass the Title field. Then you use that in a WHERE query on the next page.

 

Here's an example:

 

index.php:

<a href="book.php?title=bible">Bible</a><br>
<a href="book.php?title=something+happened">Something Happened</a><br>
<a href="book.php?title=lord+of+the+rings">Lord of the Rings</a><br>

 

book.php:

The book you picked was <?php echo htmlspecialchars($_GET['book']) ?>.

Link to comment
Share on other sites

One possibility is to insert a 'marker' into the story every 500 or so words.  Obviously something that never occurs as part of the story, e.g. [break].

 

Retrieve the text from database, use the explode() function to split the text at your [break] markers and echo out the nth array element depending on which 'page' of the story you're showing.

 

$parts = explode("[break]", $the_whole_story);
echo $parts[0]; // FIRST array element

 

You'll need to pass the array element number in the link to the 'next' part so you display $parts[1] on the second page, etc.

Link to comment
Share on other sites

One possibility is to insert a 'marker' into the story every 500 or so words.

How might I go about doing that?

 

Manually, when you enter the story or manually by editing the database is how I've done it.  Programmatically it's a bunch of string manipulation, substring extraction, and concatenation before entering it into the database.

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.