However, with the recent announcement that WordPress (our favorite blogging engine) will be dropping support for that version of PHP, I decided that it is time to also make the switch to the new version - MySQLi (MySQL improved).Īs you can see from the code above, the only major difference with the old way we connected to a database, is that here we create a MySQLi object instead of using the mysql_ functions. Up until now, we've always used the old mysql extension for database connections under PHP, as it is a bit easier to use and I wanted to keep the code compatible with PHP 4. $db_name = new mysqli($db_host, $db_user, $db_pass, $db_name) ĭie('Could not connect to the database') appreciateMe/connect.php $db_host = 'localhost' But first lets take a look at connect.php, which handles the database connection. Most of the script logic is located in c script.php which you can see below. PHP handles the database interactions and is on the backend of the AJAX requests. You will only need to change the contents of script.js to make the script working on your pages. The first is styles.css, which is used to style the page, and appreciate.css, which is located in the plugin directory, and is responsible for the styling of the appreciate button.īefore the closing body tag, you can see that I also include the jQuery library from Google's CDN repository, the plugin.js file and script.js, which uses the plugin to create the button on the page. In the page above, you can see that I am including two stylesheet files. You can safely omit the latter one, leaving you with only one div to code. To have the appreciate button functioning, you just need to provide a container in which the button is inserted, and an optional element, which holds the total number of clicks on the button. The markup of the page is extremely simple. Lets start with the XHTML part of the tutorial. You can create these two tables by running the code from tables.sql in the SQL section of phpMyAdmin from the downloadable archive, part of this tutorial. The timestamp is automatically updated to the current time when an insert occurs. The appreciate_votes table contains the IP of the person that has voted (in the form of an integer), and the id of the page from the appreciate_pages table. The appreciated column holds the number of appreciations of the pages. This way we add an UNIQUE index which will speed up the selects we run on the records, as well ensure there are no duplicate records in the table. The hash field holds an MD5 sum of the URL of the page. This way we can easily determine whether the person has previously voted for the page and display the appropriate version of the button (active or disabled). The second one stores the IP of the person that voted along the unique ID of the page. The first holds one record for each of the pages which have the appreciate button enabled. The script we are doing today uses two tables. So go ahead and download the zip from the button above ( PSD included!) and continue with the tutorial. Today we are implementing such a badge, which you can include in every page of your website with a bit of jQuery magic. With it people share their appreciation for somebody's work. If you've dropped by Behance, you've probably noticed their appreciate badge, which is a neat solution to this exact problem. Comments, the cornerstone of blogging, are too demanding, and users often prefer not to post one. When you publish something online, there are not that many ways to determine whether people like what you have to say.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |