• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

PHP Pagination/Paging

GJSNeptune

[H]F Junkie
Joined
Mar 31, 2004
Messages
12,372
It's kicking my butt. I've Googled endlessly for tutorials and examples, but implementing the different steps/parts with my existing queries is hopeless.


Here's my working query that brings up a writing piece (in this case nonfiction) after a variable is passed through a URL from an earlier query.
Code:
<?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\">&copy; $content[year]</span> <span class=\"courier\">FridayRain</span>";
    } else {
    echo "<span class=\"choose\">Please choose a title.<br /><br /><br /><br /><br /><br /><br /></span>";
    }
?>

I don't want something overly complicated, but the simpler pagination walkthroughs seem to have more than one database query, and I'm struggling to figure out how to work my working query into them.

http://www.php-mysql-tutorial.com/php-mysql-paging.php (I like this one)
http://php.about.com/od/phpwithmysql/ss/php_pagination.htm
http://www.vipercreations.com/tutorials/PHP/19.html

Does anyone have a good guide?
 
well the reason why you arent able to apply the pagination tutorials to your current work is because your current stuff returns just one thing. and since it's an essay or written piece, i'm guessing you want to paginate one essay into multiple pages. unfortunately you wouldn't want to do this with mysql, since pagination with mysql involves modifying the LIMIT x, y portion of the sql query. what you'd need to do is using php, split up your written piece into multiple pages based on length.
 
well the reason why you arent able to apply the pagination tutorials to your current work is because your current stuff returns just one thing. and since it's an essay or written piece, i'm guessing you want to paginate one essay into multiple pages. unfortunately you wouldn't want to do this with mysql, since pagination with mysql involves modifying the LIMIT x, y portion of the sql query. what you'd need to do is using php, split up your written piece into multiple pages based on length.

That's exactly what I want to do. Am I searching for the wrong PHP function? Is it not pagination or paging?
 
yeah, you're looking in the wrong place. it is still considered pagination just not with mysql. what you want to do is something like this:

Code:
$essay = "your essay here";

$pages = str_split ( $essay , 500); /* each page is 500 characters. */

$numpages = count($pages); /* now you know how many total pages there are. */

print $pages[0]; /* page 1 */

print $pages[1]; /* page 2 */

that should get you started.
 
you may notice my first suggestion might cut up your page mid-word or mid-sentence. if you dont like it, you can split up the pages yourself by inserting a special unique character sequence everyplace you want a new page to begin.

Code:
$essay = "this is page 1. `|` this is page 2. `|` this is page 3";

$pages = explode('`|`', $essay);

/* now you can do the same thing with $pages as in my previous example. */
 
you may notice my first suggestion might cut up your page mid-word or mid-sentence. if you dont like it, you can split up the pages yourself by inserting a special unique character sequence everyplace you want a new page to begin.

Code:
$essay = "this is page 1. `|` this is page 2. `|` this is page 3";

$pages = explode('`|`', $essay);

/* now you can do the same thing with $pages as in my previous example. */

Yeah this is what I have coded for my current site, except I use "[PAGE]" to explode the string instead of "|".
 
Here is the script I use. This should get you on the right track. Simply called by a function:

The working example is on http://www.juicedgaming.net

Put this in a functions page(or somewhere in the top of the page).
PHP:
// Variables for the page script
$page = $_GET['page'];
if(!isset($page)) $page = 1;
$max_results = 4;
$from = (($page * $max_results) - $max_results);

function showPageNumbers($max_results, $page)
{
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) AS Num FROM `news`"),0);
	$total_pages = ceil($total_results / $max_results);
	if($total_pages > 1){
		echo "<div align='right'><b>Select a Page&nbsp<br/>\n";
		if($page > 1){ 
			$prev = ($page - 1); 
			echo "<a href=\"index.php?page=$prev\">&lt;&lt;&nbsp;Previous</a>&nbsp;\n";
		}
		for($i = 1; $i <= $total_pages; $i++){ 
			if(($page) == $i){ 
				echo "$i "; 
				} else { 
					echo "<a href=\"index.php?page=$i\">$i</a>&nbsp;\n"; 
			} 
		}
		if($page < $total_pages){ 
			$next = ($page + 1); 
			echo "<a href=\"index.php?page=$next\">Next&nbsp;&gt;&gt;</a>\n";
		}
		echo "&nbsp;</b></div>\n";
	}
}
Then place this where your query is
PHP:
$query = "SELECT * FROM `news` ORDER BY `id` DESC LIMIT $from, $max_results";
//This function will render the script onto your page
showPageNumbers($max_results, $page);
 
yeah, you're looking in the wrong place. it is still considered pagination just not with mysql. what you want to do is something like this:

I'm not following. I have to manually insert what text to display? Why have a database then? I want to query the database for an article's text and then have it display across multiple pages.

Can you try to incorporate what I need with the code I already have in place?


EDIT: Nevermind. I made $essay = $content[text] and I got it to split, but what about the previous and next buttons?
 
I really think you should just pick up an intro to programming book because it seems you'll always be in the dark as to how to do x and y. But to answer your question, basically you need to pass around another $_GET variable in your url that designates the page #. So your url will look like this: viewessay.php?piece=1&page=1

after retrieving the essay and storing it into $pages:

Code:
$numpages = count($pages);

/* If the page variable is set too high or too low, set it to default to page 1 */
if($_GET['page'] > $numpages || $_GET['page'] < 1)
{
  $_GET['page'] = 1;
}

/* Print the page the variable specified */
print $pages[$_GET['page'] - 1];

now to print the prev and next buttons:

Code:
$prevlink = '<a href="viewessay.php?piece='. $_GET['piece'] .'&page='. ($_GET['page'] - 1).'">prev</a>';

$nextlink =  '<a href="viewessay.php?piece='. $_GET['piece'] .'&page='. ($_GET['page'] + 1).'">next</a>';

/* only print the previous page link if there is a previous page. */
if($_GET['page'] > 1)
{
   print $prevlink;
}

/* only print the next page link if there actually is a next page */
if($_GET['page'] + 1 <= $numpages)
{
  print $nextlink;
}
 
Is there a way for PHP/MySQL to process a longtext field input and insert a pagebreak indicator after say 1400-1500 characters if it's a period, and then use PHP to grab those pages and insert each of them into variables? Something with Regular Expressions?
 
hofan has pretty much given you all the code you need. If you want page numbers ..... count the number of bytes in the string and divide that by however many bytes you want to show per page.

Say you have a 1001 byte article (small I know, but good for simple math) and you want 250 bytes per page (5 pages). Get the full size of the article (1001 bytes), divide by 250. You'll have 5 pages total and start out on page one by default by reading only the first 250 bytes as explained by hofan.

By dividing 1001 bytes by 250 bytes per page you know how many page numbers to provide(1, 2, 3, 4 and 5). Use a $_GET variable to keep track of the page your on and multiply that page number by your initial starting byte you want to read. From their, you can still rely on your constant of 250 bytes to read from the current position plus the next 250 bytes. You'll have to adjust for page 0 however you please, but something for page 2 may look like:
Code:
$page_num = $_GET["page_num"];
$end_byte = $page_num * $BYTES_PER_PAGE;      //page 2 has bytes 250-500
$chomp_these_bytes = ($page_num - 1) * $BYTES_PER_PAGE;      //ignore bytes less than this (250)
.......
//read in your code up to $end_byte
//chomp bytes up to $chomp_these_bytes

I forget the syntax for chomping but its a quick easy function. Essentially you've traversed the file, grabbed page 2's 500 bytes, then chomped the first 250 off and your left with the remaining bytes you want to show on the page, bytes 250-500.

Scale that to whatever fits.
 
Is there a way for PHP/MySQL to process a longtext field input and insert a pagebreak indicator after say 1400-1500 characters if it's a period, and then use PHP to grab those pages and insert each of them into variables? Something with Regular Expressions?

yeah you can do something with regular expressions or you can traverse 1400 chars down the string and then do a forward linear search for a period before inserting the pagebreak indicator (called a delimiter).
 
Back
Top