bobafifi has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for a script that will create something like the following navigation (for example, "page 2"):

<< Prev    1 | 2 | 3 | 4 | 5 | 6    Next >>

Anybody have something that can do this?

Many thanks in advance,

-Bob
bobafifi.com

Replies are listed 'Best First'.
Re: Navigation script?
by ikegami (Patriarch) on Mar 11, 2005 at 19:26 UTC

    Perl style:

    my $current_page_num = 2; my $num_pages = 6; if ($current_page_num > 1) { ...display link to page $current_page_num-1... } for my $page_num (1 .. $num_pages) { if ($page_num == $current_page_num) { ...display page number... } else { ...display link to page $page_num... } } if ($current_page_num < $num_pages) { ...display link to page $current_page_num+1... }

    Or template style:
    (Whitespace added for readability.)
    (The template system I use looks at the class for commands.)

    <a class="if::Selector.link_prev sub::href::Selector.link_prev" href="{page_link_next}" >&lt;&lt;Prev</a> <span class="page_selector_cell"> <span class="if::Selector.Page.is_current text::Selector.Page.page_num current-page" >{page num}</span> <a class="unless::Selector.Page.is_current sub::href::Selector.Page.link text::Selector.Page.page_num" href="{page_link}" >{page num}</a> </span> <a class="if::Selector.link_next sub::href::Selector.link_next" href="{page_link_next}" >Next&gt;gt;</a>

    given

    Selector.link_prev -> Link to prev page, if any. Selector.link_next -> Link to next page, if any. page_selector_cell -> Loops over all page nums, setting these: Selector.Page.is_current -> True if page_num is current page. Selector.Page.page_num -> Loop variable. Selector.Page.link -> Link to page page_num.
      Thanks very much for the speedy reply "ikegami"!

      Unfortuantely, I'm not sure at this stage how to integrate your code into my site (I'm not a programmer...), but will do some homework and see if I can figure it out.

      In any event - many thanks for the help, :-)

      -Bob
Re: Navigation script?
by b10m (Vicar) on Mar 11, 2005 at 19:50 UTC

    You don't give much information (like where is the data coming from?) Anyways, Data::Page might be what you need ;)

    --
    b10m

    All code is usually tested, but rarely trusted.
      Thanks for the info "b10m" and sorry for the vagueness about where the data is coming from...
      Here's the page (scroll down):


      Thanks again,

      -Bob

        I understand what you're trying to achieve, I'd just think it'd be easier if you specified where the data (that you'd like to split up) came from. There are numerous "page" modules on cpan. E.g. if you use Class::DBI, there are Class::DBI::Pager and Class::DBI::Plugin::Pager, but there's also DBIx::Pager... etc. etc.

        --
        b10m

        All code is usually tested, but rarely trusted.
Re: Navigation script?
by punkish (Priest) on Mar 11, 2005 at 20:44 UTC
    here is some pseudocode
    1. decide how many items to show per page, say 'n'
    2. determine the total number of items in the result set, say 'r'
    3. calc the total number of pages, say 'p', that will be created by the result set by using the above two (typically p = int(r/n)
    4. determine which page you are on right now based on the items being displayed
    5. create your navigation links with appropriate items to start from
    6. get more fancy once you've gotten the basic system working

    Even if someone has a precanned script for it, it is likely not going to fit your requirements because you have not provided any details on how you are generating your results, pages, etc.

    Nevertheless, it is quite easy to right, and would be a really nice exercise to learn Perl. Good luck.

    btw... nice music. "Seven Steps To Heaven" really cooks. Thanks.

    --
    when small people start casting long shadows, it is time to go to bed
Re: Navigation script?
by punkish (Priest) on Mar 11, 2005 at 20:44 UTC
    gack! itchy finger on refresh button strikes twice. Removed duplicate post.