[ic] incrementing array index in [perl]
Stefan Hornburg
interchange-users@icdevgroup.org
Thu Feb 6 02:50:01 2003
On Wed, 5 Feb 2003 22:59:38 -0800 (PST)
Ryan Grace <ryan_grace@yahoo.com> wrote:
> Hi All,
>
> I've been banging my head on this one for hours now
> and have searched the archives to no avail. I'm
> hoping someone knows this one.
>
> I've got a block of perl in a [perl] tag. I'm trying
> to figure out the current page to show, the next page
> to show, and the previous page to show, all for some
> navigation links furthur down the page. I'm assigning
> page numbers from the database to scratch variables to
> keep track of the next, current, and previous pages.
>
> In the following code, the pagemin and pagemax scratch
> variables are set correctly, so I know the array of
> values form the DB is good. The viewpage scratch
> variable is also set correctly. I've got some
> [scratch] tags further down the page that show me the
> values they're set to each time. However, the last
> two variables I can not get right. I can't get the
> nextpage and prevpage vars to set. I think it's a
> problem with the array indexes. If I set one of the
> scratches like: $Scratch->{prevpage} = $pages[2]; it
> works fine. But the way it is below does not work.
> Any ideas?
>
> ---------------------------
>
> [perl tables=products interpolate=1]
> my $db = $Db{products};
>
> $set = $db->query("SELECT page FROM products WHERE
> inactive != 1 AND collec\
> tion = '[cgi cn]' ORDER BY 1 ASC");
>
> @pages = ();
>
> foreach my $row (@$set) {
> push (@pages, $$row[0]) if ($$row[0] ne "");;
> }
>
> $Scratch->{pagemin} = $pages[0];
> $Scratch->{pagemax} = $pages[$#pages];
>
> if ($CGI->{page}) {
> $Scratch->{viewpage} = $CGI->{page};
> }
> else {
> $Scratch->{viewpage} = $pages[0];
> }
>
> my $incr = 0;
>
> foreach (sort @pages) {
> if ($_ = $Scratch->{viewpage}) {
> $prev = $Scratch->{incr} - 1;
> $next = $Scratch->{incr} + 1;
> $Scratch->{nextpage} = $pages[$next];
> $Scratch->{prevpage} = $pages[$prev];
> }
>
> $Scratch->{incr}++;
> }
>
> return;
> [/perl]
>
> Of course I've also tried the more obvious
> $Scratch->{prevpage} = $pages[$incr-1], which is my
> preferred method, but that doesn't work either.
Why do you use the sort in the second loop. That puzzles me.
I suppose you can do this all in one loop anyway.
Bye
Racke