php counter help

[php]
this php counter script adds 1 everytime the page is refreshed.
problem is that the page its on uses id’s to render, so the content is never the same depending on what id was loaded. how can I make it work based on the page’s url if that makes sense?

this is the code:

<?php include_once "../login/includes/dbconnect.php"; $sql = "SELECT counter FROM counter"; $result = mysql_query($sql) or die (mysql_error()); $rows = mysql_fetch_array($result); $page_views = $rows['counter']; if (empty($page_views)) { $page_views = "1"; mysql_query("INSERT INTO counter (counter)VALUES('1')"); } echo "Video has " .$page_views. " views."; $page_views++; $sql = "UPDATE counter SET counter = $page_views"; $result = mysql_query($sql) or die (mysql_error()); ?>

this is the table

CREATE TABLE counter (
counter int(11) NOT NULL default ‘0’
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
[/php]

First of all, your update command is not working.

Change From:

[php]
$sql = “UPDATE counter SET counter = $page_views”;
[/php]

To:

[php]
$sql = “UPDATE counter SET counter = ‘$page_views’”;
[/php]

Now, if I understand correctly, what you want is a counter for each video.

In this case, I hope that you have a database table that stores the video information; if you do, simply append a column onto the videos table called “counter” and since the page is rendered via IDs, you can do something like:

[php]
$id = $_GET[‘id’];
$videoViews = mysql_fetch_array(mysql_query(“SELECT counter FROM videos WHERE id=’$id’”));
$videoViews = $videoViews[‘counter’];
$videoViews++;
mysql_query(“UPDATE videos SET counter = ‘$videoViews’ WHERE id=’$id’”);
[/php]

If you do not have your videos loaded into a database already, then a different solution you could use instead would be to add another column to the counter table called ID, and at that point you would want to add an Auto Increment index to the counter table as well, just make sure you name your actual ID column and index column in a way you can tell them apart without having to check each time you are coding.

Then you could do something like this:

[php]
$id = $_GET[‘id’];
$videoViews = mysql_fetch_array(mysql_query(“SELECT counter FROM counter WHERE id=’$id’”));
$videoViews = $videoViews[‘counter’];
$videoViews++;
mysql_query(“UPDATE counter SET counter = ‘$videoViews’ WHERE id=’$id’”);
[/php]

You really need to be careful if you attempt to add onto the counter table without an AI index, just something I try to stay away from as a general rule.

Hope this helps.

Robert

Additionally, if you really wanted to shorten the code, you could go with these two sets instead:

[php]
$videoViews = mysql_fetch_array(mysql_query(“SELECT counter FROM videos WHERE id=’”.$_GET[‘id’]."’"));
$videoViews = $videoViews[‘counter’]++;
mysql_query(“UPDATE videos SET counter = ‘$videoViews’ WHERE id=’”.$_GET[‘id’]."’");
[/php]

[php]
$videoViews = mysql_fetch_array(mysql_query(“SELECT counter FROM counter WHERE id=’”.$_GET[‘id’]."’"));
$videoViews = $videoViews[‘counter’]++;
mysql_query(“UPDATE counter SET counter = ‘$videoViews’ WHERE id=’”.$_GET[‘id’]."’");
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service