Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 9th, 2008, 08:26 AM
Newbie
 
Join Date: Aug 2008
Posts: 2
Default 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.  

Last edited by Atli; August 10th, 2008 at 03:46 AM. Reason: Added [code] tags
Reply
  #2  
Old August 10th, 2008, 04:04 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,790
Default

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.
Reply
  #3  
Old August 12th, 2008, 12:31 AM
Newbie
 
Join Date: Aug 2008
Posts: 2
Default

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.

Quote:
Originally Posted by Atli
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.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles