<?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>Michal Borychowski</title>
	<atom:link href="http://tech-blog.borychowski.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech-blog.borychowski.com</link>
	<description>Just another PHP/MySQL weblog</description>
	<lastBuildDate>Thu, 20 Aug 2009 08:44:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Redirect according to browser language: mod rewrite and HTTP_ACCEPT_LANGUAGE</title>
		<link>http://tech-blog.borychowski.com/index.php/2009/03/htaccess/redirect-according-to-browser-language-mod-rewrite-and-http_accept_language/</link>
		<comments>http://tech-blog.borychowski.com/index.php/2009/03/htaccess/redirect-according-to-browser-language-mod-rewrite-and-http_accept_language/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 14:29:09 +0000</pubDate>
		<dc:creator>Michal Borychowski</dc:creator>
				<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mod-rewrite]]></category>

		<guid isPermaLink="false">http://tech-blog.borychowski.com/?p=16</guid>
		<description><![CDATA[I wanted to redirect users to the main page of the website according to his/her browser default language setting.
This setting is sent by the browser at every request to the server as &#8216;Accept-Language&#8217; header and can look like this &#8220;pl,en-us;q=0.7,en;q=0.3&#8243;. You can check it with Live HTTP Headers Add-on for Firefox and read more about [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to redirect users to the main page of the website according to his/her browser default language setting.</p>
<p>This setting is sent by the browser at every request to the server as &#8216;Accept-Language&#8217; header and can look like this &#8220;pl,en-us;q=0.7,en;q=0.3&#8243;. You can check it with <a href="https://addons.mozilla.org/en-US/firefox/addon/3829" target="_blank" onclick="pageTracker._trackPageview('/outgoing/addons.mozilla.org/en-US/firefox/addon/3829?referer=');">Live HTTP Headers Add-on for Firefox</a> and read more about this header at <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.w3.org/Protocols/rfc2616/rfc2616-sec14.html_sec14.4?referer=');">W3C HTTP specification</a>.</p>
<p>In PHP I could do it by checking the value of $_SERVER['HTTP_ACCEPT_LANGUAGE'] and issuing the header(&#8221;Location: /LANG/foo.php&#8221;,TRUE,301); command.</p>
<p>But I didn&#8217;t want to use PHP and did want to do it with .htaccess and mod rewrite. The first thought was to use RewriteCond %{HTTP_ACCEPT_LANGUAGE} as the analogy to RewriteCond %{HTTP_USER_AGENT} or RewriteCond %{HTTP_REFERER}. Unfortunately RewriteCond %{HTTP_ACCEPT_LANGUAGE} does not work! This variable is not present.</p>
<p>What is the solution? Looking more closely at the documentation at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html we have:</p>
<p>%{HTTP:header}, where header can be any HTTP MIME-header name, can always be used to obtain the value of a header sent in the HTTP request.</p>
<p>Which means we can obtain the value of default language setting of the browser by using RewriteCond %{HTTP:Accept-Language}. This is the simple solution, difficult to find on the web.</p>
<p>So if we have a website with three languages: English, German and Spanish and we want English to be the default we would use for example this in .htaccess:</p>
<pre class="brush: bash;">
#The 'Accept-Language' header starts with 'de'
#and the test is case-insensitive ([NC])
RewriteCond %{HTTP:Accept-Language} ^de [NC]
#Redirect user to /de/hauptseite address
#sending 301 (Moved Permanently) HTTP status code
RewriteRule ^$ /de/hauptseite [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es/pagina-primera [L,R=301]

#For every other language (including English :)) use English
RewriteRule ^$ /en/main-page [L,R=301]
</pre>
<p>If you need to be more specific (eg. redirect accordingly to US or Great Britain English (en-us and en-gb)) you would not check only the first two signs of &#8216;Accept-Language&#8217; header but you would need to prepare a little bit more complicated regular expression (some language settings are only two signs, some five signs).</p>
<p>I also thought about possibility of changing redirection addresses from within the CMS system (so that the admin can change main-page, hauptseite or pagina-primera to whatever he/she wishes). The best way would be to save this information in another file (e.g. lang-redirects.conf) and include it within .htaccess. Unfortunately config files inclusion is not possible in .htaccess scope. Another solution I thought about was using mapping-function but it doesn&#8217;t work, either.<br />
So the only we can do is to edit .htaccess itself. If anyone knows a nicer solution  please share it with us.</p>
<p>Do you know any other tips &amp; tricks regarding .htaccess and/or mod rewrite? We are waiting.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftech-blog.borychowski.com%2Findex.php%2F2009%2F03%2Fhtaccess%2Fredirect-according-to-browser-language-mod-rewrite-and-http_accept_language%2F&amp;linkname=Redirect%20according%20to%20browser%20language%3A%20mod%20rewrite%20and%20HTTP_ACCEPT_LANGUAGE" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Ftech-blog.borychowski.com_2Findex.php_2F2009_2F03_2Fhtaccess_2Fredirect-according-to-browser-language-mod-rewrite-and-http_accept_language_2F_amp_linkname=Redirect_20according_20to_20browser_20language_3A_20mod_20rewrite_20and_20HTTP_ACCEPT_LANGUAGE&amp;referer=');"><img src="http://tech-blog.borychowski.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://tech-blog.borychowski.com/index.php/2009/03/htaccess/redirect-according-to-browser-language-mod-rewrite-and-http_accept_language/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to calculate in MySQL average value of data in a row?</title>
		<link>http://tech-blog.borychowski.com/index.php/2009/02/mysql/average-value-in-a-row/</link>
		<comments>http://tech-blog.borychowski.com/index.php/2009/02/mysql/average-value-in-a-row/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 10:28:43 +0000</pubDate>
		<dc:creator>Michal Borychowski</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://tech-blog.borychowski.com/?p=5</guid>
		<description><![CDATA[When programming some voting system I wanted to count in MySQL average of values inserted in one row. If I had data in a column and wanted to return average I would have used the AVG() function but I wanted to return average from a row.
You can say &#8211; where is the problem? Let&#8217;s sum [...]]]></description>
			<content:encoded><![CDATA[<p>When programming some voting system I wanted to count in MySQL average of values inserted in one row. If I had data in a column and wanted to return average I would have used the AVG() function but I wanted to return average from a row.</p>
<p>You can say &#8211; where is the problem? Let&#8217;s sum everything together and divide by the number of items.</p>
<p>Yes, but I wanted that in my voting system you don&#8217;t have to rank all the elements, e.g. let&#8217;s presume we have: cuisine, music and appearance. If someone wants to give a rank for music it is not obligatory to rank cuisine and appearance.</p>
<p>That&#8217;s why I had to use NULL values in my table. I came up with this structure:</p>
<pre class="brush: sql;">
DROP TABLE IF EXISTS `voting`;

CREATE TABLE IF NOT EXISTS `voting` (
`opinion_id` mediumint(9)
unsigned NOT NULL auto_increment,
`rank_0` tinyint(4) default NULL,
`rank_1` tinyint(4) default NULL,
`rank_2` tinyint(4) default NULL,
PRIMARY KEY  (`opinion_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
COLLATE=latin1_general_ci;
</pre>
<p>And let&#8217;s insert these values:</p>
<pre class="brush: sql;">
INSERT INTO `voting` VALUES (1, 1, 2, NULL);
INSERT INTO `voting` VALUES (2, 3, NULL, 4);
INSERT INTO `voting` VALUES (3, 3, 1, 2);
</pre>
<p>When we do:</p>
<pre class="brush: sql;">
SELECT *, (V.rank_0 + V.rank_1 + V.rank_2) / 3
AS row_avg FROM voting V
</pre>
<p>we only receive correct averages for the rows where all values are not NULL. But when I have e.g. 3, NULL, 4 I&#8217;d like to get 3.5 as a return. That&#8217;s the moment when function COALESCE() comes handy.</p>
<p>What does COALESCE () do? From MySQL manual we have:</p>
<p>Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.</p>
<p>mysql&gt; SELECT COALESCE(NULL,1);<br />
-&gt; 1<br />
mysql&gt; SELECT COALESCE(NULL,NULL,NULL);<br />
-&gt; NULL</p>
<p>And these information will help us to build another SELECT statement:</p>
<pre class="brush: sql;">
SELECT *,

#first part
(COALESCE(V.rank_0, 0)
+ COALESCE(V.rank_1, 0)
+ COALESCE(V.rank_2, 0))
/

#second part
(3 -
(COALESCE(V.rank_0 - V.rank_0, 1)
+ COALESCE(V.rank_1 - V.rank_1, 1)
+ COALESCE(V.rank_2 - V.rank_2, 1))
) AS row_avg FROM voting V
</pre>
<p>And now we have the average returned just as we wanted. NULLs are not taken into account when we do the calculations.</p>
<p>So what is inside this SELECT statement? COALESCE() returns the first non-NULL value so in the first part COALESCE(V.rank_0, 0) would return 0 if rank_0 is NULL or rank_0 itself if it is not NULL. The whole first part of SELECT would give us the sum of non-NULL values in the row.</p>
<p>In the second part we have (COALESCE(V.rank_0 &#8211; V.rank_0, 1) which would return 1 only if rank_0 is NULL (rank_0 &#8211; rank_0 is still NULL). We would receive 0 if rank_0 is not NULL (e.g. 4 &#8211; 4 = 0 and it is first non-NULL value in the list to be returned). So the second part let&#8217;s us count the number of columns which have NULL values and subtract it from the number of all items.</p>
<p>This way we have our average from row in MySQL database :)</p>
<p>Do you know any other useful things we can do with COALESCE function or averages in general? Please share them with us.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftech-blog.borychowski.com%2Findex.php%2F2009%2F02%2Fmysql%2Faverage-value-in-a-row%2F&amp;linkname=How%20to%20calculate%20in%20MySQL%20average%20value%20of%20data%20in%20a%20row%3F" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Ftech-blog.borychowski.com_2Findex.php_2F2009_2F02_2Fmysql_2Faverage-value-in-a-row_2F_amp_linkname=How_20to_20calculate_20in_20MySQL_20average_20value_20of_20data_20in_20a_20row_3F&amp;referer=');"><img src="http://tech-blog.borychowski.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://tech-blog.borychowski.com/index.php/2009/02/mysql/average-value-in-a-row/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Why yet another PHP/MySQL blog?</title>
		<link>http://tech-blog.borychowski.com/index.php/2009/02/common/yet-another-php-mysql-blog/</link>
		<comments>http://tech-blog.borychowski.com/index.php/2009/02/common/yet-another-php-mysql-blog/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 14:59:04 +0000</pubDate>
		<dc:creator>Michal Borychowski</dc:creator>
				<category><![CDATA[common]]></category>

		<guid isPermaLink="false">http://tech-blog.borychowski.com/?p=1</guid>
		<description><![CDATA[That&#8217;s a very good question! I know there are tons of information about PHP, MySQL and Apache but somehow finding answers to some questions is very difficult or even impossible. And when you don&#8217;t have a ready answer then you have to discover it by yourself. That&#8217;s why I created this blog &#8211; for sharing [...]]]></description>
			<content:encoded><![CDATA[<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">That&#8217;s a very good question! I know there are tons of information about PHP, MySQL and Apache but somehow finding answers to some questions is very difficult or even impossible. And when you don&#8217;t have a ready answer then you have to discover it by yourself. That&#8217;s why I created this blog &#8211; for sharing my smaller or bigger discoveries with you.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">I hope you would also find here some useful PHP / MySQL tips and tricks for you as I find them on other websites, forums and blogs. In my posts I try to be as accurate and concise as possible. Always waiting for your comments and sharing your experience.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Enjoy!</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Michal Borychowski</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftech-blog.borychowski.com%2Findex.php%2F2009%2F02%2Fcommon%2Fyet-another-php-mysql-blog%2F&amp;linkname=Why%20yet%20another%20PHP%2FMySQL%20blog%3F" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Ftech-blog.borychowski.com_2Findex.php_2F2009_2F02_2Fcommon_2Fyet-another-php-mysql-blog_2F_amp_linkname=Why_20yet_20another_20PHP_2FMySQL_20blog_3F&amp;referer=');"><img src="http://tech-blog.borychowski.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://tech-blog.borychowski.com/index.php/2009/02/common/yet-another-php-mysql-blog/feed/</wfw:commentRss>
		<slash:comments>84</slash:comments>
		</item>
	</channel>
</rss>
