<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Novices Design Courses &#187; MySQL</title>
	<atom:link href="http://novices-design-courses.info/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://novices-design-courses.info</link>
	<description>Design Blog</description>
	<lastBuildDate>Thu, 15 Oct 2009 05:09:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The guest book on PHP/MySQL</title>
		<link>http://novices-design-courses.info/the-guest-book-on-phpmysql/</link>
		<comments>http://novices-design-courses.info/the-guest-book-on-phpmysql/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 06:57:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[guest book]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://novices-design-courses.info/?p=56</guid>
		<description><![CDATA[Today I shall tell to you about how to write the guest book on PHP and MySQL. Anything complex{difficult} in it no, and opportunities given guest not the biggest: a paginal conclusion of recordings, check of the entered data, an opportunity to delete recording.
Today I shall tell to you about how to write the guest [...]]]></description>
			<content:encoded><![CDATA[<p>Today I shall tell to you about how to write the guest book on PHP and MySQL. Anything complex{difficult} in it no, and opportunities given guest not the biggest: a paginal conclusion of recordings, check of the entered data, an opportunity to delete recording.<span id="more-56"></span></p>
<p>Today I shall tell to you about how to write the guest book on PHP and MySQL. Anything complex{difficult} in it no, and opportunities given guest not the biggest: a paginal conclusion of recordings, check of the entered data, an opportunity to delete recording.</p>
<p>We admit{allow}, that at you already is PHP, MySQL and the web &#8211; server. All of you have established and have adjusted.</p>
<p>Let&#8217;s start with creation of the table in which the data of our guest book will be stored{kept}. We shall ask the user a name and the comment. At desire the user can inform e-mail addresses and a homepage. For administration of the book we need one more field, unique for each recording, &#8211; the identifier. Well and date, certainly. In a result such table turns out:</p>
<p>CREATE TABLE gb (</p>
<p>id int (10) unsigned NOT NULL auto_increment,</p>
<p>datetime datetime DEFAULT &#8216; 0000-00-00 00:00:00 &#8216; NOT NULL,</p>
<p>name varchar (100) NOT NULL,</p>
<p>email varchar (100),</p>
<p>www varchar (100),</p>
<p>message text NOT NULL,</p>
<p>PRIMARY KEY (id)</p>
<p>);</p>
<p>The table at us is. Now it is possible to start programming.</p>
<p>For we shall create a file with adjustments of the guest book:</p>
<p>&lt;? php</p>
<p>// The general{common} constants</p>
<p>define (&#8216;PATH&#8217;, &#8216;/gb / &#8216;); // a way to the guest book</p>
<p>define (&#8216; RECSPERPAGE &#8216;, 10); // quantity{amount} of recordings on one page</p>
<p>define (&#8216; ADMIN_EMAIL &#8216;,&#8217; artem@sapegin.ru &#8216;); // email the manager</p>
<p>define (&#8216; ERROR_LOG_FILE &#8216;,&#8217; logs/error.log &#8216;); // a file of a broad gully of mistakes</p>
<p>// Parameters of a DB</p>
<p>define (&#8216; DBHOST &#8216;,&#8217; localhost &#8216;); // the host name</p>
<p>define (&#8216; DBUSER &#8216;,&#8217; root &#8216;); // a login name</p>
<p>define (&#8216; DBPASSWD &#8216;, &#8220;); // the password</p>
<p>define (&#8216; DBNAME &#8216;,&#8217; test &#8216;); // a name of a database</p>
<p>?&gt;</p>
<p>Now we shall think, what auxiliary functions will be necessary for us. On it will be necessary to cooperate with SUBD, will check up and to process the data entered by the user. As for functions of administration on it is required to distinguish the manager from simple users.</p>
<p>Let&#8217;s start with job with SUBD.</p>
<p>&lt;? php</p>
<p>/ ** recource db_connect (string host, string user, string passwd, string dbname)</p>
<p>* Connection to SUBD and opening of a database</p>
<p>*/</p>
<p>function db_connect ($host, $user, $passwd, $dbname)</p>
<p>{</p>
<p>$link = mysql_pconnect ($host, $user, $passwd) or die (&#8216; Could not connect to database &#8216;);</p>
<p>mysql_select_db ($dbname) or die (&#8216; Could not select database &#8216;);</p>
<p>return $link;</p>
<p>}</p>
<p>/ ** Carries out search to a DB</p>
<p>*</p>
<p>* @param the text of search</p>
<p>* @return resource id</p>
<p>*/</p>
<p>function db_query ($query)</p>
<p>{</p>
<p>$result = mysql_query ($query)</p>
<p>or die (&#8216; Bad database query &#8216;);</p>
<p>return $result;</p>
<p>}</p>
<p>/ ** Carries out search to a DB (placeholder)</p>
<p>*</p>
<p>* @param the text of search</p>
<p>* @param*</p>
<p>* @return resource id</p>
<p>*/</p>
<p>function db_query_ex ($query)</p>
<p>{</p>
<p>$values = func_get_args ();</p>
<p>array_shift ($values);</p>
<p>$i = 0;</p>
<p>return db_query (preg_replace (&#8216; %? %e &#8216;,&#8217; &#8220;&#8216;&#8221; .addslashes ($values [$i ++]). &#8220;&#8216;&#8221; &#8216;,</p>
<p>$query));</p>
<p>}</p>
<p>?&gt;</p>
<p>Processing of lines (check and kill of the data entered by the user).</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Whether checks is the line e-mail address</p>
<p>*/</p>
<p>function strings_isemail ($string)</p>
<p>{</p>
<p>return preg_match (&#8216; % [-.w] + [-w] + (?:. [-w] +) + % &#8216;, $string);</p>
<p>}</p>
<p>/ **</p>
<p>* Addition of links on http and e-mail</p>
<p>*/</p>
<p>function strings_addlinks ($string)</p>
<p>{</p>
<p>return preg_replace (</p>
<p>&#8216; % ((?:http|ftp): // [-w] + (?:. [-w] +) +b [-w: and? = +! / ~ * $. ' %] *) (? &lt;! [.?!)]) % i &#8216;,</p>
<p>&#8216; &lt;a href = &#8220;\1 &#8220;&gt; \1 &lt;a&gt; &#8216;,</p>
<p>$string</p>
<p>);</p>
<p>}</p>
<p>/ **</p>
<p>* Cleaning a line</p>
<p>*/</p>
<p>function strings_clear ($string)</p>
<p>{</p>
<p>$string = trim ($string);</p>
<p>$string = stripslashes ($string);</p>
<p>return htmlspecialchars ($string, ENT_QUOTES);</p>
<p>}</p>
<p>/ **</p>
<p>* Trimming a line</p>
<p>*/</p>
<p>function strings_stripstring ($text, $wrap, $length)</p>
<p>{</p>
<p>$text = preg_replace (&#8216; % (S {&#8216;. $ wrap. &#8216;}) % &#8216;, &#8216;\1, $text);</p>
<p>return substr ($text, 0, $length);</p>
<p>}</p>
<p>?&gt;</p>
<p>autentifikacii the manager I leave a spelling to you as the domestic task. There are many ways and their discussion &#8211; a subject of separate clause{article}. I shall result only function &#8211; zaglushku:</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Check: the manager or the usual user</p>
<p>*/</p>
<p>function auth_is_admin ()</p>
<p>{</p>
<p>return $ _GET [' admin '];</p>
<p>}</p>
<p>?&gt;</p>
<p>Further goes enough the big module which contains almost all HTML-code of the guest book, &#8211; a pattern. In him there is nothing complex{difficult} and his{its} spelling is possible quite under force verstal`hhiku a site if at you those is present.</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Heading of page</p>
<p>*/</p>
<p>function template_header ($page)</p>
<p>{</p>
<p>?&gt; &lt;html&gt;</p>
<p>&lt;head&gt;</p>
<p>&lt;title&gt; page &lt;? = $page?&gt; &lt;fjGuestbook Demo &lt;/title&gt;</p>
<p>&lt;style&gt;</p>
<p>body {</p>
<p>padding: 15px;</p>
<p>margin: 0;</p>
<p>color: *333;</p>
<p>background-color: *eee;</p>
<p>border-left: 30px solid *adba8e;</p>
<p>font: 500 .9em verdana, arial, helvetica;</p>
<p>}</p>
<p>a:link {color: *250;}</p>
<p>a:visited {color: *639;}</p>
<p>a:active, a:hover {</p>
<p>color: *c00;</p>
<p>text-decoration: underline;</p>
<p>}</p>
<p>h1 {font-size: 150 %;}</p>
<p>h2 {font-size: 110 %;}</p>
<p>.c {margin-bottom: 10px;}</p>
<p>.cn {</p>
<p>background-color: *d2d6bc;</p>
<p>padding: 2px 4px;</p>
<p>margin-bottom: 4px;</p>
<p>}</p>
<p>&lt;/style&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body&gt;</p>
<p>&lt;h1&gt; fjGuestbook Demo &lt;/h1&gt; &lt;? php</p>
<p>}</p>
<p>/ **</p>
<p>* The termination{ending} of page</p>
<p>*/</p>
<p>function template_footer ()</p>
<p>{</p>
<p>?&gt;</p>
<p>&lt;p&gt; fjGuestbook 1.2. Copyright © 2002-2004</p>
<p>&lt;a href = &#8221; http: // sapegin.ru &#8220;&gt; Artem Sapegin &lt;/a&gt; &lt;/p&gt;</p>
<p>&lt;/body&gt; &lt;/html&gt;</p>
<p>&lt;? php</p>
<p>}</p>
<p>/ **</p>
<p>* The form of addition of new recording</p>
<p>*/</p>
<p>function template_form ($name, $email, $www, $message, $error)</p>
<p>{</p>
<p>// A conclusion of the message on a mistake</p>
<p>function error ($error)</p>
<p>{</p>
<p>if ($error) echo &#8216; &lt;br&gt; &lt;font color = * 880000&gt; &#8216;. $ error.</p>
<p>&#8216; &lt;/font&gt; &#8216;;</p>
<p>}</p>
<p>echo &#8216; &lt;h2&gt; To add the new message &lt;/h2&gt;</p>
<p>&lt;p&gt; &lt;table cellspacing = &#8220;2&#8243; cellpadding = &#8220;2&#8243; border = &#8220;0&#8243;&gt;</p>
<p>&lt;form action = &#8216;.PATH. &#8216;? add=1 method=post&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; the Name &lt;font color = * 880000&gt; * &lt;/font&gt;: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input type=text name = &#8220;name&#8221; size=30</p>
<p>maxlength=100 value = &#8221; &#8216;. $ name. &#8216; &#8220;&gt; &#8216;;</p>
<p>@error ($error [' name ']);</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; Email: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input type=text name = &#8220;email&#8221; size=30</p>
<p>maxlength=100 value = &#8221; &#8216;. $ email. &#8216; &#8220;&gt; &#8216;;</p>
<p>@error ($error [' email ']);</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; URL: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input type=text name = &#8220;www&#8221; size=30</p>
<p>maxlength=100 value = &#8221; &#8216;. $ www. &#8216; &#8220;&gt; &#8216;;</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; the Message &lt;font color = * 880000&gt; * &lt;/font&gt;: &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;textarea cols=40 rows=5</p>
<p>name = &#8220;message&#8221;&gt; &#8216;. $ message. &#8216; &lt;/textarea&gt; &#8216;;</p>
<p>@error ($error [' message ']);</p>
<p>echo &#8216; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;small&gt; &lt;font color = * 880000&gt; * &lt;/font&gt;</p>
<p>- Obligatory fields &lt;/small&gt; &lt;/td&gt;</p>
<p>&lt;/tr&gt; &lt;tr&gt;</p>
<p>&lt;td&gt; &lt;/td&gt;</p>
<p>&lt;td&gt; &lt;input name = &#8220;sb&#8221; type=submit</p>
<p>value = &#8221; To add the message &#8220;&gt; &lt;/td&gt;</p>
<p>&lt;/form&gt; &lt;/tr&gt;</p>
<p>&lt;/table&gt; &#8216;;</p>
<p>}</p>
<p>/ **</p>
<p>* A seal of one recording the guest book</p>
<p>*/</p>
<p>function template_show_body ($id, $name, $email, $www, $message, $datetime)</p>
<p>{</p>
<p>$out = &#8216; &lt;div&gt; &lt;div&gt; &lt;b&gt; &#8216;. $ name. &#8216; &lt;/b&gt; &#8216;;</p>
<p>// If is email or homepage &#8211; it is printed them</p>
<p>if ($email || $www)</p>
<p>{</p>
<p>$out. = &#8216; (&#8216;;</p>
<p>if ($email)</p>
<p>$out. = &#8216; &lt;a href=mailto: &#8216;. $ email. &#8216;&gt; email &lt;/a&gt; &#8216;;</p>
<p>if ($email ** $www)</p>
<p>$out. = &#8216; | &#8216;;</p>
<p>if ($www)</p>
<p>$out. = &#8216; &lt;a href = &#8216;. $ www. &#8216;&gt; www &lt;/a&gt; &#8216;;</p>
<p>$out. = &#8216;) &#8216;;</p>
<p>}</p>
<p>$out. = &#8216; writes &#8216;. $ datetime. &#8216;: &lt;/div&gt; &#8216;. $ message. &#8216; &lt;/div&gt; &#8216;;</p>
<p>// If the guest book is looked through by the manager &#8211; print the button</p>
<p>// Removals{Distances} of recording</p>
<p>if (auth_is_admin ())</p>
<p>{</p>
<p>$out. = &#8216; &lt;div&gt; [&lt;a href = '.PATH. '? admin=1*del = '. $ id.</p>
<p>'&gt; to remove &lt;/a&gt;] &lt;/div&gt; &#8216;;</p>
<p>}</p>
<p>return $out;</p>
<p>}</p>
<p>?&gt;</p>
<p>And, we at last have reached the main thing. Up to the module of the guest book. I shall try to write more comments that to you it was understandable.</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* Creation of the table if she{it} still no</p>
<p>*/</p>
<p>function gb_install ()</p>
<p>{</p>
<p>db_query (</p>
<p>&#8216; CREATE TABLE IF NOT EXISTS gb (</p>
<p>id int (10) unsigned NOT NULL auto_increment,</p>
<p>datetime datetime NOT NULL default &#8216; 0000-00-00 00:00:00 &#8216;,</p>
<p>name varchar (100) NOT NULL default &#8220;,</p>
<p>email varchar (100) default NULL,</p>
<p>www varchar (100) default NULL,</p>
<p>message text NOT NULL,</p>
<p>PRIMARY KEY (id),</p>
<p>INDEX (datetime)</p>
<p>) TYPE=MyISAM; &#8216;</p>
<p>);</p>
<p>}</p>
<p>/ **</p>
<p>* Addition of recording in the guest book</p>
<p>*/</p>
<p>function gb_add ($name, $email, $www, $message, and $error)</p>
<p>{</p>
<p>// We check correctness of filling of fields</p>
<p>$error = &#8220;;</p>
<p>if (empty ($name))</p>
<p>$error [' name '] = &#8216; This obligatory field &#8216;;</p>
<p>if (empty ($message))</p>
<p>$error [' message '] = &#8216; This obligatory field &#8216;;</p>
<p>if (! empty ($email) **! strings_isemail ($email))</p>
<p>$error [' email '] = &#8216; It not email &#8216;;</p>
<p>// If not was mistakes &#8211; it is added</p>
<p>if (! $error)</p>
<p>{</p>
<p>// We clean the data</p>
<p>$name = strings_clear ($name);</p>
<p>$message = strings_clear ($message);</p>
<p>$name = strings_stripstring ($name, 15, 100);</p>
<p>$email = strings_stripstring ($email, 100, 100);</p>
<p>$www = strings_stripstring ($www, 100, 100);</p>
<p>$message = strings_stripstring ($message, 100, 2000);</p>
<p>$message = nl2br ($message);</p>
<p>// If the user was too lazy to write http:// before the address &#8211; we shall make</p>
<p>// It for him{it}</p>
<p>if (! empty ($www) ** &#8216; http:// &#8216;! = substr ($www, 0, 7))</p>
<p>$www = &#8216; http: // &#8216;. $ www;</p>
<p>// Search about addition of recording in a database</p>
<p>db_query_ex (&#8216; INSERT INTO gb (name, email, www, message, datetime)</p>
<p>VALUES (????, NOW ()) &#8216;, $name, $email, $www, $message);</p>
<p>// We throw a browser on the first page</p>
<p>// It is necessary, that if the user will press button Refresh,</p>
<p>// Recording was not added once again</p>
<p>header (&#8216; Location: &#8216;.PATH. &#8220;? page=1 &#8220;);</p>
<p>}</p>
<p>}</p>
<p>// Removal{Distance} of recording from the guest book</p>
<p>function gb_delete ($id)</p>
<p>{</p>
<p>// Search about removal{distance} of recording from a database</p>
<p>// WHERE id = &#8216;. $ id specifies recording which should be removed</p>
<p>db_query_ex (&#8216; DELETE FROM gb WHERE id =? &#8216;, $id);</p>
<p>header (&#8216; Location: &#8216;.PATH. &#8220;? page=1 &#8220;); //???</p>
<p>}</p>
<p>// A conclusion of page with recordings</p>
<p>function gb_show ($page)</p>
<p>{</p>
<p>// Position of the first recording page</p>
<p>$begin = ($page &#8211; 1) * 10;</p>
<p>// Sample of recordings of a database</p>
<p>// SELECT * FROM gb &#8211; all fields from bd gb</p>
<p>// ORDER BY datetime DESC &#8211; sorting by date of, new from above</p>
<p>// LIMIT &#8216;. $ begin. &#8216;, &#8216;.RECSPERPAGE &#8211; restriction:</p>
<p>// RECSPERPAGE (see defines.php) recordings since $begin</p>
<p>$result = db_query (&#8216; SELECT * FROM gb ORDER BY datetime DESC LIMIT &#8216;.</p>
<p>$begin. &#8216;, &#8216;.RECSPERPAGE);</p>
<p>$out = &#8220;;</p>
<p>// A cycle on all chosen recordings</p>
<p>while ($row = mysql_fetch_array ($result))</p>
<p>$out. = template_show_body ($row [' id '], $row [' name '], $row [' email '],</p>
<p>$row [' www '], $row [' message '], $row [' datetime ']);</p>
<p>// We destroy result</p>
<p>mysql_free_result ($result);</p>
<p>echo $out;</p>
<p>}</p>
<p>// A conclusion of the list of pages</p>
<p>function gb_showpages ($current)</p>
<p>{</p>
<p>// We learn{We find out} number of recordings in the guest book</p>
<p>$result = db_query (&#8216; SELECT * FROM gb &#8216;);</p>
<p>$rows = mysql_num_rows ($result);</p>
<p>if ($rows)</p>
<p>{</p>
<p>$pages = ceil ($rows / RECSPERPAGE);</p>
<p>// We print links to pages (number{room} of the current page is not the link)</p>
<p>echo &#8216; &lt;div&gt; &#8216;;</p>
<p>for ($i = 1; $i &lt;= $pages; $i ++)</p>
<p>{</p>
<p>if ($i! = $current)</p>
<p>echo &#8216; | &lt;a href = &#8216;.PATH. &#8216;? page = &#8216;. $ i. &#8216;&gt;&#8217;. $ i. &#8216; &lt;/a&gt; &#8216;;</p>
<p>else</p>
<p>echo &#8216; | &#8216;. $ i;</p>
<p>}</p>
<p>echo &#8216; | &#8216;;</p>
<p>// If it not polslednjaja page we print the link &#8220;Further&#8221;</p>
<p>if ($current &lt;$pages)</p>
<p>echo &#8216;&gt; a href = &#8216;.PATH. &#8216;? page = &#8216;. ($ current + 1).</p>
<p>&#8216;&gt; is Farther&gt;&gt; &lt;/a&gt; &#8216;;</p>
<p>echo &#8216; &lt;/div&gt; &#8216;;</p>
<p>}</p>
<p>}</p>
<p>?&gt;</p>
<p>And the last &#8211; it is united all together.</p>
<p>&lt;? php</p>
<p>/ **</p>
<p>* fjGuestbook 1.2</p>
<p>*</p>
<p>* A nucleus of the guest book</p>
<p>*</p>
<p>* Copyright 2002-2004 Artem Sapegin</p>
<p>* http://sapegin.ru</p>
<p>*/</p>
<p>// We connect modules</p>
<p>require_once &#8216; my/defines.php &#8216;;</p>
<p>require_once &#8216; my/template.php &#8216;;</p>
<p>require_once &#8216; engine/lib/strings.php &#8216;;</p>
<p>require_once &#8216; engine/lib/auth.php &#8216;;</p>
<p>require_once &#8216; engine/lib/bd.php &#8216;;</p>
<p>require_once &#8216; engine/gb.php &#8216;;</p>
<p>// We are connected to a DB</p>
<p>db_connect (DBHOST, DBUSER, DBPASSWD, DBNAME);</p>
<p>// We create the table if she{it} no</p>
<p>gb_install ();</p>
<p>// We obtain the given forms if the form has been sent</p>
<p>if (! empty ($ _POST [' sb ']))</p>
<p>{</p>
<p>$name = $ _POST [' name '];</p>
<p>$email = $ _POST [' email '];</p>
<p>$www = $ _POST [' www '];</p>
<p>$message = $ _POST [' message '];</p>
<p>$formerr = &#8220;;</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>$name = $email = $www = $message = $formerr = &#8220;;</p>
<p>}</p>
<p>// If number{room} of page is not specified in GET-search, we deduce{remove} the first</p>
<p>if (is_numeric ($ _GET [' page ']))</p>
<p>$page = $ _GET [' page '];</p>
<p>else</p>
<p>$page = 1;</p>
<p>// If it is necessary to add recording, we add</p>
<p>if ($ _GET [' add '])</p>
<p>gb_add ($name, $email, $www, $message, $formerr);</p>
<p>// If it is necessary to remove recording, we delete</p>
<p>if (isset ($ _GET [' del ']) ** auth_is_admin ())</p>
<p>gb_delete (intval ($ _GET [' del ']));</p>
<p>// We print the guest book</p>
<p>template_header ($page);</p>
<p>gb_showpages ($page);</p>
<p>gb_show ($page);</p>
<p>gb_showpages ($page);</p>
<p>template_form ($name, $email, $www, $message, $formerr);</p>
<p>template_footer ();</p>
<p>?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://novices-design-courses.info/the-guest-book-on-phpmysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple banner system phpFBS</title>
		<link>http://novices-design-courses.info/simple-banner-system-phpfbs/</link>
		<comments>http://novices-design-courses.info/simple-banner-system-phpfbs/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 06:46:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://novices-design-courses.info/?p=37</guid>
		<description><![CDATA[For small projects if they initially are not focused on advertising, often there is a need{requirement} of a partner exchange of banners. Usually such happens few times in a month, and you decide what not bad to have near at hand the tool with which help it is possible to operate easily and simply banner [...]]]></description>
			<content:encoded><![CDATA[<p>For small projects if they initially are not focused on advertising, often there is a need{requirement} of a partner exchange of banners. Usually such happens few times in a month, and you decide what not bad to have near at hand the tool with which help it is possible to operate easily and simply banner places on pages of your site.<span id="more-37"></span></p>
<p>For small projects if they initially are not focused on advertising, often there is a need{requirement} of a partner exchange of banners. Usually such happens few times in a month, and you decide what not bad to have near at hand the tool with which help it is possible to operate easily and simply banner places on pages of your site.</p>
<p>Introduction</p>
<p>For small projects if they initially are not focused on advertising, often there is a need{requirement} of a partner exchange of banners. Usually such happens few times in a month, and you decide what not bad to have near at hand the tool with which help it is possible to operate easily and simply banner places on pages of your site.</p>
<p>What in our case means &lt;to operate banner places&gt;? What it is necessary for us from banner system? We list necessary functions:</p>
<p>1. The centralized storage of banners and information on them.</p>
<p>2. Loading a banner on a site through the web &#8211; interface.</p>
<p>3. Installation of the directing link.</p>
<p>4. Addition of new recordings about banners.</p>
<p>5. Removal{Distance} of recordings about banners.</p>
<p>6. Viewing a code and testing of a banner.</p>
<p>All these functions are carried out simple, in my opinion, by banner system phpFBS (FoxWeb Banner System), written on language PHP with base MySQL for 4 hours.</p>
<p>The description of banner system</p>
<p>The system will consist of three PHP-scripts:</p>
<p>1. adm.php &#8211; the panel of administration of banners.</p>
<p>2. conf.php &#8211; connections to base and adjustments.</p>
<p>3. i.php &#8211; for two functions: show and transition of a banner.</p>
<p>Banners will be stored{kept} and be loaded into directories &lt;b&gt; by default a file &#8211; picture, that is. Naturally, rights should be established on it{her} chmod 777 for an opportunity of loadings.</p>
<p>ATTENTION! It is not recommended to use in names of scripts and directories a word banner or similar words, in that case the data, sent to the user automatically &lt;obrezajutsja&gt; proxies and fajrvolami. At testing banner system at my customer it also happened:)</p>
<p>Let&#8217;s begin with simple &#8211; connection to DB MySQL. It carries out a script conf.php. Establish in him necessary registration given MySQL-connections. He is born separately because connection is necessary for &lt;adminskoj&gt; and for &lt;vyvodnoj&gt; parts. In a database we need only one table banners the following structure:</p>
<p>CREATE TABLE ` banners ` (</p>
<p>` banner_id ` tinyint (1) unsigned NOT NULL auto_increment,</p>
<p>` bannername ` varchar (50) default NULL,</p>
<p>` filename ` varchar (50) default NULL,</p>
<p>` url ` varchar (50) default NULL,</p>
<p>` comment ` varchar (50) default NULL,</p>
<p>PRIMARY KEY (` banner_id `)</p>
<p>) ENGINE=MyISAM;</p>
<p>At once I want to please supporters of a file way of storage of recordings &#8211; you are free to think out the functions, but from a DB this system has turned out extremely simple. And if the future it is required to you of 100 and more recordings &#8211; here certainly the DB undoubtedly wins.</p>
<p>conf.php</p>
<p>&lt;? php</p>
<p>mysql_connect (&#8220;localhost&#8221;, &#8220;db_user&#8221;, &#8220;db_pass&#8221;);</p>
<p>mysql_select_db (&#8220;db_name&#8221;);</p>
<p>?&gt;</p>
<p>Let&#8217;s pass to a script of a conclusion and perenapravlenija. To him it is passed two parameters: action (action) and id (number{room} of a banner in the table). First of all the script is connected to base and carries out search about recording id to learn{find out} a way to a file of a banner and the link on which he conducts. Generally speaking, in both cases it is carried out perenapravlenie:</p>
<p>* i.php? action=redirect*id=1 &#8211; it is carried out perenapravlenie on the address of a banner specified in base in a field url. This habitual all action at a clique on a banner the mouse.</p>
<p>* i.php? id=1 &#8211; it is carried out perenapravlenie on the file of a banner specified in base in a field filename. Actually the banner is deduced in a window of a browser as though we have requested it{him} directly (but by means of readdressing from i.php). Well, in general you have understood:)</p>
<p>i.php</p>
<p>&lt;? php</p>
<p>include &#8220;conf.php&#8221;;</p>
<p>$query = &#8221; SELECT * FROM banners WHERE banner_id = $ id &#8220;;</p>
<p>$f = mysql_fetch_array (mysql_query ($query));</p>
<p>extract ($f);</p>
<p>if ($action == &#8220;redirect&#8221;) header (&#8221; Location: &#8220;. $url);</p>
<p>elseif (! $action) header (&#8221; Location: http: // $HTTP_HOST/b / &#8220;. $ filename);</p>
<p>?&gt;</p>
<p>Perenapravlenie standard function PHP header () carries out. Apparently from a code, anything complex{difficult}.</p>
<p>The most complex{difficult} part (in comparison with the others, but actually all is very simple) is a script of administration adm.php. brief structure of this file:</p>
<p>* include (&#8220;conf.php&#8221;) &#8211; connection to a DB.</p>
<p>* function http ($str) &#8211; adds http:// if necessary. The user can enter URL a banner both with a prefix http://, and without him{it} and system it takes into account.</p>
<p>* function banners_table () &#8211; deduces the table of banners. Actually on the screen &#8211; contents of the table banners from a DB.</p>
<p>* function banner_code ($ banner _ id) &#8211; deduces a HTML-code of a banner to the user.</p>
<p>* function banner_show ($banner_id) &#8211; shows the test of a banner. He will be shown how will look on HTML-page of a site.</p>
<p>* Updating the data in base and zakachka a file by pressing the button of sending the form</p>
<p>if ($action == &#8220;write&#8221; ** $banner_id)</p>
<p>* Addition of new recording (it is strict after max numbers{rooms})</p>
<p>if ($action == &#8220;add&#8221; ** $comment)</p>
<p>* Removal{Distance} of recording with specified id</p>
<p>if ($action == &#8220;delete&#8221; ** $banner_id)</p>
<p>* Loading the data in the form from recording with specified id</p>
<p>if ($action == &#8220;read&#8221; ** $banner_id)</p>
<p>* Displays a HTML-code of a banner</p>
<p>if ($action == &#8220;code&#8221; ** $banner_id)</p>
<p>Let&#8217;s say, you have added and have edited recordings, have loaded banners and what is farther? Now having clicked on the link &lt;code&gt; in a line corresponding to the necessary banner, you receive a HTML-code the image &#8211; link like:</p>
<p>&lt;a href = &#8220;./i.php? action=redirect*id=1 &#8220;&gt; &lt;img src = &#8220;./i.php? id=1 &#8221; border = &#8220;0&#8243;/&gt; &lt;/a&gt;</p>
<p>Now you can place it a code in the necessary places of your HTML-pages and&#8230; To forget about them as now at change of a banner you will need to change only recording in the panel of administration.</p>
<p>The complete set of files is accessible here. In the same place the SQL-script for creation of the table banners and a demo of system is stored{kept}.</p>
<p>The described banner system is really used on a site http://58region.ru. It is necessary to note, what is it very simple banner system, and I have tried to make its{her} maximum &#8220;transparent&#8221; for the subsequent escalating and perfection.</p>
]]></content:encoded>
			<wfw:commentRss>http://novices-design-courses.info/simple-banner-system-phpfbs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change of appearance of the counter in CNStats</title>
		<link>http://novices-design-courses.info/change-of-appearance-of-the-counter-in-cnstats/</link>
		<comments>http://novices-design-courses.info/change-of-appearance-of-the-counter-in-cnstats/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 11:55:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://novices-design-courses.info/?p=34</guid>
		<description><![CDATA[At installation CNStats in section &#8221; the Configuration &#8211; the Code of the counter &#8221; is offered to you for accommodation on a site two types of a code:
At installation CNStats in section &#8221; the Configuration &#8211; the Code of the counter &#8221; is offered to you for accommodation on a site two types of [...]]]></description>
			<content:encoded><![CDATA[<p>At installation CNStats in section &#8221; the Configuration &#8211; the Code of the counter &#8221; is offered to you for accommodation on a site two types of a code:</p>
<p><span id="more-34"></span>At installation CNStats in section &#8221; the Configuration &#8211; the Code of the counter &#8221; is offered to you for accommodation on a site two types of a code:</p>
<p>* An obligatory code of gathering of statistics of a site &#8211; he counts visitors of a site;</p>
<p>* opcional`nyj a code of display of the counter &#8211; this code simply draws a picture with figures of visitings &#8211; the graphic counter.</p>
<p>Appearance of the counter which displays the current visitings a site it is possible to change. In given clause{article} variants of graphic counter CNStats are considered{examined}.</p>
<p>By default, counter CNStats looks as follows:</p>
<p>On him three figures are displayed. The uppermost &#8211; it is hit everything, average &#8211; it is hit today, bottom &#8211; hosts today. In this note we shall consider ways of change of appearance of the counter and ways of display on him another, not less the helpful information.</p>
<p>At the end of a note you can see all kinds of considered{examined} counters and to download them for use.</p>
<p>The initial data</p>
<p>CNStats can give the following information for display to the counter:</p>
<p>* It is hit today, yesterday and all;</p>
<p>* Hosts today, yesterday and all;</p>
<p>* Users today, yesterday and all;</p>
<p>* Users now on a site.</p>
<p>As CNStats can takes into account visitings robots it is possible to deduce{remove} the information and on them:</p>
<p>* Robots today, yesterday and all;</p>
<p>* Percentage attitudes{relations} robots / users.</p>
<p>Besides it is possible to make various counters changes of attendance of a site graphically displaying dynamics{changes}.</p>
<p>Display of counters in CNStats</p>
<p>By default, the script displaying a picture of the counter is in CNStats root and called cnts.php (from English Counter-Show). I recommend to name files of your counters in a similar way, for example cnts-big.php, cnts-ttf.php, etc.</p>
<p>Let&#8217;s begin a spelling of a code with some, standard &#8220;fish&#8221; whom the code of any counter CNStats should contain:</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>// We are connected to a configuration file.</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// We create images, from a preset pattern.</p>
<p>$im=ImageCreateFromPng (&#8220;button.png&#8221;);</p>
<p>// We are connected with MySql the server.</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose a database</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>//. Here all necessary actions are carried out</p>
<p>// On creation of the image&#8230;</p>
<p>}</p>
<p>}</p>
<p>// We send HTTP heading Content-Type</p>
<p>Header (&#8221; Content-type: image/png &#8220;);</p>
<p>// We generate images</p>
<p>ImagePng ($im);</p>
<p>// We clear memory</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>Further this code will be inserted without comments.</p>
<p>The counter ¹1 &#8211; standard</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>$im=ImageCreateFromPng (&#8220;button.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// t_hits &#8211; it is hit all</p>
<p>// hits &#8211; it is hit today</p>
<p>// hosts &#8211; hosts today</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, hits, hosts FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// Vydeljam necessary colors</p>
<p>$black=ImageColorAllocate ($im, 0,0,0);</p>
<p>$color=ImageColorAllocate ($im, $COUNTER ["inkR"],</p>
<p>$COUNTER ["inkG"],</p>
<p>$COUNTER ["inkB"]);</p>
<p>// We deduce{remove} numbers</p>
<p>ImageString ($im, 2,86-6*strlen ($res ["t_hits"]), 1, $res ["t_hits"], $color);</p>
<p>ImageString ($im, 1,85-5*strlen ($res ["hits"]), 13, $res ["hits"], $color);</p>
<p>ImageString ($im, 1,85-5*strlen ($res ["hosts"]), 20, $res ["hosts"], $color);</p>
<p>}</p>
<p>}</p>
<p>Header (&#8221; Content-type: image/png &#8220;);</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The most interesting part of this code is SQL search which chooses the displayed data from the table:</p>
<p>SELECT t_hits, hits, hosts FROM cns_counter</p>
<p>Let&#8217;s consider, that else we can &#8220;pull out&#8221; from tables CNStats so that it practically did not load the server.</p>
<p>SELECT hits, hosts, users,</p>
<p>t_hits, t_hosts, t_users,</p>
<p>u_hits, u_hosts,</p>
<p>u_t_hits, u_t_hosts</p>
<p>FROM cns_counter;</p>
<p>Here:</p>
<p>* hits &#8211; it is hit today</p>
<p>* hosts &#8211; hosts today</p>
<p>* users &#8211; users today</p>
<p>* t_hits &#8211; it is hit all</p>
<p>* t_hosts &#8211; hosts of all</p>
<p>* t_users &#8211; users of all</p>
<p>* u_hits &#8211; it is hit from visitors for today (robots are excluded)</p>
<p>* u_hosts &#8211; hosts from visitors for today (robots are excluded)</p>
<p>* u_t_hits &#8211; it is hit from visitors of all (robots are excluded)</p>
<p>* u_t_hosts &#8211; hosts from visitors of all (robots are excluded)</p>
<p>* u_hits-hits &#8211; it is hit from robots for today</p>
<p>* u_hosts-hosts &#8211; hosts from robots for today</p>
<p>* u_t_hits-t_hits &#8211; it is hit from robots of all</p>
<p>* u_t_hosts-t_hosts &#8211; hosts from robots of all</p>
<p>Remember! Robots are taken into account only at use &#8220;PHP-Include&#8221; and the &#8220;combined&#8221; counters.</p>
<p>The search which obtains quantity{amount} of visitors in the data the moment taking place on a site (conditionally) is below resulted.</p>
<p>SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 3 MINUTE;</p>
<p>The counter ¹2 &#8211; is hit everything, hit today, users today and now on a site</p>
<p>As you see from the image of the counter, here we used other base image, for that what to find room for figure &#8221; now on a site &#8220;. (red color)</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>$im=ImageCreateFromPng (&#8220;button-tt.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// t_hits &#8211; it is hit all</p>
<p>// hits &#8211; it is hit today</p>
<p>// users &#8211; users today</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, hits, users FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// We select necessary colors</p>
<p>$black=ImageColorAllocate ($im, 0,0,0);</p>
<p>$red=ImageColorAllocate ($im, 255,0,0);</p>
<p>// We deduce{remove} numbers</p>
<p>ImageString ($im, 2,3,1, $res ["t_hits"], $black);</p>
<p>ImageString ($im, 1,3,13, $res ["hits"], $black);</p>
<p>ImageString ($im, 1,3,20, $res ["users"], $black);</p>
<p>// We choose &#8221; now on a site &#8221;</p>
<p>$r = mysql_query (&#8221; SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 3 MINUTE; &#8220;);</p>
<p>$text = mysql_result ($r, 0,0);</p>
<p>@mysql_free_result ($r);</p>
<p>// We deduce{remove} &#8221; now on a site &#8221;</p>
<p>ImageString ($im, 1,80-5*strlen ($text), 4, $text, $red);</p>
<p>}</p>
<p>}</p>
<p>Header (&#8221; Content-type: image/png &#8220;);</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The counter ¹3 &#8211; use True-Type of fonts at generation of counters</p>
<p>Unfortunately, that support TrueType of fonts is established far from being on all servers, such counter is not included in the distribution kit. It is a pity, very beautiful counter</p>
<p>On this counter, for a change, we have deduced{removed} is hit today, hosts today and users today.</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// This function allows to define{determine} length of a line in pixels</p>
<p>function width ($text) {</p>
<p>$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);</p>
<p>return ($box [2] &#8211; $box [1]);</p>
<p>}</p>
<p>// A font which we use for a conclusion of the counter.</p>
<p>// It is possible to take from C:WindowsFonts</p>
<p>define (&#8216; FONT_TAHOMA &#8216;,&#8217; tahoma.ttf &#8216;);</p>
<p>// Attention! The image should be True-Color (24 bat)</p>
<p>$im=ImageCreateFromPng (&#8220;button-tt.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// hits &#8211; it is hit today</p>
<p>// hosts &#8211; hosts today</p>
<p>// users &#8211; users today</p>
<p>$r = mysql_query (&#8221; SELECT hits, hosts, users FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// We deduce{remove} the text</p>
<p>$text = $res ["hits"];</p>
<p>Imagettftext ($im, 7, 0, 4, 10, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = $res ["hosts"];</p>
<p>Imagettftext ($im, 7, 0, 4, 19, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = $res ["users"];</p>
<p>Imagettftext ($im, 7, 0, 4, 28, 0&#215;03814A, FONT_TAHOMA, $text);</p>
<p>$r=mysql_query (&#8221; SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 10 MINUTE; &#8220;);</p>
<p>$text = mysql_result ($r, 0,0);</p>
<p>Imagettftext ($im, 7, 0, 80-width ($text), 12, 0xC0314A, FONT_TAHOMA, $text);</p>
<p>}</p>
<p>}</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The counter ¹4 &#8211; is hit everything, the percent{interest} of robots is hit today, users today,</p>
<p>In this example, besides robots we shall a little change appearance of the counter &#8211; we shall add shadows, and with the help of spaces we shall make numbers more legible.</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// This function allows to define{determine} length of a line in pixels</p>
<p>function width ($text) {</p>
<p>$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);</p>
<p>return ($box [2] &#8211; $box [1]);</p>
<p>}</p>
<p>// A font which we use for a conclusion of the counter.</p>
<p>// It is possible to take from C:WindowsFonts</p>
<p>define (&#8216; FONT_TAHOMA &#8216;,&#8217; tahoma.ttf &#8216;);</p>
<p>// Attention! The image should be True-Color (24 bat)</p>
<p>$im=ImageCreateFromPng (&#8220;button-tt.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// t_hits &#8211; it is hit all</p>
<p>// hits &#8211; it is hit today</p>
<p>// users &#8211; users today</p>
<p>// u_hits &#8211; it is hit from visitors</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, hits, users, u_hits FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// We deduce{remove} the text</p>
<p>$text = number_format ($res ["t_hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 5, 11, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 4, 10, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 5, 20, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 4, 19, 0&#215;04476C, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["users"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 5, 29, 0xcccccc, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 4, 28, 0&#215;04876C, FONT_TAHOMA, $text);</p>
<p>// We consider percentage of robots</p>
<p>if ($res ["hits"]! =0)</p>
<p>$text = intval ((1-$ res ["u_hits"] / $res ["hits"]) *100). &#8220;%&#8221;;</p>
<p>else</p>
<p>$text = &#8220;0&#8243;;</p>
<p>// We deduce{remove} the text</p>
<p>Imagettftext ($im, 7, 0, 80-width ($text), 12, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 79-width ($text), 11, 0&#215;03804A, FONT_TAHOMA, $text);</p>
<p>}</p>
<p>}</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The counter ¹5 &#8211; all together</p>
<p>This big counter is the sum of all previous.</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// This function allows to define{determine} length of a line in pixels</p>
<p>function width ($text) {</p>
<p>$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);</p>
<p>return ($box [2] &#8211; $box [1]);</p>
<p>}</p>
<p>// A font which we use for a conclusion of the counter.</p>
<p>// It is possible to take from C:WindowsFonts</p>
<p>define (&#8216; FONT_TAHOMA &#8216;,&#8217; tahoma.ttf &#8216;);</p>
<p>// Attention! The image should be True-Color (24 bat)</p>
<p>$im=ImageCreateFromPng (&#8220;button-big.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONFs ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, t_hosts, t_users, hits, hosts,</p>
<p>users, u_hits FROM cns_counter &#8220;);</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>@mysql_free_result ($r);</p>
<p>$text = number_format ($res ["t_hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 11, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 10, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 20, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 19, 0&#215;0331FF, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["t_hosts"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 31, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 30, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["hosts"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 40, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 39, 0&#215;0331FF, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["t_users"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 51, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 50, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["users"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 60, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 59, 0&#215;0331FF, FONT_TAHOMA, $text);</p>
<p>$r=mysql_query (&#8221; SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 3 MINUTE; &#8220;);</p>
<p>$text = mysql_result ($r, 0,0);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 70, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 69, 0xC0314A, FONT_TAHOMA, $text);</p>
<p>$text = sprintf (&#8221; % .2f %% &#8220;, (1-$ res ["u_hits"] / $res ["hits"]) *100);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 82, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 81, 0&#215;03804A, FONT_TAHOMA, $text);</p>
<p>}</p>
<p>}</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://novices-design-courses.info/change-of-appearance-of-the-counter-in-cnstats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Job with the text and graphic data in common in PHP and MySQL</title>
		<link>http://novices-design-courses.info/job-with-the-text-and-graphic-data-in-common-in-php-and-mysql/</link>
		<comments>http://novices-design-courses.info/job-with-the-text-and-graphic-data-in-common-in-php-and-mysql/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 11:52:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://novices-design-courses.info/?p=31</guid>
		<description><![CDATA[I have read many documents devoted to this subject, in result have tried ten from them, and nothing having received in the answer correctly working, have decided to write itself more less suitable for my problem{task} the decision.
I have read many documents devoted to this subject, in result have tried ten from them, and nothing [...]]]></description>
			<content:encoded><![CDATA[<p>I have read many documents devoted to this subject, in result have tried ten from them, and nothing having received in the answer correctly working, have decided to write itself more less suitable for my problem{task} the decision.</p>
<p><span id="more-31"></span>I have read many documents devoted to this subject, in result have tried ten from them, and nothing having received in the answer correctly working, have decided to write itself more less suitable for my problem{task} the decision.</p>
<p>So, production of a problem{task}:</p>
<p>1. It is required to store{keep} such information in a database:</p>
<p>Surname, name, patronymic email, a photo and the brief description (or the biography) the person.</p>
<p>For achievement of an object in view quite widespread complete set of the web &#8211; server is chosen: Apache 1.3.20, PHP 4.1.0, MySQL 4.0.0.</p>
<p>For the beginning we create a DB:</p>
<p>mysqladmin-p create testdb</p>
<p>Then we do{make} its{her} current:</p>
<p>use testdb.</p>
<p>Further we create the table for storage of the information:</p>
<p>CREATE TABLE infouser (</p>
<p>id_infouser int (7) unsigned NOT NULL auto_increment,</p>
<p>lastname varchar (255) NOT NULL default &#8220;,</p>
<p>firstname varchar (255) NOT NULL default &#8220;,</p>
<p>patronym varchar (255) NOT NULL default &#8220;,</p>
<p>imageinfouser mediumblob,</p>
<p>filename1 varchar (50) default NULL,</p>
<p>filesize1 varchar (50) default NULL,</p>
<p>filetype1 varchar (50) default NULL,</p>
<p>infoinfouser varchar (255) default NULL,</p>
<p>emailinfouser varchar (100) default NULL,</p>
<p>PRIMARY KEY (id_infouser))</p>
<p>First we shall create a file for storage of functions, such as, connection with a DB, and patterns that ten times to not copy same:</p>
<p>&lt;? php</p>
<p>// tags for open html-docs</p>
<p>function html_begin ($header)</p>
<p>{</p>
<p>print (&#8221; &lt;html&gt; n &#8220;);</p>
<p>print (&#8221; &lt;head&gt; n &#8220;);</p>
<p>print (&#8221; &lt;META HTTP-EQUIV = &#8220;Content-Type&#8221;</p>
<p>Content = &#8220;text/html&#8221;&gt; &#8220;);</p>
<p>print (&#8221; &lt;title&gt; the Test DB &lt;/title&gt; n &#8220;);</p>
<p>print (&#8221; &lt;/head&gt; n &#8220;);</p>
<p>print (&#8221; &lt;body text = &#8220;*000000&#8243; bgcolor</p>
<p>= &#8220;*52FA90&#8243;&gt; n &#8220;);</p>
<p>print (&#8221; &lt;br&gt; &lt;center&gt; &lt;table width = &#8221; 90 % &#8221;</p>
<p>border = &#8220;1&#8243; bgcolor = &#8220;green&#8221; cols = &#8220;1&#8243;&gt; &#8220;);</p>
<p>print (&#8221; &lt;tr&gt; &lt;td&gt; &lt;p style = &#8221; text-align:</p>
<p>justify; margin-left: 50 px; margin-right: 50 px &#8220;&gt;&#8221;);</p>
<p>if ($header)</p>
<p>print (&#8221; &lt;h3&gt; $header &lt;/h3&gt; n &#8220;);</p>
<p>print (&#8221; &lt;/p&gt; &lt;hr width = &#8221; 100 % &#8221;</p>
<p>size = &#8220;1&#8243; color = &#8220;*c0c0c0&#8243;&gt; &lt;p&gt; &#8220;);</p>
<p>}</p>
<p>//</p>
<p>// tags for close html-docs</p>
<p>function html_end ()</p>
<p>{</p>
<p>print (&#8221; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/center&gt; &#8220;);</p>
<p>print (&#8221; &lt;/body&gt; &lt;/html&gt; &#8220;);</p>
<p>}</p>
<p>//</p>
<p>// function for connect mysql and select database</p>
<p>function connect_mysql ()</p>
<p>{</p>
<p>define (&#8220;DBName&#8221;, &#8220;testdb&#8221;);</p>
<p>define (&#8220;HostName&#8221;, &#8220;localhost&#8221;);</p>
<p>define (&#8220;UserName&#8221;, &#8220;valery&#8221;);</p>
<p>define (&#8220;Password&#8221;, &#8221; &#8220;);</p>
<p>if (! mysql_connect (HostName, UserName, Password))</p>
<p>{echo &#8221; the Server temporarily does not work, come later. &#8220;. DBName. &#8220;! &lt;br&gt; &#8220;;</p>
<p>echo mysql_error ();</p>
<p>exit;</p>
<p>}</p>
<p>mysql_select_db (DBName);</p>
<p>}</p>
<p>?&gt;</p>
<p>We do{make} the form for input of the information.</p>
<p>File with a name insert.php</p>
<p>&lt;? php</p>
<p>include (&#8220;function.inc&#8221;);</p>
<p>$header = (&#8221; to enter the new person &#8220;);</p>
<p>html_begin ($header);</p>
<p>?&gt;</p>
<p>&lt;form action = &#8220;insert2.php&#8221; method = &#8220;post&#8221;</p>
<p>enctype = &#8220;multipart/form-data&#8221;&gt;</p>
<p>Surname: &lt;input type = &#8220;text&#8221; name = &#8220;lastname&#8221;</p>
<p>size = &#8220;30&#8243; maxlenght = &#8220;30&#8243;&gt; &lt;br&gt;</p>
<p>Name: &lt;input type = &#8220;text&#8221; name = &#8220;firstname&#8221;</p>
<p>size = &#8220;30&#8243; maxlenght = &#8220;30&#8243;&gt; &lt;br&gt;</p>
<p>Patronymic: &lt;input type = &#8220;text&#8221; name = &#8220;patronym&#8221;</p>
<p>size = &#8220;30&#8243; maxlenght = &#8220;30&#8243;&gt; &lt;br&gt;</p>
<p>Email: &lt;input type = &#8220;text&#8221; name = &#8220;email&#8221;</p>
<p>size = &#8220;30&#8243; maxlenght = &#8220;30&#8243;&gt; &lt;br&gt;</p>
<p>Photo: &lt;input type = &#8220;file&#8221; name = &#8220;userfile&#8221;&gt; &lt;br&gt;</p>
<p>The information on the person: &lt;br&gt;</p>
<p>&lt;textarea name = &#8220;infoinfouser&#8221; cols = &#8220;40&#8243;</p>
<p>rows = &#8220;5&#8243;&gt; &lt;/textarea&gt;</p>
<p>&lt;br&gt;</p>
<p>&lt;input type = &#8220;submit&#8221; value = &#8221; To enter the new person &#8220;&gt; &lt;br&gt;</p>
<p>&lt;/form&gt;</p>
<p>&lt;? php</p>
<p>html_end ();</p>
<p>?&gt;</p>
<p>Then there is a page accepting the information a name of a file obviously enough insert2.php:</p>
<p>&lt;? php</p>
<p>include (&#8220;function.inc&#8221;);</p>
<p>$header = (&#8221; input of the information &#8220;);</p>
<p>html_begin ($header);</p>
<p>//</p>
<p>// read file image</p>
<p>$fd = fopen ($userfile, &#8220;rb&#8221;);</p>
<p>$userfile2 = fread ($fd, filesize ($userfile));</p>
<p>fclose ($fd);</p>
<p>$userfile2 = addslashes ($userfile2);</p>
<p>//</p>
<p>// insert in db</p>
<p>//</p>
<p>connect_mysql ();</p>
<p>mysql_query (&#8221; SELECT * FROM infouser &#8220;);</p>
<p>mysql_query (&#8221; INSERT INTO infouser (id_infouser, lastname, firstname, patronym,</p>
<p>imageinfouser, filename1, filesize1, filetype1, infoinfouser, email_infouser)</p>
<p>VALUES (&#8220;, &#8216; $ lastname &#8216;,&#8217; $firstname &#8216;,&#8217; $patronym &#8216;,&#8217; $userfile2 &#8216;,&#8217; $userfile_name &#8216;,</p>
<p>&#8216; $userfile_size &#8216;,&#8217; $userfile_type &#8216;,&#8217; $infoinfouser &#8216;,&#8217; $email &#8216;) &#8220;);</p>
<p>echo &#8220;&lt;br&gt;&#8221;;</p>
<p>print &#8221; the New user is successfully entered &#8220;;</p>
<p>mysql_close ();</p>
<p>html_end ();</p>
<p>?&gt;</p>
<p>That&#8217;s all we have entered the data, now they should be read somehow.</p>
<p>For this purpose it is used three more files.</p>
<p>The first file gives out the list of users.</p>
<p>prev.php</p>
<p>&lt;? php</p>
<p>include (&#8220;function.inc&#8221;);</p>
<p>//</p>
<p>$header = (&#8221; viewing of recordings &#8220;);</p>
<p>html_begin ($header);</p>
<p>//</p>
<p>connect_mysql ();</p>
<p>//</p>
<p>print (&#8221; &lt;table cols = &#8220;1&#8243;&gt; &#8220;);</p>
<p>// We deduce{remove} all recordings</p>
<p>$r=mysql_query (&#8221; SELECT * FROM infouser &#8220;);</p>
<p>for ($i=0; $i &lt;mysql_num_rows ($r); $i ++)</p>
<p>{$f=mysql_fetch_array ($r);</p>
<p>print &#8220;&lt;tr&gt;&#8221;;</p>
<p>print &#8221; &lt;td align = &#8220;center&#8221;&gt; &#8220;;</p>
<p>print &#8221; &lt;a target = &#8221; _new &#8221; href = &#8221; sample.php? id = $ f [id_infouser] &#8220;&gt; $f [lastname]</p>
<p>$f [firstname] $f [patronym] &lt;/a&gt; &#8220;;</p>
<p>print &#8220;&lt;p&gt;&#8221;;</p>
<p>//</p>
<p>print &#8220;&lt;/td&gt;&#8221;;</p>
<p>print &#8220;&lt;/tr&gt;&#8221;;</p>
<p>}</p>
<p>print &#8220;&lt;/table&gt;&#8221;;</p>
<p>mysql_close ();</p>
<p>html_end ();</p>
<p>?&gt;</p>
<p>The second file is used for delivery of the data on one user: sample.php</p>
<p>&lt;? php</p>
<p>//</p>
<p>include (&#8220;function.inc&#8221;);</p>
<p>$header = (&#8221; viewing of recordings &#8220;);</p>
<p>html_begin ($header);</p>
<p>//</p>
<p>connect_mysql ();</p>
<p>//</p>
<p>reset ($HTTP_GET_VARS);</p>
<p>while (list ($key, $val) = each ($HTTP_GET_VARS)) {</p>
<p>//</p>
<p>$sql = &#8221; SELECT * FROM infouser WHERE id_infouser = &#8216; $val &#8216; &#8220;;</p>
<p>$result = mysql_query ($sql);</p>
<p>$rows = mysql_num_rows ($result);</p>
<p>echo &#8221; &lt;table border = &#8220;0&#8243; width = &#8221; 70 % &#8220;&gt; n &#8220;;</p>
<p>for ($i = 0; $i &lt;$rows; $i ++) {</p>
<p>$data = mysql_fetch_object ($result);</p>
<p>echo &#8221; &lt;tr&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td&gt; &lt;font color = &#8220;red&#8221; size</p>
<p>= &#8220;+1&#8243;&gt; $data-&gt; lastname &lt;br&gt; $data-&gt; firstname &lt;br&gt;</p>
<p>$data-&gt; patronym &lt;br&gt; &lt;/font&gt; &lt;/td&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td rowspan = &#8220;2&#8243;&gt; &lt;center&gt; &lt;img</p>
<p>src = &#8216; download.php? id = $ data-&gt; id_infouser &#8216; border =</p>
<p>&#8216; 2 &#8216; bgcolor = &#8216; *01cccc &#8216;&gt; &lt;/center&gt; &lt;/td&gt; n &#8220;;</p>
<p>echo &#8221; &lt;/tr&gt; n &#8220;;</p>
<p>echo &#8221; &lt;tr&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td&gt; &lt;font color = &#8220;green&#8221;&gt; $data-&gt; emailinfouser &lt;/font&gt; &lt;/td&gt; n &#8220;;</p>
<p>echo &#8221; &lt;/tr&gt; n &#8220;;</p>
<p>echo &#8221; &lt;tr&gt; n &#8220;;</p>
<p>echo &#8221; &lt;td colspan = &#8220;2&#8243;&gt; $data-&gt; infoinfouser &lt;/td&gt; n &#8220;;</p>
<p>echo &#8221; &lt;/tr&gt; n &#8220;;</p>
<p>}</p>
<p>mysql_free_result ($result);</p>
<p>}</p>
<p>mysql_close ();</p>
<p>//</p>
<p>html_end ();</p>
<p>?&gt;</p>
<p>The following file realizes processing photos and delivery of them: download.php</p>
<p>&lt;? php</p>
<p>//</p>
<p>reset ($HTTP_GET_VARS);</p>
<p>while (list ($key, $val) = each ($HTTP_GET_VARS)) {</p>
<p>//</p>
<p>//</p>
<p>define (&#8220;DBName&#8221;, &#8220;testdb&#8221;);</p>
<p>define (&#8220;HostName&#8221;, &#8220;localhost&#8221;);</p>
<p>define (&#8220;UserName&#8221;, &#8220;valery&#8221;);</p>
<p>define (&#8220;Password&#8221;, &#8221; &#8220;);</p>
<p>if (! mysql_connect (HostName, UserName, Password))</p>
<p>{echo &#8221; the Server temporarily does not work, come</p>
<p>Later. &#8220;. DBName. &#8220;! &lt;br&gt; &#8220;;</p>
<p>echo mysql_error ();</p>
<p>exit;</p>
<p>}</p>
<p>mysql_select_db (DBName);</p>
<p>//</p>
<p>//</p>
<p>$sql = &#8221; SELECT imageinfouser, filename1, filetype1 FROM</p>
<p>infouser WHERE id_infouser = &#8216; $ val &#8216; &#8220;;</p>
<p>$result = @mysql_query ($sql);</p>
<p>$data = @mysql_result ($result, 0, &#8220;imageinfouser&#8221;);</p>
<p>$name = @mysql_result ($result, 0, &#8220;filename1&#8243;);</p>
<p>$type = @mysql_result ($result, 0, &#8220;filetype1&#8243;);</p>
<p>header (&#8221; Content-type: $type &#8220;);</p>
<p>echo $data;</p>
<p>}</p>
<p>mysql_close ();</p>
<p>?&gt;</p>
<p>Change of appearance of the counter in CNStats</p>
<p>Requirements: CNStats 2.7 and is higher. GD 1.0 and is higher</p>
<p>At installation CNStats in section &#8221; the Configuration &#8211; the Code of the counter &#8221; is offered to you for accommodation on a site two types of a code:</p>
<p>* An obligatory code of gathering of statistics of a site &#8211; he counts visitors of a site;</p>
<p>* opcional`nyj a code of display of the counter &#8211; this code simply draws a picture with figures of visitings &#8211; the graphic counter.</p>
<p>Appearance of the counter which displays the current visitings a site it is possible to change. In given clause{article} variants of graphic counter CNStats are considered{examined}.</p>
<p>By default, counter CNStats looks as follows:</p>
<p>On him three figures are displayed. The uppermost &#8211; it is hit everything, average &#8211; it is hit today, bottom &#8211; hosts today. In this note we shall consider ways of change of appearance of the counter and ways of display on him another, not less the helpful information.</p>
<p>At the end of a note you can see all kinds of considered{examined} counters and to download them for use.</p>
<p>The initial data</p>
<p>CNStats can give the following information for display to the counter:</p>
<p>* It is hit today, yesterday and all;</p>
<p>* Hosts today, yesterday and all;</p>
<p>* Users today, yesterday and all;</p>
<p>* Users now on a site.</p>
<p>As CNStats can takes into account visitings robots it is possible to deduce{remove} the information and on them:</p>
<p>* Robots today, yesterday and all;</p>
<p>* Percentage attitudes{relations} robots / users.</p>
<p>Besides it is possible to make various counters changes of attendance of a site graphically displaying dynamics{changes}.</p>
<p>Display of counters in CNStats</p>
<p>By default, the script displaying a picture of the counter is in CNStats root and called cnts.php (from English Counter-Show). I recommend to name files of your counters in a similar way, for example cnts-big.php, cnts-ttf.php, etc.</p>
<p>Let&#8217;s begin a spelling of a code with some, standard &#8220;fish&#8221; whom the code of any counter CNStats should contain:</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>// We are connected to a configuration file.</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// We create images, from a preset pattern.</p>
<p>$im=ImageCreateFromPng (&#8220;button.png&#8221;);</p>
<p>// We are connected with MySql the server.</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose a database</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>//. Here all necessary actions are carried out</p>
<p>// On creation of the image&#8230;</p>
<p>}</p>
<p>}</p>
<p>// We send HTTP heading Content-Type</p>
<p>Header (&#8221; Content-type: image/png &#8220;);</p>
<p>// We generate images</p>
<p>ImagePng ($im);</p>
<p>// We clear memory</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>Further this code will be inserted without comments.</p>
<p>The counter ¹1 &#8211; standard</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>$im=ImageCreateFromPng (&#8220;button.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// t_hits &#8211; it is hit all</p>
<p>// hits &#8211; it is hit today</p>
<p>// hosts &#8211; hosts today</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, hits, hosts FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// Vydeljam necessary colors</p>
<p>$black=ImageColorAllocate ($im, 0,0,0);</p>
<p>$color=ImageColorAllocate ($im, $COUNTER ["inkR"],</p>
<p>$COUNTER ["inkG"],</p>
<p>$COUNTER ["inkB"]);</p>
<p>// We deduce{remove} numbers</p>
<p>ImageString ($im, 2,86-6*strlen ($res ["t_hits"]), 1, $res ["t_hits"], $color);</p>
<p>ImageString ($im, 1,85-5*strlen ($res ["hits"]), 13, $res ["hits"], $color);</p>
<p>ImageString ($im, 1,85-5*strlen ($res ["hosts"]), 20, $res ["hosts"], $color);</p>
<p>}</p>
<p>}</p>
<p>Header (&#8221; Content-type: image/png &#8220;);</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The most interesting part of this code is SQL search which chooses the displayed data from the table:</p>
<p>SELECT t_hits, hits, hosts FROM cns_counter</p>
<p>Let&#8217;s consider, that else we can &#8220;pull out&#8221; from tables CNStats so that it practically did not load the server.</p>
<p>SELECT hits, hosts, users,</p>
<p>t_hits, t_hosts, t_users,</p>
<p>u_hits, u_hosts,</p>
<p>u_t_hits, u_t_hosts</p>
<p>FROM cns_counter;</p>
<p>Here:</p>
<p>* hits &#8211; it is hit today</p>
<p>* hosts &#8211; hosts today</p>
<p>* users &#8211; users today</p>
<p>* t_hits &#8211; it is hit all</p>
<p>* t_hosts &#8211; hosts of all</p>
<p>* t_users &#8211; users of all</p>
<p>* u_hits &#8211; it is hit from visitors for today (robots are excluded)</p>
<p>* u_hosts &#8211; hosts from visitors for today (robots are excluded)</p>
<p>* u_t_hits &#8211; it is hit from visitors of all (robots are excluded)</p>
<p>* u_t_hosts &#8211; hosts from visitors of all (robots are excluded)</p>
<p>* u_hits-hits &#8211; it is hit from robots for today</p>
<p>* u_hosts-hosts &#8211; hosts from robots for today</p>
<p>* u_t_hits-t_hits &#8211; it is hit from robots of all</p>
<p>* u_t_hosts-t_hosts &#8211; hosts from robots of all</p>
<p>Remember! Robots are taken into account only at use &#8220;PHP-Include&#8221; and the &#8220;combined&#8221; counters.</p>
<p>The search which obtains quantity{amount} of visitors in the data the moment taking place on a site (conditionally) is below resulted.</p>
<p>SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 3 MINUTE;</p>
<p>The counter ¹2 &#8211; is hit everything, hit today, users today and now on a site</p>
<p>As you see from the image of the counter, here we used other base image, for that what to find room for figure &#8221; now on a site &#8220;. (red color)</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>$im=ImageCreateFromPng (&#8220;button-tt.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// t_hits &#8211; it is hit all</p>
<p>// hits &#8211; it is hit today</p>
<p>// users &#8211; users today</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, hits, users FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// We select necessary colors</p>
<p>$black=ImageColorAllocate ($im, 0,0,0);</p>
<p>$red=ImageColorAllocate ($im, 255,0,0);</p>
<p>// We deduce{remove} numbers</p>
<p>ImageString ($im, 2,3,1, $res ["t_hits"], $black);</p>
<p>ImageString ($im, 1,3,13, $res ["hits"], $black);</p>
<p>ImageString ($im, 1,3,20, $res ["users"], $black);</p>
<p>// We choose &#8221; now on a site &#8221;</p>
<p>$r = mysql_query (&#8221; SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 3 MINUTE; &#8220;);</p>
<p>$text = mysql_result ($r, 0,0);</p>
<p>@mysql_free_result ($r);</p>
<p>// We deduce{remove} &#8221; now on a site &#8221;</p>
<p>ImageString ($im, 1,80-5*strlen ($text), 4, $text, $red);</p>
<p>}</p>
<p>}</p>
<p>Header (&#8221; Content-type: image/png &#8220;);</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The counter ¹3 &#8211; use True-Type of fonts at generation of counters</p>
<p>Unfortunately, that support TrueType of fonts is established far from being on all servers, such counter is not included in the distribution kit. It is a pity, very beautiful counter</p>
<p>On this counter, for a change, we have deduced{removed} is hit today, hosts today and users today.</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// This function allows to define{determine} length of a line in pixels</p>
<p>function width ($text) {</p>
<p>$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);</p>
<p>return ($box [2] &#8211; $box [1]);</p>
<p>}</p>
<p>// A font which we use for a conclusion of the counter.</p>
<p>// It is possible to take from C:WindowsFonts</p>
<p>define (&#8216; FONT_TAHOMA &#8216;,&#8217; tahoma.ttf &#8216;);</p>
<p>// Attention! The image should be True-Color (24 bat)</p>
<p>$im=ImageCreateFromPng (&#8220;button-tt.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// hits &#8211; it is hit today</p>
<p>// hosts &#8211; hosts today</p>
<p>// users &#8211; users today</p>
<p>$r = mysql_query (&#8221; SELECT hits, hosts, users FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// We deduce{remove} the text</p>
<p>$text = $res ["hits"];</p>
<p>Imagettftext ($im, 7, 0, 4, 10, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = $res ["hosts"];</p>
<p>Imagettftext ($im, 7, 0, 4, 19, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = $res ["users"];</p>
<p>Imagettftext ($im, 7, 0, 4, 28, 0&#215;03814A, FONT_TAHOMA, $text);</p>
<p>$r=mysql_query (&#8221; SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 10 MINUTE; &#8220;);</p>
<p>$text = mysql_result ($r, 0,0);</p>
<p>Imagettftext ($im, 7, 0, 80-width ($text), 12, 0xC0314A, FONT_TAHOMA, $text);</p>
<p>}</p>
<p>}</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The counter ¹4 &#8211; is hit everything, the percent{interest} of robots is hit today, users today,</p>
<p>In this example, besides robots we shall a little change appearance of the counter &#8211; we shall add shadows, and with the help of spaces we shall make numbers more legible.</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// This function allows to define{determine} length of a line in pixels</p>
<p>function width ($text) {</p>
<p>$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);</p>
<p>return ($box [2] &#8211; $box [1]);</p>
<p>}</p>
<p>// A font which we use for a conclusion of the counter.</p>
<p>// It is possible to take from C:WindowsFonts</p>
<p>define (&#8216; FONT_TAHOMA &#8216;,&#8217; tahoma.ttf &#8216;);</p>
<p>// Attention! The image should be True-Color (24 bat)</p>
<p>$im=ImageCreateFromPng (&#8220;button-tt.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONF ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>// We choose the data</p>
<p>// t_hits &#8211; it is hit all</p>
<p>// hits &#8211; it is hit today</p>
<p>// users &#8211; users today</p>
<p>// u_hits &#8211; it is hit from visitors</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, hits, users, u_hits FROM cns_counter &#8220;);</p>
<p>// It is transferable{tolerable} the unique chosen recording in an associative file</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>// We release{exempt} memory</p>
<p>@mysql_free_result ($r);</p>
<p>// We deduce{remove} the text</p>
<p>$text = number_format ($res ["t_hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 5, 11, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 4, 10, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 5, 20, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 4, 19, 0&#215;04476C, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["users"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 5, 29, 0xcccccc, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 4, 28, 0&#215;04876C, FONT_TAHOMA, $text);</p>
<p>// We consider percentage of robots</p>
<p>if ($res ["hits"]! =0)</p>
<p>$text = intval ((1-$ res ["u_hits"] / $res ["hits"]) *100). &#8220;%&#8221;;</p>
<p>else</p>
<p>$text = &#8220;0&#8243;;</p>
<p>// We deduce{remove} the text</p>
<p>Imagettftext ($im, 7, 0, 80-width ($text), 12, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 79-width ($text), 11, 0&#215;03804A, FONT_TAHOMA, $text);</p>
<p>}</p>
<p>}</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
<p>The counter ¹5 &#8211; all together</p>
<p>This big counter is the sum of all previous.</p>
<p>&lt;?</p>
<p>error_reporting (E_ALL and ~E_NOTICE);</p>
<p>include &#8220;config.php&#8221;;</p>
<p>// This function allows to define{determine} length of a line in pixels</p>
<p>function width ($text) {</p>
<p>$box=imagettfbbox (7, 0, FONT_TAHOMA, $text);</p>
<p>return ($box [2] &#8211; $box [1]);</p>
<p>}</p>
<p>// A font which we use for a conclusion of the counter.</p>
<p>// It is possible to take from C:WindowsFonts</p>
<p>define (&#8216; FONT_TAHOMA &#8216;,&#8217; tahoma.ttf &#8216;);</p>
<p>// Attention! The image should be True-Color (24 bat)</p>
<p>$im=ImageCreateFromPng (&#8220;button-big.png&#8221;);</p>
<p>$CONN = mysql_connect ($STATS_CONF ["sqlhost"],</p>
<p>$STATS_CONF ["sqluser"],</p>
<p>$STATS_CONFs ["sqlpassword"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>@mysql_select_db ($STATS_CONF ["dbname"]);</p>
<p>if (mysql_errno () == 0) {</p>
<p>$r = mysql_query (&#8221; SELECT t_hits, t_hosts, t_users, hits, hosts,</p>
<p>users, u_hits FROM cns_counter &#8220;);</p>
<p>$res = mysql_fetch_array ($r, MYSQL_ASSOC);</p>
<p>@mysql_free_result ($r);</p>
<p>$text = number_format ($res ["t_hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 11, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 10, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["hits"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 20, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 19, 0&#215;0331FF, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["t_hosts"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 31, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 30, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["hosts"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 40, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 39, 0&#215;0331FF, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["t_users"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 51, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 50, 0&#215;03314A, FONT_TAHOMA, $text);</p>
<p>$text = number_format ($res ["users"], 0 &#8220;,&#8221;, &#8221; &#8220;);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 60, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 59, 0&#215;0331FF, FONT_TAHOMA, $text);</p>
<p>$r=mysql_query (&#8221; SELECT count (DISTINCT hid) as online</p>
<p>FROM cns_log</p>
<p>WHERE date&gt; NOW () &#8211; INTERVAL 3 MINUTE; &#8220;);</p>
<p>$text = mysql_result ($r, 0,0);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 70, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 69, 0xC0314A, FONT_TAHOMA, $text);</p>
<p>$text = sprintf (&#8221; % .2f %% &#8220;, (1-$ res ["u_hits"] / $res ["hits"]) *100);</p>
<p>Imagettftext ($im, 7, 0, 86-width ($text), 82, 0xCCCCCC, FONT_TAHOMA, $text);</p>
<p>Imagettftext ($im, 7, 0, 85-width ($text), 81, 0&#215;03804A, FONT_TAHOMA, $text);</p>
<p>}</p>
<p>}</p>
<p>ImagePng ($im);</p>
<p>ImageDestroy ($im);</p>
<p>?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://novices-design-courses.info/job-with-the-text-and-graphic-data-in-common-in-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
