Example of statistics on the number of PHP + Mysql + jQuery file downloads

Source: Internet
Author: User
Example of statistics on the number of PHP + Mysql + jQuery file downloads ,. PHP + Mysql + jQuery: the number of file downloads is an example. in the project, we need to count the number of file downloads. each time a user downloads a file, the corresponding number of downloads is increased by 1, examples of statistics on the number of downloads of similar applications in PHP + Mysql + jQuery files,

In the project, we need to count the number of file downloads. each time a user downloads a file, the corresponding number of downloads increases by 1. similar applications are used in many download sites. This article uses PHP + Mysql + jQuery in combination with examples to achieve the process of clicking files, downloading files, and accumulating the number of times. The whole process is very smooth.

Preparations
In this example, you need to have basic knowledge about PHP, Mysql, jQuery, html, css, and so on. before developing an example, you need to prepare a Mysql data table. In this example, assume that there is a downloads file download table, used to record the file name, the file name stored on the file server, and the number of downloads. The premise is that if data already exists in the download table, the data may be inserted when the file is uploaded in the background of the project, so that we can read the data on the page. The downloads table structure is as follows:

CREATE TABLE IF NOT EXISTS `downloads` (  `id` int(6) unsigned NOT NULL AUTO_INCREMENT,  `filename` varchar(50) NOT NULL,  `savename` varchar(50) NOT NULL,  `downloads` int(10) unsigned NOT NULL DEFAULT '1',  PRIMARY KEY (`id`),  UNIQUE KEY `filename` (`filename`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

You can also directly download the Demo and import the SQL file. all the data is available.
HTML
We add the following HTML structure to the body of the index.html page, where ul. filelist is used to display the file list. now there is no content in it. we will use jQuery to asynchronously read the file list, so don't forget, we also need to load the jQuery library file in html.

CSS
To make the demo better display the page effect, we use CSS to modify the page. the following code mainly sets the display effect of the file list. of course, you can set the corresponding style as needed in the actual project.

#demo{width:728px;margin:50px auto;padding:10px;border:1px solid #ddd;background-color:#eee;} ul.filelist li{background:url("img/bg_gradient.gif") repeat-x center bottom #F5F5F5; border:1px solid #ddd;border-top-color:#fff;list-style:none;position:relative;} ul.filelist li.load{background:url("img/ajax_load.gif") no-repeat; padding-left:20px; border:none; position:relative; left:150px; top:30px; width:200px} ul.filelist li a{display:block;padding:8px;} ul.filelist li a:hover .download{display:block;} span.download{background-color:#64b126;border:1px solid #4e9416;color:white; display:none;font-size:12px;padding:2px 4px;position:absolute;right:8px; text-decoration:none;text-shadow:0 0 1px #315d0d;top:6px; -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;} span.downcount{color:#999;padding:5px;position:absolute; margin-left:10px;text-decoration:none;} 

PHP
The others page is called, and the other is download. php, which is used to respond to the download action, update the download times of the corresponding file, and complete the download through the browser. Filelist. php reads the downloads table and outputs data in JSON format through json_encode (). This is prepared for the following Ajax asynchronous operations.

Require 'Conn. php '; // connect to the database $ result = mysql_query ("SELECT * FROM downloads"); if (mysql_num_rows ($ result) {while ($ row = mysql_fetch_assoc ($ result )) {$ data [] = array ('id' => $ row ['id'], 'file' => $ row ['filename'], 'downloads' => $ row ['downloads']);} echo json_encode ($ data );}

Download. php transmits parameters based on the url to query the corresponding data and check whether the file to be downloaded exists. if so, the number of downloads of the corresponding data is updated + 1 and the header () is used () download. It is worth mentioning that the header () function is used to forcibly download an object, and you can set the name of the object to be downloaded and saved locally. In general, we use the background Upload program to rename the uploaded files and save them to the server. commonly, there are files named by date and time, one of these benefits is to avoid repeated file names and garbled Chinese names. To download a local file, you can use header ("Content-Disposition: attachment; filename =". $ filename) to set the file name to a name that is easy to recognize.

Require ('Conn. php'); // connect to the database $ id = (int) $ _ GET ['id']; if (! Isset ($ id) | $ id = 0) die ('Parameter error! '); $ Query = mysql_query ("select * from downloads where id =' $ ID'"); $ row = mysql_fetch_array ($ query); if (! $ Row) exit; $ filename = iconv ('utf-8', 'gbk', $ row ['filename']); // note the conversion encoding for the Chinese name $ savename = $ row ['savename']; // the actual storage name on the server $ myfile = 'File /'. $ savename; if (file_exists ($ myfile )) {// if the file exists // update the download times mysql_query ("update downloads set downloads = downloads + 1 where id = '$ ID '"); // download the file $ file = @ fopen ($ myfile, "r"); header ("Content-type: application/octet-stream"); header ("Content-Disposition: attachment; filena Me = ". $ filename); while (! Feof ($ file) {echo fread ($ file, 50000) ;}fclose ($ file); exit;} else {echo 'The file does not exist! ';}

JQuery
JQuery on the front-end page mainly completes two tasks: reading the file list asynchronously through Ajax and displaying the file list; responding to the user click event, adding the corresponding file download times to 1. let's see the code:

$ (Function () {$. ajax ({// asynchronous request type: 'GET', url: 'filelist. php ', dataType: 'json', cache: false, beforeSend: function () {$ (". filelist "pai.html ("
  • Loading...
  • ") ;}, Success: function (json) {if (json) {var li =''; $. each (json, function (index, array) {li = li +'
  • '+ Array ['file'] + ''+ array ['downloads'] +' Click to download
  • ';}); $ (". Filelist ").html (li) ;}}); $ ('Ul. filelist '). live ('click', function () {var count = $ ('. downcount', this); count. text (parseInt (count. text () + 1); // number of downloads + 1 });});

    First, after the page is loaded, use $. ajax () to the background filelist. php sends a GET Ajax request when filelist. after php succeeds, it receives the returned json data through $. each () traverses json data objects, constructs html strings, and adds the final strings to ul. the file list in the demo is formed in filelist.
    Then, when you click download, you can use live () to respond to the click event of the dynamically added list element and accumulate the download count.
    Finally, read this article. this is an Ajax case that we usually use. of course, we also have the knowledge of PHP and mysql for download. I hope it will help you.

    Statistics, we need to count the number of file downloads in the project. each time a user downloads a file, the corresponding number of downloads increases by 1. similar applications are in...

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.