in reply to Way to insert some ad code

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

Replies are listed 'Best First'.
Re^2: Way to insert some ad code
by Fletch (Bishop) on Jan 25, 2007 at 20:25 UTC

    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
Re^2: Way to insert some ad code
by htmanning (Friar) on Jan 25, 2007 at 21:22 UTC
    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!