By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
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

P: 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

Post your reply

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