<?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>Saki's Blog &#187; php</title>
	<atom:link href="http://blog.extjs.eu/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.extjs.eu</link>
	<description>For good of all productive developers</description>
	<lastBuildDate>Mon, 16 Aug 2010 23:12:57 +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>Displaying 1:n Data in Grid</title>
		<link>http://blog.extjs.eu/know-how/displaying-1n-data-in-grid/</link>
		<comments>http://blog.extjs.eu/know-how/displaying-1n-data-in-grid/#comments</comments>
		<pubDate>Sun, 11 May 2008 17:13:13 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[extjs]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=68</guid>
		<description><![CDATA[<br/>The Problem
Imagine that you have a one-to-many relationship in your database, for example, you have table person in which you keep personal data (first, middle, last names, etc.) and you have table phone where you keep phone numbers (phone type, phone number).
It is quite common to have person:phones, company:phones, order:items, invoice:items, etc relationships, isn&#8217;t it?
Now, [...]]]></description>
			<content:encoded><![CDATA[<br/><h1>The Problem</h1>
<p>Imagine that you have a one-to-many relationship in your database, for example, you have table <b><code>person</code></b> in which you keep personal data (first, middle, last names, etc.) and you have table <b><code>phone</code></b> where you keep phone numbers (phone type, phone number).</p>
<p>It is quite common to have person:phones, company:phones, order:items, invoice:items, etc relationships, isn&#8217;t it?</p>
<p>Now, it is quite easy to create a grid that displays list of persons but what about their phones? They are in the different table. Yes, we could create two stores: one for persons grid and another, hidden, for phones, load them from server and somehow filter phones depending on persons.</p>
<p>Nevertheless, I was looking for a simpler solution as I wanted one client server round trip and I wanted to display &#8220;many&#8221; data in QuickTip. And I found one&#8230;</p>
<h1>Solution &#8211; Server Side</h1>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" target="_blank">MySql</a>, that I use as my main database backend, has function <b><code>group_concat</code></b> since version 4.1 and <a href="http://www.sqlite.org/lang_aggfunc.html" target="_blank">SQLite</a> has it since version 3.5.</p>
<p>The idea is to join tables <b><code>person</code></b> and <b><code>phone</code></b> server side and return &#8220;many&#8221; data in one extra field as string separated by arbitrary separators. The SQL statement would be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> 
    persFirstName<span style="color: #66cc66;">,</span> persMidName<span style="color: #66cc66;">,</span> persLastName<span style="color: #66cc66;">,</span>
    group_concat<span style="color: #66cc66;">&#40;</span>concat_ws<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'~'</span><span style="color: #66cc66;">,</span> phoneType<span style="color: #66cc66;">,</span> phoneNumber<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> phones
<span style="color: #993333; font-weight: bold;">FROM</span> person
    <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> phone <span style="color: #993333; font-weight: bold;">ON</span> person<span style="color: #66cc66;">.</span>persID<span style="color: #66cc66;">=</span>phone<span style="color: #66cc66;">.</span>persIDs
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> person<span style="color: #66cc66;">.</span>persID</pre></div></div>

<p><b><code>phones</code></b> part of the output of the above sql would look like</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Home~<span style="color: #000000;">123456</span><span style="color: #000000; font-weight: bold;">|</span>Work~<span style="color: #000000;">87654</span><span style="color: #000000; font-weight: bold;">|</span>Mobile~<span style="color: #000000;">654321</span></pre></div></div>

<h1>Solution &#8211; Client Side</h1>
<p>We cannot display received phones directly in <b><code>person</code></b> grid (well we could but users would hate us) but we need some processing. I decided to display phones in QuickTips so I needed custom renderer for <b><code>persLastName</code></b>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Last Name rederer including tooltip with phones
 * @param {Mixed} val Value to render
 * @param {Object} cell
 * @param {Ext.data.Record} record
 */</span>
<span style="color: #339933;">,</span>renderLastName<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> cell<span style="color: #339933;">,</span> record<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// get data</span>
    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> record.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// convert phones to array (only once)</span>
    data.<span style="color: #660066;">phones</span> <span style="color: #339933;">=</span> 
        Ext.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">phones</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> data.<span style="color: #660066;">phones</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPhones</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">phones</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// create tooltip</span>
    <span style="color: #003366; font-weight: bold;">var</span> qtip <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">qtipTpl</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">phones</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// return markup</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'&lt;div qtip=&quot;'</span> <span style="color: #339933;">+</span> qtip <span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> val <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function renderLastName</span></pre></div></div>

<p>and <b><code>getPhones</code></b> function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Converts string phones to array of objects
 * @param {String} phones
 * @return {Array} Array of phone objects
 */</span>
<span style="color: #339933;">,</span>getPhones<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>phones<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// empty array if nothing to do</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>phones<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// init return value</span>
    <span style="color: #003366; font-weight: bold;">var</span> retval <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// split string to phones</span>
    <span style="color: #003366; font-weight: bold;">var</span> aps <span style="color: #339933;">=</span> phones.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'|'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// iterate through phones to extract phoneType and phoneNumber</span>
    Ext.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>aps<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>phone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> phone.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'~'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        retval.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>phoneType<span style="color: #339933;">:</span>a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> phoneNumber<span style="color: #339933;">:</span>a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> retval<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function getPhones</span></pre></div></div>

<p>A bit of XTemplate work for QuickTips and we&#8217;re done.</p>
<h1>Conclusion</h1>
<p>This is not full fledged one-to-many data handling with editing, adding and deleting items at &#8220;many&#8221; side, it is just simple display of data from &#8220;many&#8221; table, anyway, it can come handy sometimes.</p>
<p>You can see the working example here: <a href="http://examples.extjs.eu/?ex=one2many" target="_blank">http://examples.extjs.eu</a></p>
<p>The &#8220;many&#8221; display target does not need to be QuickTip, it can be row expander as well.<br />
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/displaying-1n-data-in-grid/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to switch between Production and Development system</title>
		<link>http://blog.extjs.eu/know-how/how-to-switch-between-production-and-development-system/</link>
		<comments>http://blog.extjs.eu/know-how/how-to-switch-between-production-and-development-system/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 00:58:45 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=29</guid>
		<description><![CDATA[<br/>Preface
This article it the follow up of the Writing a Big Application in Ext post and it assumes that your application is written, at least to some degree, that way. It also assumes that the server side language is PHP 5.x, other users can take the idea and write the server side logic in their [...]]]></description>
			<content:encoded><![CDATA[<br/><h1>Preface</h1>
<p>This article it the follow up of the <a title="Permanent Link: Writing a Big Application in Ext" rel="bookmark" href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext" target="_blank">Writing a Big Application in Ext</a> post and it assumes that your application is written, at least to some degree, that way. It also assumes that the server side language is <a href="http://php.net" target="_blank">PHP 5.x</a>, other users can take the idea and write the server side logic in their languages. It is written on and for <a href="http://opensuse.org" target="_blank">Linux</a> environment, again, users of other systems must accommodate it to their environments.</p>
<h1>What&#8217;s the difference anyway?</h1>
<p>On a <em>development</em> system we want fast cycle of edit-file/run-application. Also, we want to have access to human readable, fully commented files into which we can step with <a href="http://getfirebug.com" target="_blank">Firebug</a> debugger to see what is happening when code runs. If your application consists of 100 javascript files we want them all individually included in page head to be able to quickly edit/reload page/see the result.</p>
<p>On a <em>production</em> system we want fastest load possible, no debugging, and possibly to prevent unauthorized users or attackers to read our files. No comments are needed in files.</p>
<h1>The Files</h1>
<p>We have our source javascript files but we need a bit more. First of all we need a file list that would define which files and in which order we will include. This file list will be used by both switch logic and Makefile that will create the compressed version of sources. An example of such file list can be:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">FILES</span>=\
    Ext.ux.grid.Search.js \
    Ext.ux.IconMenu.js \
    Ext.ux.IconCombo.js \
    Ext.ux.ThemeCombo.js \
    Ext.ux.LangSelectCombo.js \
    Ext.ux.grid.RowActions.js \
    Ext.ux.grid.CellActions.js \
    Ext.ux.tree.ArrayTree.js \
    Ext.ux.FileUploader.js \
    Ext.ux.UploadPanel.js \
    Ext.ux.FileTreeMenu.js \
    Ext.ux.FileTreePanel.js</pre></div></div>

<p>Next, we need <code><strong>Makefile</strong></code> (file with rules for <code><strong>make</strong></code> program):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">include filelist
&nbsp;
application-all.js: application-all-debug.js
    java <span style="color: #660033;">-jar</span> ~<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>yuicompressor.jar \
        application-all-debug.js <span style="color: #000000; font-weight: bold;">&gt;</span> application-all.js
&nbsp;
application-all-debug.js: $<span style="color: #7a0874; font-weight: bold;">&#40;</span>FILES<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #c20cb9; font-weight: bold;">cat</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>FILES<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> application-all-debug.js
&nbsp;
clean:
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> application-all-debug.js application-all.js
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">install</span>: application-all.js
    <span style="color: #666666; font-style: italic;"># whatever you need to do to upload </span>
    <span style="color: #666666; font-style: italic;"># new version to production server</span></pre></div></div>

<p>You can download yuicompressor <a href="http://developer.yahoo.com/yui/compressor">here</a> and you may need to install some &#8220;devel tools&#8221; and java on your Linux box for the above to run.</p>
<h1>The Switch</h1>
<p>We need a server-side logic that would generate the includes for individual files on the development system and one-file include on productions system. Decision what system it is running on can be also automatic. For example, our public, production web server has name <code><strong>extjs.eu</strong></code> and our development system has name <strong><code>extjs.localhost</code></strong>.</p>
<p>We can easily check if <strong><code>"extjs.localhost" === $_SERVER["SERVER_NAME"]</code></strong> and if yes we can run development branch if not we run production branch.</p>
<h1>The Development Cycle</h1>
<p>With all this in place, if you add a file you just add it to the file list, test it and debug it on the development system and if everything is works you issue the command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>The compressed version is created and it is uploaded to your production system. The same is true for editing of a file, you only won&#8217;t modify your file list. If you delete file, just remove its line from file list and <strong><code>make install</code></strong>.</p>
<h1>Conclusion</h1>
<p>Hopefully this can make your developer&#8217;s life easier; I couldn&#8217;t live without it.</p>
<p>The complete working example of the above system is <a href="http://examples.extjs.eu?ex=prodswitch" target="_blank">here</a>, with manual switch for demonstration purposes.</p>
<p>Enjoy!<br />
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/how-to-switch-between-production-and-development-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP File Pattern</title>
		<link>http://blog.extjs.eu/patterns/file-patters/php-file-pattern/</link>
		<comments>http://blog.extjs.eu/patterns/file-patters/php-file-pattern/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 15:20:05 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[File Patters]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=9</guid>
		<description><![CDATA[<br/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
&#60;?
// vim: ts=4:sw=4:fdc=4:nu:nospell
/**
 * An Application
 * 
 * @author     Ing. Jozef Sakáloš
 * @copyright  (c) 2009, by Ing. Jozef Sakáloš
 * @date       15. Sepember 2009
 * @version    1.0
 * @revision   $Id$
 *
 * @license application.php is licensed under [...]]]></description>
			<content:encoded><![CDATA[<br/>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">// vim: ts=4:sw=4:fdc=4:nu:nospell</span>
<span style="color: #009933; font-style: italic;">/**
 * An Application
 * 
 * @author     Ing. Jozef Sakáloš
 * @copyright  (c) 2009, by Ing. Jozef Sakáloš
 * @date       15. Sepember 2009
 * @version    1.0
 * @revision   $Id$
 *
 * @license application.php is licensed under the terms of the Open Source
 * LGPL 3.0 license. Commercial use is permitted to the extent that the 
 * code/component(s) do NOT become part of another Open Source or Commercially
 * licensed development library or toolkit without explicit permission.
 * 
 * License details: http://www.gnu.org/licenses/lgpl.html
 */</span>
&nbsp;
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// code here</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// eof</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/patterns/file-patters/php-file-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
