Jump to content

Recommended Posts

Hi, I have been working on a page view counter. Now I haver researched google five times already and can't find a script that calculates a specific page's hits (not the whole site's page htis). So I made my own. Here is the code. I have tried it and it doesn't seem to work. Can you please help me?

 

<?php

// Page ID
$page="1";
?>

<?php

require("db/connect.php");
// Already connected to mysql
$query = "SELECT * FROM `table` WHERE id='$page';
$result = mysql_query($query);

$cnt = $row["count"];
$cnt++;


mysql_query( "UPDATE table SET  count = '$cnt' WHERE id= $page";");

// echo the number of htis
echo $cnt;

?>

Link to comment
https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/
Share on other sites

try:

<?php

// Page ID
$page="1";
?>

<?php

require("db/connect.php");
// Already connected to mysql
$query = "SELECT * FROM table WHERE id = $page";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
  	$cnt = $row["count"];
$cnt++;
}


mysql_query( "UPDATE table SET  count = $cnt WHERE id = $page");

// echo the number of htis
echo $cnt;

?>

I tried it and it didn't work.

 

Here is the code I tried:

 

<?php

// Page ID
$page="1";

require("connect.php");
// Already connected to mysql
$query = "SELECT * FROM view WHERE id = $page";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
  	$cnt = $row["count"];
$cnt++;
}


mysql_query( "UPDATE view SET count = $cnt WHERE id = $page");

// echo the number of hits
echo $cnt;
echo Hello;

?>

Thanks for all your help so far. I greatly appreciate it.

 

Here's the sql code I used for mysql:

 

CREATE TABLE `view` (
  `ID` bigint(20) NOT NULL auto_increment,
  `count` varchar(6) default NULL,
  PRIMARY KEY  (`ID`),
  KEY `count` (`count`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

#
# Dumping data for table `hitcounter`
#

 

I'm not sure if the table is structured properly.

<?php

 

// Page ID

$page="1";

 

require("connect.php");

// Already connected to mysql

$query = "SELECT `count` FROM view WHERE `id` = '" . $page . "'";

$result = mysql_query($query)

or die("ERROR: " . mysql_error());

 

while ($row = mysql_fetch_array($result)) {

  $cnt = $row["count"];

$cnt++;

}

 

 

mysql_query( "UPDATE `view` SET `count` = '" . $cnt . "' WHERE `id` = '" . $page . "'")

or die("ERROR: " . mysql_error());

 

// echo the number of hits

echo $cnt;

?>

Try this:

 

Drop the old table and use this one:

CREATE TABLE `view` (
  `page_id` int(11) NOT NULL default '0',
  `page_count` int(11) default NULL,
  PRIMARY KEY  (`page_id`)
) ENGINE=MyISAM;

 

And use this as your php:

<?php

// Page ID
$page = 1;

require("connect.php");

$result = mysql_query("SELECT page_count FROM view WHERE page_id = $page");
$t_count = mysql_num_rows($result);

if($t_count > 0){
$cnt = mysql_result($result, 0, 'page_count');
$cnt++;
mysql_query("UPDATE view SET page_count = $cnt WHERE page_id = $page");
}else{
mysql_query("INSERT INTO view ( page_id , page_count ) VALUES ($page, '1')");
$cnt = 1;
}

// echo the number of htis
echo $cnt;

?>

 

It worked for me :)

You can use something like this:

 

$result = mysql_query("SELECT page_count FROM view WHERE page_id = $page");
$t_count = mysql_num_rows($result);

if($t_count > 0){
$cnt = mysql_result($result, 0, 'page_count');
echo "Threre are $cnt page views.";
}else{
echo "There are no page views.";
}

I'll try that one out too. But the one before that you gave me worked perfectly! Thanks soo much. Thanks to everyone else to help me out on getting it to work. You don't know how relieved I am that it finally started working. You can check out the test out at:

 

http://www.TheFilmGod.com/test/scripty

 

Now I'm going to add it into my currently running pages on my site. Check out TheFilmGod.com out soon to it action (underneath its movie it will have total views: (Number of views) - Thanks to all you)! Again, without phpfreaks and this forum I don't know what I would do.

 

Is there any way to recommend those who helped me?

I'm glad we can help you out. We do not have a rating system anymore. We started with one when we converted over to this board (SMF) but some people abused it a little too much so the mods took it off. But most people in here know who's good and who's not. You'll know that too when you are around a little bit longer.

 

Whenever you need something answered just post and you shall receive :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.