Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Making an automatic counter adder for all webpages

by polettix (Vicar)
on Dec 22, 2007 at 16:15 UTC ( [id://658659]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Making an automatic counter adder for all webpages
in thread Making an automatic counter adder for all webpages

Don't you think there's a contradiction here?
I beleive the idea of altering the table structure on the fly is great, so you dont have to mess with mysql all the time, altering tables, or dropping them and create new ones...
(Bold is mine) You either think that altering the table is a great idea, or that it's a complete mess. And you're right in the second case.

What Fletch meant<stroke>, I think,</stroke> is that you should have a table with two columns: one for the name of the page, and the other for the associated counter. Something like this:

+----------+---------+ | pagename | counter | +----------+---------+ | home | 123 | | page_1 | 22 | | page_2 | 16 | | credits | 2 | | policy | 1 | +----------+---------+
This way, each new page is just a new record in this table, which you can do with a simple INSERT.

Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Io ho capito... ma tu che hai detto?

Replies are listed 'Best First'.
Re^4: Making an automatic counter adder for all webpages
by Nik (Initiate) on Dec 22, 2007 at 21:17 UTC
    Yes indeed this is very clear and better.

    But in order to increase a counter for a specific page or create it first and then populate it, the script must know which webpage just loaded for example page_5.html and extract just the filename without the dot and extension.

    How to do this? The best i can do is this:

    $pagename = "something that tells me what si the name of the currnt sc +ript running, just its filename"; if ( $pagename is an already actual field inside table 'counter' then +) { $select = $db->prepare( "SELECT pagename, pagecounter FROM counter +s" ); $select->execute; } else { $select = $db->prepare( "INSERT INTO counters (pagename, pagecount +er) VALUES (?, ?)" ); $select->execute( $pagename, 1 ); my $pagecounter; while( $row = $select->fetchrow_hashref ) { $pagecounter += $row->{pagecounter} unless ( $row->{host} eq "&#92 +5;&#943;&#954;&#959;&#962;" ); }
      Nothing prevents you from using the full file name in the "pagename" column :)

      Regarding how to get the page name, you provided little insight about how a page name is "generated"; in any case, whatever your approach (the one suggested by Fletch, or the ALTER TABLE one), you have to find some way to decide which counter you want to increase. Which means that this is not a problem tied to this specific solution. If the page name is tied to the current script, anyway, you might benefit from the contents of either $0 (perlvar) or of __FILE__ (perldata), whatever applies best to your case.

      I'd make the "pagename" column a primary key for the table, and just do something like this:

      # Unconditionally try to create a new record. eval { $db->do('INSERT INTO counters (pagename, pagecounter) VALUES ($, 0) +', undef, $pagename); }; # Unconditionally increase the counter $db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename);
      If the record for the given page does not exist, it's created and the counter is initialised to 0. If it already exists, the INSERT fails due to the fact that the pagename column is a primary key, and you can't have two records with the same primary key. In either case, after the eval you're sufficiently confident* that a record for the given page actually exists in the table.

      After this, you increase the counter. If the record was just created, it is correctly bumped to 1. In the other case, it is simply increased, which is what you want.

      No checks (the RDBMS does these for you), no race conditions... maybe a little dirt? Acceptable, IMHO.

      * "sufficiently confident" means that the INSERT could fail for other reasons apart the field being there. If all the rest is working properly, than you can be confident that after the eval you'll have something to increase in that table.

      Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Io ho capito... ma tu che hai detto?
        I can't beleive it can be done in a so few lines!

        I though i had to connect to the database table and check if the specific field is present at the table 'counters' but with the eval you give i dont have too....

        Unfortunately i didn't understand how the eval() is working here and what the 'undef, $pagename)' is doing after the eval(). Inever used eval before!

        Eval is trying to evaluate the statement closed within its parenthesis? Can you explain a bit more if its possible?

        The $ stands for the name of the current webpage thst is running? is it with or witout the '.html'? Also because you ask we dont need to know how the current webpage loaded just its name.

        in your second statemtn alsoin, the increament $db->do(), you say where pagename = ?, whats the value of ?

        $db->do( 'CREATE TABLE counters (pagename text primary key, pagecoun +ter int default 0)' ); eval { $db->do('INSERT INTO counters (pagename, pagecounter) VALUES ($, 0) +', undef, $pagename); }; $db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename);
        Also i forgot to tell you that i also need to print the current webpage's counter to the output after the increment so i need to store it in a variable called $pagecounter.
        print table( {class=>'info'}, Tr( td( {class=>'lime'}, a( {href=>'/cgi-bin/show.pl?name=s +howlog'}, h1( {class=>'lime'}, $host )))), Tr( td( {class=>'yellow'}, $display_date)), Tr( td( {class=>'cyan'}, $pagecounter )) );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://658659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-24 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found