Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to be able to poll how many global users have downloaded the item and update the frontend without refreshing the page you will either need to use sockets or setInterval():</p> <p>Here's how you would do it with setInterval() and updating the numbers every 3 seconds (you don't want to do it too much or it will be a big load on your server):</p> <pre><code>&lt;ul&gt; &lt;?php foreach($files_array as $key =&gt; $val) { echo "&lt;li id=\"download-$key\"&gt;"; echo '&lt;a class="btn btn-primary btn-large" href="download.php?file=' . urlencode($val) . '"&gt;&lt;span class="download-text"&gt;Download&lt;/span&gt;&lt;/a&gt;&lt;span class="download-count" title="Times Downloaded"&gt;' . (int)$file_downloads[$val] . '&lt;/span&gt;'; echo '&lt;/li&gt;'; } ?&gt; &lt;/ul&gt; &lt;script&gt; setInterval(function() { $.getJSON('/download-count.php', function(e) { var i; for (i in e) { if (e.hasOwnProperty(i)) { $('#download-' + i + ' .download-count').text(e[i]); } } }); }, 3000); // ping every 3 seconds &lt;/script&gt; </code></pre> <p>You'll need a new file for your Ajax (in my example I called it <code>/download-count.php</code>) that will echo out the JSON for your numbers</p> <pre><code>echo json_encode($file_downloads); </code></pre> <p>This is obviously a simplistic example but hopefully it gets the point across.</p> <p>UPDATE: The <strong>download-count.php</strong> file will need to look something like this:</p> <pre><code>&lt;?php /** * You don't show how you populate $file_downloads in your question but * however you do it, do it the same way here */ $file_downloads = foo(); echo json_encode($file_downloads); die(); </code></pre>
 

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