We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26968
    • 25 Posts
    Hi,
    I came across this plug-in:
    http://modxcms.com/extras.html?view=package/view&package=332

    But am unable to make it work in 0.9.6.3
    Its not really what I want, but I’ll use it for now if I can make it work. What I would ultimately like to do is paginate [*content*] based on character count.
    I am working on a project in which the posts are very long and would like a way to break them up into several pages. You can preview a live page here:
    http://www.ttmags.com/index.php?id=10

    Does anyone know how to make this work in modx without using a thirdparty php script as a snippet?
    Any advice would be appreciated. this is my first major project for work using modx.
    Also, please let me know if this is the wrong thread for this question.
      • 4041
      • 788 Posts
      I have not used this yet but it seems to fit your description, maybe give it a shot and see if its useful for you.

      http://www.dynamicdrive.com/dynamicindex17/virtualpagination.htm
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 5340
        • 1,624 Posts
        Here is my idea. Unfortunately I could not find the tools for it.

        logic
        - <p> will be the tag used to split your content.
        - create a snippet that will get the content as input.
        - let’s assume that you want to split the content after ~3000 characters.
        - 5-10% is probably html so let’s say 3300 is a better number

        php part
        - create a snippet that will take the content as input.
        - the snippet has to able to detect the first <p> after the 3300 characters. put those characters into content[0]. remove them from the initial content
        - repeat the step above for the rest of the content
        - You will end up having an array of content parts
        - Loop through those arrays and output them as
        <div class="page">$content[0]</div>

        javascript part
        - use any javascript framework you want to do the pagination
        This one my do the trick
        http://plugins.jquery.com/project/pagination

        Good Luck
          • 26968
          • 25 Posts
          Thank you Xpix and Breezer,

          So I took both of your advice and "kind of" got it working:
          http://www.ttmags.com/index.php?id=16#

          I downloaded the Dynamic Drive Javascript. I liked it because it keeps all the content on one page and just hides the rest in a div (or container of choice). If the user has javascript disabled, they just simply get the whole story. This appeals to me for two reasons: 1. You can control the style and elements with CSS and 2. Search Engine Optimization

          So all I had to do was figure out how to auomatically divide the content by character count and then wrap or format it in a div with class.
          After some time I found the php function string split:
          http://us.php.net/manual/en/function.str-split.php

          Then after hours and hours pouring over php tutorials and scripts tc etc.
          I came across this post at PHPfreaks.com by paul2463 that was extremely helpful:
          http://www.phpfreaks.com/forums/index.php?topic=139886

          I then changed my template to suit the js and linked the script.

          I fiddled with the php a bit and you can see the results above.

          Here is the snippet:

          <?php
          function splitString($string, $amount)
          {
          $start = 0;
          $end = $amount;
          while ($end < strlen($string)+$amount) //while $end is less than the length of $string + $amount to make sure it gets is all
          {
          $strArray[] = substr($string, $start, $amount);
          $start = $end;
          $end = $end + $amount;
          }
          return $strArray;
          }

          $test = $modx->documentObject[’content’];

          $return = splitString($test, 3600); //$return = the array of 3600 letter strings

          print_r("<div class=\"virtualpage hidepiece\">".($return[0])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[1])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[2])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[3])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[4])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[5])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[6])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[7])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[8])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[9])."</div>");
          print_r("<div class=\"virtualpage hidepiece\">".($return[10])."</div>");
          ?>

          So here are my current challenges. One, as you can see, I am using the print_r function to display sections of an array. Unfortunately, that creates empty divs that are then read by the javascript and translates into empty pages in the bottom navigation for the user. Are there any PHP wizards out there that can tweak this script so that it creates the right amount of divs?

          Also, it splits words. Is there a way to make it stop doing that?

          And finally, is there a way to automatically create a "continued from" on the second and following pages?

          I apologize profusely to any PHP or Javascript experts who may look at this ham hocked code and/or method in disgust.
          My only defense is that I am a newbie who knows his way around CSS and wants this to work.
          Any help would be greatly appreciated.
          s.


            • 21561
            • 16 Posts
            Not sure if this is the best way, in fact it probably isn’t but it does work.

            function splitString($string, $amount)
            {
                $start = 0;
                $end = $amount;
            
                while ($end < strlen($string)+$amount) //while $end is less than the length of $string + $amount to make sure it gets it all
                {
                    $chunk = substr($string, $start, $amount); //assign to variable instead of array
                    $chunk = strrev($chunk); // reverse string created
                    $new_chunk = preg_split("/[\s]+/", $chunk, 2); // split reversed string into 2 pieces on the first space.  So you are left with an array. Item [0] being the part of the last word and item [1] the remaining string
            
                    $page_text = strrev($new_chunk[1]); // reverse the remaining string back so it is readable
            
                    $offset = strlen($new_chunk[0]); // count the length of the part of the word left over so we can substract that from the $end.
            
                    $strArray[] = $page_text;  // assign to array
            
                    $start = $end - $offset;  // subtract that from the end
                    $end = $end + $amount;
                }
            return $strArray;
            }
            
            $test = $modx->documentObject['content'];
            
            $return = splitString($test, 3600); //$return = the array of 3600 letter strings
            
            $num = count($return); // get number of elements in array
            
            $output = '';
            
            // loop through the $return array and assign each array element to textchunk so we can print each chunk
            for($x=0;$x<$num;$x++) {
              $output .= ($x > 0 ) ?  '<div class="virtualpage hidepiece"><p><em>Continued From Page '.$x.'</em></p>'.$return[$x].'</div>' : '<div class="virtualpage hidepiece">'.$return[$x].'</div>';
            }
            echo $output;
            


            I just made an edit in the post and switched to a for loop from a foreach, so I haven’t tested that part let me know how that works out. A better way would be to put the html output into a chunk tpl and call that.
            but this will work as well.

            I’m curious if anyone else knows a better way to do the pagination stuff.

            Angelo
              • 26968
              • 25 Posts
              I apologize for this response coming so late.
              We launched the site using multiple templates for various lengths, (knowing approx. how long the stories were to begin with)
              but recently got a chance to test this code Angelo posted and it works well.
              It works REALLY well. No split words and it it automatically post the the continued page.
              nice.
              Thank you.