Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd go for a jQuery event listener on the download link, and have it all done server side through PHP. It may not be foolproof, but it doesn't impact on the user as much.</p> <p>If you wanted to do something like this, then you first need to group your buttons in a way that is accessible through jQuery; there are other ways to select multiple elements in jQuery but the easiest is by making them all members of a specific class.</p> <p>You can then loop through the class using the .each() function. So for initialisation of the page; i.e. getting all the page content working; you need to loop through every button and make a post request to the PHP script; all that needs to be sent to the PHP script is the id of the download. This is done in Part 1 of the code below.</p> <p>Part 2 is setting up the event listener for when a download occurs. Once again, by just putting an event listener on the whole class we can detect a click on any of them; and then identify which button it came from by accessing it's id. (Note: For simplicity's sake; make the id in the markup the same as the id in the database)</p> <p>This then makes another post request to the php script; but supplies an additional parameter - "download". In reality, the value that corresponds to download doesn't actually matter; because this case is simple enough that the user is either downloading it or not. So in the PHP segment of the code (later below) we don't even check it's value; <em>we just check that it exists</em>.</p> <pre><code>$(document).ready(function(){ /* [[PART 1]]: loop through every element in the .button class*/ $(".button").each(function (e) { /* get it's ID (which is equal to the download id stored in the db */ var id = e.target.id; /* Post the download id to ranking.php; then echo the returned value to an ** element with the id of 'count&lt;num&gt;'; where num is the download id. */ $.post( 'ranking.php', { 'id' : id }, function(data){ $('#count' + id).html(data); }); }); /* Part 2: When button is clicked, post the download data to "ranking.php" */ $('.button').click(function(event){ var id = e.target.id; //get the id of the button $.post( 'ranking.php', { 'id' : event.target.id, 'download' : 'YES' }, //set 'download' function(){ $('#count' + id).html(data) }); //echo new value to html }); }); </code></pre> <p>I suggest jQuery as it provides enough syntactical sugar to make things pretty easy. Additionally, by using jQuery - you're opening up the doorway to lots of possibilities in the future if you choose.</p> <p>Server Side is where the PHP takes over. All this needs to do in reality is to check whether 'download' is set, and echo back the number of downloads. (Obviously incrementing the number with the database if the 'download' parameter is set) </p> <p>This could be achieved with something like...</p> <pre><code>&lt;?php mysql_connect(localhost,$user,$password); @mysql_select_db($database) or die( "Unable to select database"); if(! isset($_POST['id']) { //die error! } /* if $_POST['download'] isn't set, then just get the current download number and echo it back to the browser */ if(! isset($_POST['download'] ){ $query = sprintf('SELECT * FROM dl_table WHERE id = %d', $_POST['id']); mysql_query($query); print mysql_result($query, 0, 'download'); exit; // no need to carry on, our job is done.. } /* if the script reaches here then $_POST['download'] is set, so you need to log a ** new download */ $query = //generate query to increment the database field ?&gt; </code></pre> <p>The above example would work (obviously with the database functions corrected) for simple cases, but obviously - this probably isnt valid code as I've literally typed it over my dinner! Naturally, it's just to give you an idea of a possible solution. (If you do decide to do similar, I've demonstrated <strong>bad practice</strong> - mysql operations should preferably be done with mysqli* functions or PDO now) </p> <p>Look up the PHP My SQL documentation; hopefully this has pointed you in the right direction.</p> <p>Now alternatively, as I know you aren't too keen on jQuery (it's great once you get in to it!) the PHP script that you'd use with the jQuery solution can be used on it's own. Simply changing $_POST[] to $_GET[] and, as other answers have said, using URLs like "ranking.php?id="; however then you need to look at getting the PHP script to pass the file on after the database queries have finished. </p> <p>It also leaves a question as to the displaying of the download number; as without AJAX you will have to embed some PHP in the HTML mark-up inline. I hope I've edited this to make a bit more sense, and given you an overview of the other option! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload