Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Making an automatic counter adder for all webpages

by Nik
on Dec 22, 2007 at 10:34 UTC ( #658635=perlquestion: print w/ replies, xml ) Need Help??
Nik has asked for the wisdom of the Perl Monks concerning the following question:

The following code connect to my db, graps the counter field value which holds an integer that represents the total amount of hits in index.pl
my $db = ( $ENV{'SERVER_NAME'} !~ /varsa/ ) ? DBI->connect('DBI:mysql:pneumatika;localhost', 'root', '***** +**', {RaiseError=>1}) : DBI->connect('DBI:mysql:18177_pneumatika;fdb1.runhosting.com' +, '18177_pneumatika', '*****', {RaiseError=>1}); $select = $db->prepare( "SELECT counter, host FROM guestlog" ); $select->execute; my $pagecounter; while( $row = $select->fetchrow_hashref ) { $pagecounter += $row->{counter} unless ($row->{host} eq "varsa); }
What must be add to this code that is able to save each unique webpage counter at a specific mysql table field adding 1 every time and also do this for every new page i create because the number of the pages aren't fixed plz post it here.

The code also has to be able to modify mysql table 'guestlog' and add a new table field every time a new page is loaded that doesnt have a corresponding counter by means of table field named as the page itself holding an integer value.

For example if a user opens index.html then connect to mysql and to table counter, find index filed add 1 to it!

if a page named hello.html its loades try to add 1 to its table filed too if it does not exist, alter the table structure by adding 1 more field with the same name of the webpage and then add 1 too.

For example it the name is hello.html then the corresponing table filed has to be 'hello' with an integer value of one.

That way one can use the code in every new webpage he creates without dropping and creating a bigger mysql table.

is this possible?

Comment on Making an automatic counter adder for all webpages
Download Code
Re: Making an automatic counter adder for all webpages
by Fletch (Chancellor) on Dec 22, 2007 at 15:04 UTC

    Altering your table structure on the fly like that is just inane. You want to have a table with a row for each page with the counter on that.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Well, that is what i would have done if i knew aprioari how many pages i would create. Then i would count them and create a mysql table with as many fields as the amount of total web pages.

      But what happens if you don't know how many pages you are about to make, or you do but then you decide to make some more?

      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...Can you help me make script?

        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://658635]
Approved by polettix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-08-22 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls