Jump to content

[SOLVED] Simple page view counter not displaying result


MP145

Recommended Posts

Hi guys,

 

I have this simple page view counter in php that stores the page name into mysql table and displays the no of times the page has been viewed.

 

When the page is called the first time, i get this

Notice: Undefined variable: vpage in /page path/ on line 107

but the initial line of code for the if result==0 insert value works, but it's not displaying. Is it because of the while loop?

 

And when i refresh the page or call it the second time it displays the data.

 

How can i rectify this? and do let me know if there are better or simpler way to code this.  ;)

 

<?php
$pagename = $_SERVER["REQUEST_URI"];
        $parts = Explode('/', $pagename);
        $npage  = $parts[count($parts) - 1];

        mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error());
        $db5 = mysql_select_db("$dbname") or die("Unable to select db");
        $result=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error());

        if (mysql_num_rows($result)==0){
          mysql_query("insert into pageview (namapage,hits) values ('$npage','1')");
        }
        while($pinfo = mysql_fetch_array( $result )) {
          $count=$pinfo['hits'];
          $count=$count + 1;

          mysql_query ("update pageview set hits='$count' where namapage='$npage'");

          $vpage=$count; // will echo this on the designated area (Line 107)
          }
?>

 

Thanks.

ok, sorry. It was my mistake..it was the while loop after all.

 

Changed it to :

 

<?php
$pagename = $_SERVER["REQUEST_URI"];
        $parts = Explode('/', $pagename);
        $npage  = $parts[count($parts) - 1];

        mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error());
        $db5 = mysql_select_db("$dbname") or die("Unable to select db");
        $result=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error());

        if (mysql_num_rows($result)==0){
          mysql_query("insert into pageview (namapage,hits) values ('$npage','1')");
        }
        $pinfo = mysql_fetch_array( $result );
        $count=$pinfo['hits'];
        $count=$count + 1;
        mysql_query ("update pageview set hits='$count' where namapage='$npage'");

        $vpage=$count;
?>

 

;D

What is happening is that you query your database to find out if the page exists, and if it doesn't you add it. All well and good.

 

However then you go on to use the results of the initial query, which returned no records. It doesn't matter that you have subsequently added a record, as the initial result set is unaffected.

 

You could requery the database, but you already know what it will say.

 

So, inside your loop where you add the record, just set $count to equal 1.

 

Then, only run the second query if the insert part did not run, so use an else clause on your initial if statement. Inside the else clause, get your $count value, increment it, and run your update statement.

 

You can skip the while loop altogether I think, as you're only expecting one row to be returned, and at that stage you will already know that there is at least one row.

 

In pseudo code:

if (count of rows = 0) {

  insert a row

  set $count = 1

} else {

  get $count

  $count++

  update database with new count value

}

use $count

 

I'm not sure why the vpage thing is erroring. I don't understand exactly what you're saying there.

 

 

 

Thanks SammyP for the reply. I made some changes and it's all good.  ;D

 

Thanks for pointing out the

 

What is happening is that you query your database to find out if the page exists, and if it doesn't you add it. All well and good.

 

However then you go on to use the results of the initial query, which returned no records. It doesn't matter that you have subsequently added a record, as the initial result set is unaffected.

 

<?php
$pagename = $_SERVER["REQUEST_URI"];
        $parts = Explode('/', $pagename);
        $npage  = $parts[count($parts) - 1];

        mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error());
        $db5 = mysql_select_db("$dbname") or die("Unable to select db");
        $result=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error());

        if (mysql_num_rows($result)==0){
          mysql_query("insert into pageview (namapage,hits) values ('$npage','1')");
        }
        $result1=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error());
        $pinfo = mysql_fetch_array( $result1 );
        $count=$pinfo['hits'];
        $count=$count + 1;
        mysql_query ("update pageview set hits=$count where namapage='$npage'");

        $vpage=$count;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.