Jump to content

Split a text field from database in pages

- - - - -

  • Please log in to reply
3 replies to this topic

#1
constantinone

constantinone

    Newbie

  • Members
  • Pip
  • 2 posts
I have text field in database, where I have let's say 5000 words.
I want to split this text having 5000 words in pages, each one having 500 words.
How can I realise this ?
A practical example would be appreciate !
Thank you in advance.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
My approach would be to use the explode() function to convert the text file to an array of individual words. Then, you could easily write a function to return a subset of this array using array_slice().

Documentation on the explode() function:
PHP: explode - Manual

Example:

// Assuming your 5000 word file is in the variable $text, and assuming the words are delimited by whitespace

$text_array = explode(" ", $text);

// If you want page 1 of the array:

$page_one = array_slice($text_array, 0, 500);

// If you want page n in the array:

$page_n = array_slice($text_array, $n * 500, 500);


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
As you are working with the database, do you mean pagination? i.e. 500 words are displayed, and there is a link to the next page?
http://forum.codecal...post206671.html
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
constantinone

constantinone

    Newbie

  • Members
  • Pip
  • 2 posts
Yes that's right.
I mean that that field text from database could have 5000, 7000 words, maybe less maybe more.

And I intend to break it in blocks of 500 words on a page and then to display a link to the next page where
you can see the next 500 words and so ....

PS
Yes this must work with database.

Thank you very much for help !
It works great.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users