Location: PHPKode > scripts > Comments script with pagination > Read.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ro">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Comments Script - CoursesWeb.net</title>
<style type="text/css"><!--
pre {font-size:16px; font-weight:bold; color:blue;}
--></style>
</head>
<body>
<div style="margin:2px; padding:2px; background-color:#fdfdfe;">
<center>
<h2 style="margin:0px">Script Comments with Pagination</h2>
<br />(<i>from <a href="http://www.coursesweb.net/php-mysql/">www.coursesweb.net</a></i>)
</center>
<p>
  - This is a PHP-AJAX script to add and display comments in web pages, the comments are paginated. Pagination is only for comments, keeping the same original URL and webpage content.<br />
  - The script uses jQuery effects, and can be included in both HTML, and PHP files (<i>but the server must have installed PHP</i>).<br />
  - Comments are added into a MySQL table and displayed in UTF-8 encoding (it is used PHP PDO). Thus can be added less common character sets, such as diacritics, special characters or characters specific to other languages.<br/>
  - In the comments can be added "smiles" and an image that will be uploaded on the server. The image is proportioned in the comment to a width of 125 pixels, but when you click on it, will be displayed in full page. The text in comments can be formated with bold, italic, and underline.<br />
  - The code generated by the script is valid XHTML.<br />
  - For anti-spam protection, the form to add comment has a verification code (captcha), and the time when the comment is added it is stored in Session and Cookie, so, the user can add only one comment in an interval of 5 minutes.<br/>
  - The script can send email notifications when new comments are added, if the user selects this option.<br/>
  - In the notification mail, the user receives a link to the page with the comments, and a link to unsubscribe if he/she wants to not receive other notification when new comments are added in that page.<br/>
  - When the Admin logs in, to each comment appears the options: MODIFY, DELETE.<br/>
  - If the comment contains an image uploaded by the user, when that comment is deleted, the file with the image will be deleted from server.<br/>
  - You can test the script on the page: <a href="http://www.coursesweb.net/scripts/test-comments-script-with-pagination.php" title="Test - Comments Script">www.coursesweb.net/scripts/test-comments-script-with-pagination.php</a>
</p>
<p>
To add this script in a web site, you must use a server with PHP and MySQL database.
</p>
<h4>Follow these 7 steps:</h4>
<p>
  <ul>
	1. Copy all these directories on your server: "<b>comm</b>", "<b>commimg</b>", "<b>icos</b>" and "<b>commincls</b>" (with all their files), and the file "<b>comments.php</b>"; in the same directory with the file in wich you want to include this script.<br />
	2. Add in the HTML HEAD section of the file that will display the comments, the following lines (required for graphics, and jQuery):
	  <ul><pre>&lt;link href=&quot;commincls/style_c.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;commincls/jquery_1.7.js&quot;&gt;&lt;/script&gt;</pre></ul>
 <i><b>- If the page already uses jQuery, not add the code that include the jQuery library (jquery_1.7.js).</b></i><br/><br/>
	3-a. In the same file, <b><u>if it is .php</u></b>, add <b>at the beginning</b> of the file (before any other code, or eaven space) the following line:
  <ul><pre>&lt;?php include('comments.php'); ?&gt;</pre></ul>
  Then, in the same file, in the place you want to display the comments, add this code:
	<ul><pre>
&lt;div id=&quot;coment_tag&quot;&gt;&lt;?php echo $comments; ?&gt;&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;commincls/functions.js&quot;&gt;&lt;/script&gt;
	</pre>
	<u>Like you can see added in the code of the file "<b>test.php</b>".</u>
  </ul><br />
  3-b. <b><u>If the file you want to include the comments is .html</u></b>, add the following code:
	<ul><pre>
&lt;div id=&quot;coment_tag&quot;&gt; &lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;commincls/functions.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
// data that are passed to ajaxSend()
var apel_pag = 'pg='+encodeURIComponent(location.pathname+location.search);
ajaxSend('post', 'comments.php?nrp=0', apel_pag, 'coment_tag');
--&gt;&lt;/script&gt;
	</pre>
	<u>Like you can see added in the code of the file "<b>test.html</b>", if you open it with Notepad.</u>
  </ul><br />
	4. Open the file "<b>config.php</b>" (in the "comm/" directory) and change the following data.
	<ul>
	  - For accessing the MySQL database:
	  <ul>
		- <i>$cmysql['host'] = 'localhost';</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
- replace <b>localhost</b> with your MySQL server address.<br />
		- <i>$cmysql['user'] = 'root';</i> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - replace <b>root</b> with your database user name.<br />
		- <i>$cmysql['pass'] = 'passdb';</i>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; - replace <b>passdb</b> with your password for MySQL database.<br />
		- <i>$cmysql['bdname'] = 'dbname';</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - replace <b>dbname</b> with the name of your MySQL database.
	  </ul><br/>
	  - For administrator login, and e-mail, change the following data:
	  <ul>
		- <i>define('CMANAME', 'admin');</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - replace <b>admin</b> with a name you want for Administrator.<br />
		- <i>define('CMAPASS', 'pass');</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - replace <b>pass</b> with a password you want for Administrator.<br/>
		- <i>define('CMAMAIL', 'hide@address.com');</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - replace <b>hide@address.com</b> with the Administrator, or website e-mail.<br />
		<ul><br/>
		  <li><i> Advice: It is indicated to use different data for administrator name and password from those used to access the database, so you can ask someone else to handle the comments administration without he/she has direct access to the database.</i></li></ul>
	  </ul><br/>
    - If your site has a register /login script, and you want than only the logged users to can add comments, set <b>$addcomm</b> to 0 . And set <b>$_SESSION['username']</b> with the session that your script uses to keep logged users; in the following code, in "config.php":
<pre>
$addcomm = 1;
if($addcomm !== 1) {
  if(isset($_SESSION['username'])) $nameusr = $_SESSION['username'];
}
</pre><br/>
	  - If you want the users to not add links in the context of comments, change the value of the following constant, from <b>1</b> to <b>0</b>
		<ul> - <i>define('CMALLOWL', 1);</i></ul>
    - To not allow uploading image in the comment, set the value of the following constant, from 1 to 0:
		<ul> - <i>define('CMALLOWI', 1);</i></ul>
    - To remove the option for notify the users when new comment is added, set the value of the following constant, from 1 to 0:
		<ul> - <i>define('CMALLOWM', 1);</i></ul>
	  - To change the number of comments displayed on the page, change the number 12 of the next constant to your desired number of comments to be displayed on the page:
		<ul> - <i>define('CMROWSPAGE', 12);</i></ul><br/>
- To edit the permissions for the image uploded by User in comment, change these settings (in "config.php"):
<pre>
$imguprule = array(
  'dir' => 'commimg/',                // directory to store uploded images
  'allowext' => array('gif', 'jpg', 'jpe', 'png'),        // allowed extensions
  'maxsize' => 200,       // maximum allowed size for the image file, in KiloBytes
  'width' => 800,         // maximum allowed width, in pixeli
  'height' => 600         // maximum allowed height, in pixeli
);
</pre>
	5. Give read-write-execute permissions to the file "<b>resetcheck.txt</b>" (in the folder "comm/"), by setting CHMOD 0777 with your FTP application, (This file stores the last reset date of the checking comments on the site).<br/><br/>
	6. Give read-write-execute permissions to the directory "<b>commimg</b>",  CHMOD 0777 (this folder store the images added by the user in comments).<br/>
If you don't know how to set  CHMOD permissions, copy the script on your server, right-click on the file, or folder, choose "<b>Properties</b>", and check/select all permissions.<br /><br/>
	7. The last step, create the table in MySQL database. To create the table, open in your browser the file "<b>comm/create_table.php</b>" (In your browser you must access an address like: "<i>http://localhost/comm/create_table.php</i>"), "localhost" is the domain name.
  </ul>
</p><br />
<p>
  Other specifications:
  <ul>
   - The script can be easily changed to be used in other languages, just translate the text in the file "<b>texts.php</b>" (in "comm/" folder).<br/>
    - <b>For a good functionality, it is indicated to include the script in .php file.</b><br />
	- In the file "<b>resetcheck.txt</b>" (in the "comm/" folder) is stored the time of the last reset for checking new comments added in the site. It is used to visualize all comments that have been added in the database since the last reset. This checking can be made by administrator, when he is logged, in the webpage, under comments appears a link: "<b>Latest Comments Posted</b>". After you click on this link, will opens the file "<b>new.php</b>". This file will show all the comments added in the site from the last reset. To reset the time of the last checking, you must add the admin password in the text box at the bottom of the page "new.php", then click "Reset" button. This option is useful especially if you use this script to add comments in multiple pages of your website. Thus, by simply checking this page, you can see if new comments have been added in any page of the site, with a link to it.<br />
	- In the database it is stored the IP of the user which posted the comment, visible only for admin.<br />
	- The "<b>.htaccess</b>" file (in the "comm/" folder) isn't strictly necesary for the script functionality, but it is used to block the external access to "config.php", and "resetcheck.txt".<br />
	- The code for the form to add comments is in the file "formcomm.php" (in "commincls/" folder).<br />
	- The script was successfully tested with Mozilla Firefox, Internet Explorer, and Opera, for a general configuration on localhost with XAMPP, and also on the server of the website <i>www.coursesweb.net</i>. If on other systems doesn't work properly, depends on their configuration. Problems can appear especially on free hostings which can have restrictions on the "session", or not support PDO; or if you modify something in the script code, without knowing PHP and JavaScript.<br />
	-  The files: "<b>test.php</b>", and "<b>test.html</b>" are only for test, and to see how to include the script, you can delete them.<br />
	- The graphics of the comments can be modified in the file "style_c.css" (in the "commincls/" folder).<br /><br />
	- <i><u>If you have respect</u>, not modify the copyright (it is visible only when the form for adding comments is displayed).</i>
  </ul>
</p>
<br />
<p><b><u>IMPORTANT!</u> - The directories "comm", "commimg", "icos", and "commincls" (with all their files), and the file "comments.php" must be copied in the same directory in which you have the file in which you want to include the script.</b>
<br /><br />
- <em>The script is free, <u>does not provide support or personal modifications</u></em>.<br /><br />
<ul>
<h3>Advanced modifications:</h3>
<i>- Make these changes only if you know what this is about!</i><br/><br/>
If you want the comments to be displayed in reverse order, the last comment to be displayed above, make the following changes in the file "class.GetComm.php" (in "comm/" directory):<br/><br/>
<ul><b>1.</b> At the line 52-53, replace the following code:<br/><br/>
<b><i>// Define the SELECT to get the rows for the current page<br/>
$sql = &quot;SELECT * FROM `$this-&gt;table` WHERE `page`='$this-&gt;page' ORDER BY `id` ASC LIMIT $this-&gt;startrow, $this-&gt;rowsperpage&quot;;</i></b></ul><br/>
With this one:
<pre style="font-size:14px;">
$sql = &quot;SELECT * FROM `$this-&gt;table` WHERE `page`='$this-&gt;page' ORDER BY `id` DESC LIMIT $this-&gt;startrow, $this-&gt;rowsperpage&quot;;
</pre><br/>
<ul><b>2.</b> And to change the order number of the pagination, replace this code (at line 48):<br/><br/>
<b><i>$this-&gt;currentpage = isset($_POST['nrp']) ? intval($_POST['nrp']) : $this-&gt;totalpages;</i></b></ul><br/>
With this:
<pre>
if(isset($_POST['nrp'])) $this-&gt;currentpage = intval($_POST['nrp']);
</pre>
<ul>
Replace this code (at line 105):<br/><br/>
<b><i>$nr_coment = $this-&gt;startrow; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // to show the comment number</i></b></ul><br/>
With this one:
<pre>
$nr_coment = $this-&gt;totalcomm - $this-&gt;rowsperpage * ($this-&gt;currentpage - 1) + 1;
</pre>
<ul>
And this code (at line 115):<br/><br/>
<b><i>$nr_coment++;</i></b></ul><br/>
Replace it with this:
<pre>
$nr_coment--;
</pre>
<ul>
- And the last step, replace in the file: "<b>functions.js</b>" (in "commincls/" folder), to the line 329, the value <i>99999</i> with <b>1</b> . This code:<br/><br/>
<b><i>var datasend = 'isajax=1&pg='+url_pag+'&nrp=99999';</i></b></ul><br/>
Replace it with::
<pre>
var datasend = 'isajax=1&pg='+url_pag+'&nrp=1';
</pre><br/><hr/>

With respect,<br />
MarPlo
</ul></p>
</div>
<br />
</body>
</html>
Return current item: Comments script with pagination