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

Can you please tell me if I'm doing this part wrong. This seems to be the problem area, so I'll only show this aspect of the code, as it's a pretty long system I'm building.

First let me say this, I've been working on this since about 6am this morning, almost non stop, so I'm very tired, and may not get my point across like I normally would when I'm well rested(not much lately;o))

Second, let me explain what I'm doing. I am writing my own Affiliate program into our Website. I'm using ONE perl script, for the entire system, every aspect of our site. Kind of like this site does, but just a different style of programming. This affiliate program is going to have a way of tracking how the affiliates are doing with each different banner we have created for them.

When they click on "view stats", it loads a page with Daily Stats the default, and gives them a menu of several other choices. The Daily, shows every banner, and under the banner it shows impressions, clicks, sales and a Click Through Rate(clicks divided by impressions).

That is what I'm trying to do...
Here it is:
$sth = $dbh->prepare (qq{ SELECT * FROM table_name ORDER BY bid }) +; $sth->execute(); while ($row = $sth->fetchrow_hashref()) { $tracked_impressions = 0; $tracked_clicks = 0; $tracked_sales = 0; $sth1 = $dbh->prepare (qq{ SELECT * FROM aff_trac WHERE id = $ +aff_id AND bid = $row->{bid} AND t BETWEEN "$database_query_date_s" A +ND "$database_query_date_e" }); $sth1->execute(); while ($aff_row = $sth1->fetchrow_hashref()) { if ($aff_row->{utype} =~ /^i$/) { $tracked_impressions++; } elsif ($aff_row->{utype} =~ /^h$/) { $tracked_clicks++; } elsif($aff_row->{utype} =~ /^s$/) { $tracked_sales++; } } $html_content .= qq~ <tr valign="top"> <td align="center" class="header_cell"> Advertisement ID: $row->{bid} </td /> </tr /> <tr valign="middle"> <td align="center" class="non_label_cell"> <img src="$surl?pg=$in{pg}&do=$in{do}&action=show_imag +e&imgid=$row->{bid}" border="0" alt="Program ID $row->{bid}" /> </td /> </tr /> <tr valign="top"> <td align="left" class="non_label_cell"> <table class="non_label_cell" align="center" border="1 +" cellpadding="2" cellspacing="2" bordercolor="$Page_table_border_col +or" width="100%"> <tr valign="top"> <td align="left" class="label_cell"> <b>Impressions</b>: </td /> <td align="left" class="non_label_cell"> <b>$tracked_impressions</b> </td /> <td align="left" class="label_cell"> <b>Clicks</b>: </td /> <td align="left" class="non_label_cell"> <b>$tracked_clicks</b> </td /> <td align="left" class="label_cell"> <b>Click Through Rate</b>: </td /> <td align="left" class="non_label_cell"> <b>~; if ($tracked_click || $tracked_impressions) { $html_content .= ($tracked_click*1) / ($tracked_impressions*1) +; } else { $html_content .= "1000"; # Temp so I can see when it did NOT w +ork... or there was 0 to track... } $html_content .= qq~%</b> </td /> <td align="left" class="label_cell"> <b>Sales</b>: </td /> <td align="left" class="non_label_cell"> <b>$tracked_sales</b>: </td /> </tr /> </table /> </td /> </tr />\n~; } $dbh->disconnect();
Anyways that is the part that does not seem to be working. It actually works ok, but it's only showing 1 click and 1 impression when I've made sure there are at least 2 or 3 of each. Plus it either shows 1000%(did not work) or 0%.

Do you see something WRONG with that code?

thx,
Richard

Replies are listed 'Best First'.
Re: Troubleshooting help...
by dws (Chancellor) on Feb 15, 2003 at 07:54 UTC
    Do you see something WRONG with that code?

    I consider that not using query parameters is wrong, but that probably has nothing to do with the problem you're seeing.

    The only way you should see "1000%" is if you fetch a row whose "utype" field doesn't start with "i" or "h". How certain are you of your data? Are you sure that some fields don't start with "I" or "H". Or are some "s" records sneaking in?

    A quick-and-dirty way to get some visibility onto the problem is to add

    $html_content .= "<!-- " . $aff_row->{utype} . " -->";
    after you fetch the row. Then, you can view the HTML source to see what you really got.

    Dumping diagnostic info into HTML comments is a trick you can get a log of leverage from.

    Update: You're generating bogus HTML, but that's a separate issue. Emit </td>, not </td />.

      Thank you. I'm very sure of the i,h, and s. I know that, because it's a enum("i","h","s") i=impression, h=hit(click), s=sale

      </td /> Is not that the way CGI.pm prints a table? I once asked why, and they said it was for DHTML, or XML or something like that. That is why I do that.

      I dunno ;o)

      Anyways, I made a mistake with what is wrong. It is adding them correctly, I forgot this was on the daily report, so the ones I added, went in with a timestamp different from the days I was looking at, so I changed days to today, and they are added up correctly, however, I get 0% from the division.

      So, I guess that is the only problem now.

      Should I get rid of the </td /> and just use the regular code?

      thx,
      Richard
        I changed days to today, and they are added up correctly, however, I get 0% from the division.

        The way your logic is set up, only one of $tracked_click or $tracked_impressions will be 1 when

        ($tracked_click*1) / ($tracked_impressions*1)
        is executed. The other will be zero. You're either going to divide zero by one, giving zero, or you're going to divide by zero. Is this really what you want to do?

        Should I get rid of the </td /> and just use the regular code?

        You should use </td>. The "trailing slash" form of tags is reserved for those tags that are not paired, like <br/>.