473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

creating a counter for 'hits/listens' for each mp3 file.

I am designing a site that is regulary updated with new podcasts. I would like to be able to have a counter next to each podcast showing how many hits/listens/plays/views each particular podcast has gotten.

eg: podcast001.mp3 plays: 12

I have set up a column in a mysql database for each podcast called 'counter'.

I then tried to make a php-javascript hybrid code with functions on onClick that went into and added one to the counter column of the appropriate row in the mysql database. This failed miserably. Turns out you cannot mix these things, one being client based, the other browser based.

Is this the best way to approach the problem? Am I missing something really obvious? Is it possible to use awstats somehow? Is there a sneaky way of getting php code to act like java's onClick? I searched around for tutorials and examples but I haven't come across anything that helps.

The site is www.hockeyrant.com if that helps.

Any help on an idea of how to approach this would be greatly appreciated.

Here is the function that browser's really don't like:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3.  
  4. function addCounter() {
  5.  
  6. <?php
  7.  
  8. [connect to appropriate database with appropriate passwords]
  9.  
  10. $query = "select counter from hockeyrant where (id='$id') limit 1";
  11. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  12.  
  13. $numrows=mysql_numrows($result);
  14.  
  15. if ($numrows == 0) {
  16.    exit;
  17. }  
  18. else {
  19.   $counter = mysql_result($result, 0, counter);
  20.   $counter = $counter + 1;
  21.   $query3="update hockeyrant set counter = '$counter' where id = $id";
  22.   $result3=mysql_query($query3) or die('Query failed: ' . mysql_error());
  23.   exit;
  24. }
  25.  
  26. mysql_free_result($result);
  27. mysql_close($db);
  28.  
  29. ?>
  30.  
  31. }
  32.  
  33. //-->
  34.  
  35. </script>
  36.  
Aug 9 '08 #1
2 2836
Atli
5,058 Expert 4TB
Hi. Welcome to Bytes!

As you say, mixing JavaScript and PHP in the way you do is not possible, JavaScript being executed on the client browser while PHP is executed on the server.

From PHP's point of view, there is no difference between HTML or JavaScript, or even Images or binary data for that matter. It's all just output.

The best way to have JavaScript call PHP directly would be to use AJAX.
An AJAX request would be able to call a PHP page on the server, get it's response, and print it directly to the page, without ever having to refresh or redirect.

Consider this example.
Expand|Select|Wrap|Line Numbers
  1. # File: rand.php
  2. <?php
  3. echo mt_rand(0, 100);
  4. ?>
  5.  
Expand|Select|Wrap|Line Numbers
  1. # File index.html
  2. <html>
  3. <head>
  4.   <script type="text/javascript">
  5.     function GetRandom() {
  6.          // Create and initialize xmlHttpRequest
  7.       var request = null;
  8.  
  9.       if (window.XMLHttpRequest) { // Mozilla/Safari
  10.           request = new XMLHttpRequest();
  11.       }
  12.       else if (window.ActiveXObject) { // IE
  13.           request = new ActiveXObject("Microsoft.XMLHTTP");
  14.       }
  15.  
  16.       // Define how to handle the response
  17.       request.onreadystatechange = function() {
  18.             // Readystate 4 means it's been received
  19.           if (request.readyState == 4) {
  20.               document.getElementById('outputBox').innerHTML = request.responseText;    
  21.           }
  22.       }
  23.  
  24.       // Send the request
  25.       request.open("GET", "rand.php", true);
  26.       request.send(null);
  27.     }
  28.   </script>
  29.  
  30. </head>
  31.  
  32. <body>
  33.   <div id="outputBox">Nothing fetched yet</div><br />
  34.   <button onclick="javascript: GetRandom();">Get randon number</button>
  35. </body>
  36. </html>
  37.  
This just has the JavaScript code send a AJAX request for "rand.php", and prints the result of that into the <div>.

It's pretty useless as such, but you get the picture.
Aug 10 '08 #2
Awesome. Thanks. Looked into Ajax but was thoroughly confused by the tutorials and examples. This one makes sense to me. Will play with it to make it work for my site.
Thanks again.

Hi. Welcome to Bytes!

As you say, mixing JavaScript and PHP in the way you do is not possible, JavaScript being executed on the client browser while PHP is executed on the server.

From PHP's point of view, there is no difference between HTML or JavaScript, or even Images or binary data for that matter. It's all just output.

The best way to have JavaScript call PHP directly would be to use AJAX.
An AJAX request would be able to call a PHP page on the server, get it's response, and print it directly to the page, without ever having to refresh or redirect.

Consider this example.
Expand|Select|Wrap|Line Numbers
  1. # File: rand.php
  2. <?php
  3. echo mt_rand(0, 100);
  4. ?>
  5.  
Expand|Select|Wrap|Line Numbers
  1. # File index.html
  2. <html>
  3. <head>
  4.   <script type="text/javascript">
  5.     function GetRandom() {
  6.          // Create and initialize xmlHttpRequest
  7.       var request = null;
  8.  
  9.       if (window.XMLHttpRequest) { // Mozilla/Safari
  10.           request = new XMLHttpRequest();
  11.       }
  12.       else if (window.ActiveXObject) { // IE
  13.           request = new ActiveXObject("Microsoft.XMLHTTP");
  14.       }
  15.  
  16.       // Define how to handle the response
  17.       request.onreadystatechange = function() {
  18.             // Readystate 4 means it's been received
  19.           if (request.readyState == 4) {
  20.               document.getElementById('outputBox').innerHTML = request.responseText;    
  21.           }
  22.       }
  23.  
  24.       // Send the request
  25.       request.open("GET", "rand.php", true);
  26.       request.send(null);
  27.     }
  28.   </script>
  29.  
  30. </head>
  31.  
  32. <body>
  33.   <div id="outputBox">Nothing fetched yet</div><br />
  34.   <button onclick="javascript: GetRandom();">Get randon number</button>
  35. </body>
  36. </html>
  37.  
This just has the JavaScript code send a AJAX request for "rand.php", and prints the result of that into the <div>.

It's pretty useless as such, but you get the picture.
Aug 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: [XaToA] | last post by:
Hello. I am trying to put one counter in my php with the next code: <?php $hitfile = "/admin/contador.txt"; if (!file_exists($hitfile)) { $file = fopen($hitfile,"r+"); $hits =...
0
by: deko | last post by:
I use this script to keep count of visits to my site - it's on every page in the site. <?php $file = '/home/post/public_html/viscount'; if ($counter = @file ($file)) { $line = each($counter);...
8
by: alfa_beveren | last post by:
How to make a simple Hit Counter ? I am old VB Programmer, trying to learn ASP... alfa_beveren@hotmail.com
8
by: Jim in Arizona | last post by:
I've been looking for a counting script to count the number of hits to my HTM and asp web pages on my company's internal website. So far, everything I've seen only works on asp pages. Where would I...
7
by: JellyON | last post by:
Hi. Is there a way to delay a call to a page counter (ie. call to a server script from an IMG tag) for the purpose to not lock the page loading awaiting counter be displayed. Maybe a...
4
by: SH | last post by:
I wish to create a program (really a Windows Service) that sits and waits for a client PC to communicate with it, but I can't come up with a good method of doing so. I want to have a service...
2
by: tehgreatmg | last post by:
What I have is this php code saved as a .php file <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits ++; $fp = fopen($count_my_page , "w"); fputs($fp , "$hits");...
11
by: tony19760619 | last post by:
To make this work on a single page you will need to create/modify 4 files. So here goes... Create a file called counter.php and put following code in it: <?php //Counter $count_my_page =...
30
by: Paul W Smith | last post by:
I have written a hit counter, which I believe counts the times my page is hit by a user for each unique session by the user. The relevant part of my code is below: If...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.