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

Here is some code I use to generate the latest 10 headlines from my database, and write them to a single txt file. I'm trying to create a way to print some ad code after 3 headlines. I either need an auto incrementing variable within a while statement so I can print to separate text files, OR better yet, a way to insert code only after the third headline keeping everything in a single txt file.
$SQL = "SELECT * FROM $database_table order by date DESC, ID DESC limi +t 10"; $sth = $dbh->prepare($SQL); $sth->execute; open(TXTFILE, ">$rootpath$categoryDir$frontPage"); while ($pointer = $sth->fetchrow_hashref) { $fullpath = $pointer->{'fullfilepath'}; $name = $pointer->{'filename'}; $thumbnail = $pointer->{'thumbnail'}; $image = $pointer->{'image'}; $imagewidth = $pointer->{'imagewidth'}; $headlinelink = $pointer->{'headlinelink'}; print TXTFILE <<TEXT; <a href=$url NOUNDERLINE id="headline"><font size="+1" face=arial>$tit +le</font></a><br> <font size="1">$category - $month $day, $year $catsiteshow</font><br> TEXT }
That code puts the last 10 headlines in a single txt file. Thanks.

Replies are listed 'Best First'.
Re: Way to insert some ad code
by eric256 (Parson) on Jan 25, 2007 at 20:08 UTC

    Do exactly what you are saying, but in code.

    my $count = 0; #temporary variable to count with $SQL = "SELECT * FROM $database_table order by date DESC, ID DESC limi +t 10"; $sth = $dbh->prepare($SQL); $sth->execute; open(TXTFILE, ">$rootpath$categoryDir$frontPage"); while ($pointer = $sth->fetchrow_hashref) { $fullpath = $pointer->{'fullfilepath'}; $name = $pointer->{'filename'}; $thumbnail = $pointer->{'thumbnail'}; $image = $pointer->{'image'}; $imagewidth = $pointer->{'imagewidth'}; $headlinelink = $pointer->{'headlinelink'}; $count++; # umm...err...count if ($count > 2) { # if we counted to 3 then do something print TXTFILE "YOU AD HERE"; # and now we start counting agian. $count = 0; } print TXTFILE <<TEXT; <a href=$url NOUNDERLINE id="headline"><font size="+1" face=arial>$tit +le</font></a><br> <font size="1">$category - $month $day, $year $catsiteshow</font><br> TEXT }

    ___________
    Eric Hodges

      Using the modulo (%) operator is probably a bit more idiomatic way to do this rather than explicitly resetting the counter.

      my $count = 0; while( $count < 30 ) { print "count $count\n" if ++$count % 3 == 0; }

      Update: And a gripe for the OP: your open statement would be better written:

      my $outfile = "$rootpath$categoryDir$frontPage"; open(TXTFILE, ">", $outfile ) or die "Can't open output file '$outfile': $!\n";

      You'll be thanking yourself for providing a useful error message n months down the road when something changes and you're one step closer to fixing it rather than trying to chase down a mysterious "write on a closed filehandle" error.

        Definitely, but if your new to programming then the other method would be more intuitive even while being less idiomatic.


        ___________
        Eric Hodges
      Thanks so much!! I used this code but then it was printing the ads every 3 headlines. I changed this...
      $count++; # umm...err...count if ($count > 2) {
      to this...
      $count++; # umm...err...count if ($count == 2) {
      Then I reset the counter afterwards from

      $count = 0 to $count = 4.

      Thanks a bunch!

Re: Way to insert some ad code
by ikegami (Patriarch) on Jan 25, 2007 at 20:42 UTC
    The answer hasn't changed since the last time you asked.