Forum Moderators: coopster

Message Too Old, No Replies

Pagination php (splitting article into pages)

         

Velo

1:01 am on Nov 20, 2006 (gmt 0)

10+ Year Member



This is an older posted version but I need more help:

All the articles/tutorials I've found on pagination always refer to paginating multiple rows out of a database. Like displaying 5 rows on each page. But I'm trying to paginate an article that is being stored in one row of the table.

I'm pulling an article from the "article" cell out of the "Articles" table. And want the article to display something like 1500 chars or 15 lines on a page?

I know it is possible, I was just wondering if anyone knew how? The main problem is how do I get it to go on to the next page and display the "<--Previous page [1] [2] [3].... Next-->" links and all that stuff as well? Can I do this using one php page, or will I need several php pages?

Thanks for any help.

eelixduppy

1:17 am on Nov 20, 2006 (gmt 0)



Welcome to WebmasterWorld, Velo!

There are tons of threads on this topic here at WW, you just have to search for them [google.com] ;)

>>Can I do this using one php page

Yes.

>>But I'm trying to paginate an article that is being stored in one row of the table.

Instead of using LIMIT within the query, use something like SUBSTRING [dev.mysql.com] to extract a certain amount of text. However this may cause a problem because it may cut off a word, in which case it may be better to extract a little more than what you want and then cut it off at the last work. Refer to How to avoid words being chopped with SUBSTR [webmasterworld.com] for more information.

Best of luck!

Velo

2:25 am on Nov 20, 2006 (gmt 0)

10+ Year Member



Hey Thx for the welcome :)

Well, I know what you are saying in using the substring function. I think I could/can figure that part out. I think what I need though is an actual example of code that paginates from within one field/cell. Every example in the search results is a pagination splitting rows that are easily split and accessible via other options like id or number. Paginating within only one cell does not allow that, and so I am having trouble.

Not to mention I am a complete newb. lol. I know most of the time getting things like this to work it through trial and error, so I will keep working on it. Thanks.

eelixduppy

10:15 pm on Nov 20, 2006 (gmt 0)



Post what you have worked on so far and we'll see what we can do with it :)

Please only post relevant code as to make this as easy to read as possible, along with abiding to the forum charter [webmasterworld.com].

Velo

2:28 am on Nov 21, 2006 (gmt 0)

10+ Year Member



Ok, to solve this problem this was my idea. To first get the character length of my article via the strlen($info["article"]) command. Then divide that result by the number of characters I want on each page (1500 in this instance). So I started with this:

(since the code style is extremely small I will designate my code with ----CODE BEGINS---- and ----CODE ENDS---)

------CODE BEGINS-----
$query=mysql_query("SELECT title, article, time, id, author FROM Articles where id={$_GET['id']} ORDER BY `time` DESC LIMIT 0, 30");
while ($info=@mysql_fetch_array($query)) {
$length = strlen($info["article"]);
$numpages = $length/1500;
}
-----CODE ENDS-----

Then I tried to implement the values of my overall length and total number of pages into a tutorial I found on creating pagination links and this is what I have overall (minus some other irrelavent echo's and stuff):

----CODE BEGINS-----
// Connecting to a database
$query=mysql_query("SELECT title, article, time, id, author FROM Articles where id={$_GET['id']} ORDER BY `time` DESC LIMIT 0, 30");

// Let's say you entered a few info fields and you have a few items in your database. You have to loop the query in order to get all the info. We do this the following way

while ($info=@mysql_fetch_array($query)) {

$page = intval($_REQUEST['page']);
$substring_num = ($page * 1500) - 1500;
$end_subs = $substring_num + 1500;

echo ("<div class='text1' style='margin-left: 20px; margin-right: 20px; line-height: 1.5'>").substr($info["article"], $substring_num, $end_subs)."</div><br /><br />";

$length = strlen($info["article"]);
$numpages = $length/1500;

$limit = $numpages + 1;
$limitvalue = $page * $limit - ($limit);
if($page!= 1){
$pageprev = $page--;
// Fancy way of subtracting 1 from $page

echo("<a href=\"http://www.example.com/article.php&page=$pageprev\">PREV".$limit."</a> ");
/* Tip: It is a good idea NOT to use $PHP_SELF in this link. It may work,
but to be 99.9% sure that it will, be sure to use the actual name of the file
this script will be running on. Also, the adds a space to the end of
PREV, and gives some room between the numbers. */
}else
echo("PREV".$limit." ");
// If we're on page 1, PREV is not a link

for($i = 1; $i <= $numpages; $i++){
/* This for loop will add 1 to $i at the end of each pass until $i is greater
than $numofpages (4.08). */

if($i == $page){
echo($i." ");
}else{
echo("<a href=\"http://www.example.com/article.php&page=$i\">$i</a> ");
}

/* This if statement will not make the current page number available in
link form. It will, however, make all other pages available in link form. */
} // This ends the for loop

if(($length % 1500)!= 0){
/* The above statement is the key to knowing if there are remainders, and it's
all because of the %. In PHP, C++, and other languages, the % is known as a
Modulus. It returns the remainder after dividing two numbers. If there is no
remainder, it returns zero. In our example, it will return 0.8 */

if($i == $page){
echo($i." ");
}else{
echo("<a href=\"http://www.example.com/article.php&page=$i\">$i</a> ");
}
/* This is the exact statement that turns pages into link form that is used

above */
} // Ends the if statement

if(($length - (1500 * $page)) > 0){
/* This statement checks to see if there are more rows remaining, meaning there
are pages in front of the current one. */

$pagenext = $page++;
// Fancy way of adding 1 to page

echo("<a href=\"http://www.example.com/article.php?page=$pagenext\">NEXT".$limit."</a>");
/* Since there are pages remaining, this outputs NEXT in link form. */
}else{
echo("NEXT".$limit);
/* If we're on the last page possible, NEXT will NOT be displayed in link
form. */
}

//mysql_free_result($result);
/* This line is not required, since MySQL will free the result after all
scripts have finished executing; however, it's a nice little backup. */

}
----CODE ENDS----

It currently displays nothing but my footer.....any ideas on what I'm doing wrong?

[edited by: jatar_k at 6:56 pm (utc) on Nov. 21, 2006]
[edit reason] examplified urls [/edit]

eelixduppy

8:06 pm on Nov 22, 2006 (gmt 0)



Have you tried a query like:

$query=mysql_query("SELECT `title`,[b]SUBSTRING(`article`,0,1500)[/b] as `text`,`time`,`id`,`author` FROM Articles where id='".mysql_real_escape_string($_GET['id'])."' ORDER BY `time` DESC LIMIT 0, 30");

If you make the SUBSTRING arguments dynamic based on the page you are viewing, it will select a different amount of characters. This is assuming that they exists so you might want to run a check on that first. Also, words are going to be chopped off, you you may want to refer to the link I provided earlier.