Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Looping and counting

by htmanning (Pilgrim)
on Apr 04, 2012 at 00:46 UTC ( #963339=perlquestion: print w/replies, xml ) Need Help??
htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I'm grabbing latest headlines from a database and print them on 2 separate pages. I go through and grab the lastest 10, but then I want to grab the next 40 for page 2.
$filebase = "news"; $num_records = 10; $SQL = "SELECT * FROM $content_table WHERE contenttype = 'video' O +RDER BY dateadded DESC, ID DESC"; for ($n=1; $n<=$num_records; $n++) { print outfile1 " latest headlines"; } $filebase = "morenews"; $num_records = 40; for ($n=1; $n<=$num_records; $n++) { print outfile2 "latest headlines"; }
The print lines above are fake but you get the idea. The problem is the first page gets 10 headlines and the second page gets 40, but the 40 includes the first 10. I'd like to grab 10 headlines then the NEXT 40. Thanks.

Replies are listed 'Best First'.
Re: Looping and counting
by Riales (Hermit) on Apr 04, 2012 at 01:06 UTC

    I suppose you're fetching a row at a time within your for loops?

    It might be easier fetch the first 50 headlines (by putting a LIMIT in the SQL query) and grabbing all of them at once and putting that into an array. Then, you can just output the first 10 into the first file and the next 40 into the second file.

    Alternatively, you can use something like your existing for loop like so:

    for ($n = 1; $n < 50; $n++) { # Fetch your row however you're doing it and put into $headline if ($n < 10) { print outfile1 $headline; } else { print outfile2 $headline; } }
      Thanks. grabbing only the first 50 is how I normally do it, but I didn't write this script so I'm a bit lost. Here's more code to see if it makes it clearer. This is the skeleton as I've ripped out a bunch of variable setting.
      $filebase = "news"; $num_records = 10; $SQL = "SELECT * FROM $content_table WHERE contenttype = 'video' ORDER + BY dateadded DESC, ID DESC"; for ($n=1; $n<=$num_records; $n++) { $pointer = $sth->fetchrow_hashref; $title = $pointer->{'title'}; $main_list2 = $main_list2 . "$title"; print outfile1 "$main_list2"; } $filebase = "morenews"; $num_records = 40; for ($n=1; $n<=$num_records; $n++) { $pointer = $sth->fetchrow_hashref; $title = $pointer->{'title'}; $main_list2 = $main_list2 . "$title"; print outfile2 "$main_list2"; }
      As you can see, it loops through and adds another $title to $main_list2. No matter what if statement I put before $main)list2, it doesn't work. I had it like you suggested but no dice. What I'm ending up with is the first 10 titles on page 1, then the first 10 titles, plus the first 40 titles on page 2. Thanks.

        Put this between your two for loops:

        $main_list2 = '';

        But seeing as you have have a $main_list2, why don't you use $main_list1 in the first for loop instead?

        Also, probably want to have a "\n" between your titles:

        $main_list2 .= "$title\n";
Re: Looping and counting
by Anonymous Monk on Apr 04, 2012 at 12:56 UTC
    MySQL, for example, has a limit x,y clause for the select statement, and it is more-or-less designed for handling this.   Your result set will contain only the specified range of rows (if they exist).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963339]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-07-22 11:11 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (454 votes). Check out past polls.