<?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>VinylFox &#187; PHP</title>
	<atom:link href="http://www.vinylfox.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vinylfox.com</link>
	<description>The Playground of VinylFox (Shea Frederick)</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:37:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Grid Filter Plugin &#8211; Backend Code (in PHP)</title>
		<link>http://www.vinylfox.com/grid-filter-php-backend-code/</link>
		<comments>http://www.vinylfox.com/grid-filter-php-backend-code/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 00:00:34 +0000</pubDate>
		<dc:creator>Shea Frederick</dc:creator>
				<category><![CDATA[ExtJS Tutorials]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://ww2.vinylfox.com/?p=3</guid>
		<description><![CDATA[Ive been using the Grid Filter plugin since it was first released - I just fell in love with it right away, so I thought it would be a nice idea to share with you my back end PHP code that makes the filter grid actually return filtered data. It handles all the types of filtering that the Grid Filter plugin has available.]]></description>
			<content:encoded><![CDATA[<p class="p">The code used for this example is available <a href="http://www.vinylfox.com/extjs/examples/grid-filter.zip">by itself</a>, or as one of the examples in the <a href="http://extjs.com/deploy/dev/examples/grid-filtering/grid-filter.html" target"_blank">ExtJS API download.</a> This code is provided with the intent of showing how to build a filtered query from the Grid Filter Plugin in PHP, therefore it does NOT contain any security precautions. Please modify this script before use in a production environment.</p>
<h3>The Plugin</h3>
<p class="p">If your not familiar with the Grid Filter plugin, I would suggest checking out the ExtJS User Extension and Plugins <a href="http://extjs.com/forum/showthread.php?t=14503">forum thread on this plugin</a>. ExtJS has decided to include my PHP script in the example code released with their 2.1 API download, which I think is a great idea, since the question of how exactly to make this grid filtering work seems to come up very often.</p>
<h3>The General Concept</h3>
<p>There are five data types that can be filtered, and each of them has their own specific options.</p>
<ul>
<li>string</li>
<li>list</li>
<li>boolean</li>
<li>numeric</li>
<li>date</li>
</ul>
<p class="p">All the info needed to filter a column is passed by the filter grid plugin as a single &#8216;filter&#8217; post variable that essentially is a multi-dimentional array.</p>
<p class="p">Each filtered column passes three values, and in some cases a fourth.</p>
<ul>
<li>type</li>
<li>value</li>
<li>field</li>
<li>comparison (only for date or numeric &#8211; ne, eq, gt, lt)</li>
</ul>
<p class="p">This ends up making the post variable look something like this &#8211; with two filters.</p>
<pre style="color:#000000">
	 filter[0][data][type] : list
	filter[0][data][value] : small,medium
	      filter[0][field] : size
	 filter[1][data][type] : boolean
	filter[1][data][value] : true
	      filter[1][field] : visible</pre>
<p class="p">The predictable format of post variables makes it easy to code the server side PHP for this.</p>
<h3>The Code</h3>
<p><textarea name="code" class="php" rows="15" cols="100"><br />
$where = &#8221; 0 = 0 &#8220;;<br />
if (is_array($filter)) {<br />
	for ($i=0;$i<count($filter);$i++){<br />
		switch($filter[$i]['data']['type']){<br />
			case &#8216;string&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; LIKE &#8216;%&#8221;.$filter[$i]['data']['value'].&#8221;%&#8217;&#8221;; Break;<br />
			case &#8216;list&#8217; :<br />
				if (strstr($filter[$i]['data']['value'],&#8217;,')){<br />
					$fi = explode(&#8216;,&#8217;,$filter[$i]['data']['value']);<br />
					for ($q=0;$q<count($fi);$q++){<br />
						$fi[$q] = &#8220;&#8216;&#8221;.$fi[$q].&#8221;&#8216;&#8221;;<br />
					}<br />
					$filter[$i]['data']['value'] = implode(&#8216;,&#8217;,$fi);<br />
					$qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; IN (&#8220;.$filter[$i]['data']['value'].&#8221;)&#8221;;<br />
				}else{<br />
					$qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; = &#8216;&#8221;.$filter[$i]['data']['value'].&#8221;&#8216;&#8221;;<br />
				}<br />
			Break;<br />
			case &#8216;boolean&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; = &#8220;.($filter[$i]['data']['value']); Break;<br />
			case &#8216;numeric&#8217; :<br />
				switch ($filter[$i]['data']['comparison']) {<br />
					case &#8216;ne&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; != &#8220;.$filter[$i]['data']['value']; Break;<br />
					case &#8216;eq&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; = &#8220;.$filter[$i]['data']['value']; Break;<br />
					case &#8216;lt&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; < ".$filter[$i]['data']['value']; Break;<br />
					case &#8216;gt&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; > &#8220;.$filter[$i]['data']['value']; Break;<br />
				}<br />
			Break;<br />
			case &#8216;date&#8217; :<br />
				switch ($filter[$i]['data']['comparison']) {<br />
					case &#8216;ne&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; != &#8216;&#8221;.date(&#8216;Y-m-d&#8217;,strtotime($filter[$i]['data']['value'])).&#8221;&#8216;&#8221;; Break;<br />
					case &#8216;eq&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; = &#8216;&#8221;.date(&#8216;Y-m-d&#8217;,strtotime($filter[$i]['data']['value'])).&#8221;&#8216;&#8221;; Break;<br />
					case &#8216;lt&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; < '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'"; Break;<br />
					case &#8216;gt&#8217; : $qs .= &#8221; AND &#8220;.$filter[$i]['field'].&#8221; > &#8216;&#8221;.date(&#8216;Y-m-d&#8217;,strtotime($filter[$i]['data']['value'])).&#8221;&#8216;&#8221;; Break;<br />
				}<br />
			Break;<br />
		}<br />
	}<br />
	$where .= $qs;<br />
}<br />
$query = &#8220;SELECT * FROM demo WHERE &#8220;.$where;<br />
</textarea></p>
<p class="p">This simple bit of code is all we need on the server side to have a fully filterable set of data.</p>
<p class="p"><a href="http://www.extjs.com/deploy/dev/examples/grid-filtering/grid-filter-local.html">See it in action here (switch &#8216;Local Filtering&#8217; off)</a></p>
<p class="p">UPDATE &#8211; 5/28 : Added support for &#8216;ne&#8217; (not equals) on numeric and date fields.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinylfox.com/grid-filter-php-backend-code/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP/MySQL Efficient Paged Data</title>
		<link>http://www.vinylfox.com/phpmysql-efficient-paged-data/</link>
		<comments>http://www.vinylfox.com/phpmysql-efficient-paged-data/#comments</comments>
		<pubDate>Thu, 05 Jul 2007 23:44:39 +0000</pubDate>
		<dc:creator>Shea Frederick</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://ww2.vinylfox.com/?p=32</guid>
		<description><![CDATA[There was some talk recently about the inefficiency of running two queries verses a single query to retrieve paged data, so I decided to do some testing of my own, and here are my results. The first way to retrieve paged data is to run two queries, the first query would be just like the [...]]]></description>
			<content:encoded><![CDATA[<p class="p">
		There was some talk recently about the inefficiency of running two queries verses a single query to retrieve paged data, so I decided to do some testing of my own, and here are my results.
	</p>
<p class="p">
		The first way to retrieve paged data is to run two queries, the first query would be just like the second, just without the LIMIT statement. You would use <i>mysql_num_rows</i> to retreive the total count using the first query, and the second query would be your page of data.
	</p>
<p class="p">
		<b>Testing average for this configuration was 1.9 seconds for every 200 repetitions.</b>
	</p>
<p><textarea name="code" class="php" rows="15" cols="100"><br />
	$sql_count = &#8220;SELECT id, name, title, hire_date, active FROM random_employee_data&#8221;;<br />
	$sql = $sql_count . &#8221; LIMIT &#8220;.$_GET['start'].&#8221;, &#8220;.$_GET['limit'];</p>
<p>	$rs_count = mysql_query($sql_count);<br />
	$rows = mysql_num_rows($rs_count);</p>
<p>	$rs = mysql_query($sql);</p>
<p>	while($obj = mysql_fetch_object($rs))<br />
	{<br />
		$arr[] = $obj;<br />
	}</p>
<p>	$bla = &#8216;{&#8220;total&#8221;:&#8221;&#8216;.$rows.&#8217;&#8221;,&#8221;results&#8221;:&#8217;.json_encode($arr).&#8217;}'; </p>
<p></textarea></p>
<p class="p">
		The second way is almost the same as the first, but instead of using <i>mysql_num_rows</i> to get a total count, we use SQL to provide the count.
	</p>
<p class="p">
		<b>Testing average for this configuration was 1.8 seconds for every 200 repetitions.</b>
	</p>
<p><textarea name="code" class="php" rows="15" cols="100"><br />
	$sql_count = &#8220;SELECT count(*) FROM random_employee_data&#8221;;<br />
	$sql = &#8220;SELECT id, name, title, hire_date, active FROM random_employee_data<br />
			LIMIT &#8220;.$_GET['start'].&#8221;, &#8220;.$_GET['limit'];</p>
<p>	$rs_count = mysql_query($sql_count);<br />
	$data = mysql_fetch_array($rs_count);<br />
	$rows = $data[0];</p>
<p>	$rs = mysql_query($sql);</p>
<p>	while($obj = mysql_fetch_object($rs))<br />
	{<br />
		$arr[] = $obj;<br />
	}</p>
<p>	$bla = &#8216;{&#8220;total&#8221;:&#8221;&#8216;.$rows.&#8217;&#8221;,&#8221;results&#8221;:&#8217;.json_encode($arr).&#8217;}';<br />
</textarea></p>
<p class="p">
		The third way is to use a single query, and utilize PHP to split the data into pages. It was suggested that this would be the fastest way because it only has a single call to the database, however testing showed that was not a correct assumption. The overhead for two calls to the database was not enough to out weigh the processing time needed for all the rows to be returned and PHP to process them.
	</p>
<p class="p">
		<b>Testing average for this configuration was 2.2 seconds for every 200 repetitions.</b>
	</p>
<p><textarea name="code" class="php" rows="15" cols="100"><br />
	$sql = &#8220;SELECT id, name, title, hire_date, active FROM random_employee_data&#8221;;</p>
<p>	$rs = mysql_query($sql);</p>
<p>	$r = 0;<br />
	while($obj = mysql_fetch_object($rs))<br />
	{<br />
		if ($r >= $_GET['start'] &#038;&#038; $r <= ($_GET['start'] + $_GET['limit'])) {<br />
			$arr[] = $obj;<br />
		}<br />
		$r++;<br />
	}</p>
<p>	$bla = '{"total":"'.$rows.'","results":'.json_encode($arr).'}';<br />
</textarea></p>
<p class="p">
		It would appear that the second method is the most efficient, followed closely by the first. This could change based on your dataset, for instance the third method might turn out to be the best in a situation where you dont have many rows in your table. There are also situations where the first method would run much slower than the second. My opinion would be to stick with the second method.
	</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinylfox.com/phpmysql-efficient-paged-data/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Grid &#8211; Basics of Paging</title>
		<link>http://www.vinylfox.com/grid-paging/</link>
		<comments>http://www.vinylfox.com/grid-paging/#comments</comments>
		<pubDate>Mon, 02 Apr 2007 03:46:54 +0000</pubDate>
		<dc:creator>Shea Frederick</dc:creator>
				<category><![CDATA[ExtJS Tutorials]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ExtJS 1.x]]></category>

		<guid isPermaLink="false">http://ww2.vinylfox.com/?p=46</guid>
		<description><![CDATA[This tutorial walks you through adding paging to your grid. ]]></description>
			<content:encoded><![CDATA[<p>I would suggest downloading the code used for this example <a href="/yui-ext/examples/grid-paging/">here</a> so you have something to work with. A working example can be found <a href="/yui-ext/examples/grid-paging/grid-paging-working.php">here</a>.</p>
<h3>Grid Data</h3>
<p>A paging grid must have a server side element to perform the breaking up of data into pages.</p>
<p>For this example we are using php as the server side language, and a MySQL database with some randomly generated data. The PHP script below is used to retrieve our data and break it up into pages using the <i>limit</i> and <i>start</i> variables that are passed from the paging toolbar.</p>
<p>
<textarea name="code" class="php" rows="15" cols="100"><br />
$link = mysql_pconnect(&#8220;test-db.vinylfox.com&#8221;, &#8220;test&#8221;, &#8220;testuser&#8221;)<br />
	or die(&#8220;Could not connect&#8221;);<br />
mysql_select_db(&#8220;test&#8221;) or die(&#8220;Could not select database&#8221;);</p>
<p>$sql_count = &#8220;SELECT id, name, title, hire_date, active FROM random_employee_data&#8221;;<br />
$sql = $sql_count . &#8221; LIMIT &#8220;.$_GET['start'].&#8221;, &#8220;.$_GET['limit'];</p>
<p>$rs_count = mysql_query($sql_count);</p>
<p>$rows = mysql_num_rows($rs_count);</p>
<p>$rs = mysql_query($sql);</p>
<p>while($obj = mysql_fetch_object($rs))<br />
{<br />
	$arr[] = $obj;<br />
}</p>
<p>Echo $_GET['callback'].&#8217;({&#8220;total&#8221;:&#8221;&#8216;.$rows.&#8217;&#8221;,&#8221;results&#8221;:&#8217;.json_encode($arr).&#8217;})&#8217;; </p>
<p></textarea></p>
<p>Were not going to dig much into the server side code, since it can vary quite a bit for each developers needs and environment.</p>
<h3>Whats Needed to Make a Paged Grid?</h3>
<p>NOTE: The example uses <i>ScriptTagProxy</i> only because the data resides on a different server from the example code. In most cases you will be pulling data from the same server the code resides on and using <i>HttpProxy</i>.</p>
<p>The only difference in the data Store is the addition of a <i>totalProperty</i> declaration. In our example, we use &#8216;total&#8217; which is coming from our server side script with the value for the total number of rows we had before breaking them up into pages.</p>
<p>
<textarea name="code" class="js" rows="15" cols="100"><br />
    var ds = new Ext.data.Store({</p>
<p>        proxy: new Ext.data.ScriptTagProxy({<br />
            url: &#8216;http://www.vinylfox.com/yui-ext/examples/grid-paging/grid-paging-data.php&#8217;<br />
        }),</p>
<p>        reader: new Ext.data.JsonReader({<br />
            root: &#8216;results&#8217;,<br />
            totalProperty: &#8216;total&#8217;,<br />
            id: &#8216;id&#8217;<br />
        }, [<br />
            {name: 'employee_name', mapping: 'name'},<br />
            {name: 'job_title', mapping: 'title'},<br />
            {name: 'hire_date', mapping: 'hire_date', type: 'date', dateFormat: 'm-d-Y'},<br />
            {name: 'is_active', mapping: 'active'}<br />
        ])</p>
<p>    });<br />
</textarea>
</p>
<h3>The Paging Bar</h3>
<p>Adding a paging toolbar to the bottom of the grid pane, and your almost done.</p>
<p>
<textarea name="code" class="js" rows="15" cols="100"><br />
    var gridFoot = grid.getView().getFooterPanel(true);</p>
<p>    var paging = new Ext.PagingToolbar(gridFoot, ds, {<br />
        pageSize: 25,<br />
        displayInfo: true,<br />
        displayMsg: &#8216;Displaying results {0} &#8211; {1} of {2}&#8217;,<br />
        emptyMsg: &#8220;No results to display&#8221;<br />
    });<br />
</textarea>
</p>
<p>The last step is to pass the initial <i>start</i> and <i>limit</i> parameters to your data load.</p>
<p>
<textarea name="code" class="js" rows="15" cols="100"><br />
	ds.load({params:{start:0, limit:25}});<br />
</textarea>
</p>
<p>A large portion of the time spent setting up paging is going to be your server side environment that handles passing data back and forth with the grid, once you have that in place the task of adding paging to a grid can be very simple.</p>
<h2>The Final Product</h2>
<h4>grid-paging.js</h4>
<p>
<textarea name="code" class="js" rows="15" cols="100"><br />
Ext.onReady(function(){</p>
<p>    var ds = new Ext.data.Store({<br />
		// HttpProxy should be used here<br />
        proxy: new Ext.data.ScriptTagProxy({<br />
            url: &#8216;http://www.vinylfox.com/yui-ext/examples/grid-paging/grid-paging-data.php&#8217;<br />
        }),</p>
<p>        reader: new Ext.data.JsonReader({<br />
            root: &#8216;results&#8217;,<br />
            totalProperty: &#8216;total&#8217;,<br />
            id: &#8216;id&#8217;<br />
        }, [<br />
            {name: 'employee_name', mapping: 'name'},<br />
            {name: 'job_title', mapping: 'title'},<br />
            {name: 'hire_date', mapping: 'hire_date', type: 'date', dateFormat: 'm-d-Y'},<br />
            {name: 'is_active', mapping: 'active'}<br />
        ])</p>
<p>    });</p>
<p>    var cm = new Ext.grid.ColumnModel([{<br />
		   id: 'name',<br />
           header: "Name",<br />
           dataIndex: 'employee_name',<br />
           width: 100<br />
        },{<br />
           header: "Title",<br />
           dataIndex: 'job_title',<br />
           width: 170<br />
        },{<br />
           header: "Hire Date",<br />
           dataIndex: 'hire_date',<br />
           width: 70,<br />
		   renderer: Ext.util.Format.dateRenderer('n/j/Y')<br />
        },{<br />
           header: "Active",<br />
           dataIndex: 'is_active',<br />
           width: 50<br />
        }]);</p>
<p>    var grid = new Ext.grid.Grid(&#8216;grid-paging&#8217;, {<br />
        ds: ds,<br />
        cm: cm,<br />
		autoExpandColumn: &#8216;name&#8217;<br />
    });</p>
<p>    grid.render();</p>
<p>    var gridFoot = grid.getView().getFooterPanel(true);</p>
<p>    var paging = new Ext.PagingToolbar(gridFoot, ds, {<br />
        pageSize: 25,<br />
        displayInfo: true,<br />
        displayMsg: &#8216;Displaying results {0} &#8211; {1} of {2}&#8217;,<br />
        emptyMsg: &#8220;No results to display&#8221;<br />
    });</p>
<p>    ds.load({params:{start:0, limit:25}});</p>
<p>});<br />
</textarea></p>
<h4>grid-paging-working.php</h4>
<p><textarea name="code" class="xml" rows="15" cols="100"><br />
<html><br />
	<head></p>
<link rel="stylesheet" type="text/css" href="/lib/resources/css/ext-all.css" />
		<script type="text/javascript" src="/lib/yui-utilities.js"></script><br />
		<script type="text/javascript" src="/lib/ext-yui-adapter.js"></script><br />
		<script type="text/javascript" src="/lib/ext-all-debug.js"></script></p>
<p>		<script type="text/javascript" src="grid-paging.js"></script></p>
<p>	</head><br />
	<body></p>
<div id="grid-paging" style="border:1px solid #99bbe8;overflow: hidden; width: 665px; height: 300px;"></div>
<p>	</body><br />
</html><br />
</textarea></p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinylfox.com/grid-paging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating JSON Data in PHP</title>
		<link>http://www.vinylfox.com/creating-json-data-in-php/</link>
		<comments>http://www.vinylfox.com/creating-json-data-in-php/#comments</comments>
		<pubDate>Sat, 10 Mar 2007 11:00:38 +0000</pubDate>
		<dc:creator>Shea Frederick</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://ww2.vinylfox.com/?p=48</guid>
		<description><![CDATA[This tutorial shows you a very simple way to create JSON data from a MySQL database using PHP. ]]></description>
			<content:encoded><![CDATA[<p>So you need to encode JSON data for use with ExtJS or some other library? If you are lucky enough to be running PHP 5.2.0 or greater, then you have the optimal environment for encoding JSON data. But all is not lost if you dont have this setup, there are plenty of librarys you can use to take care of this task.
<p>For our examples we are using a MySQL database as the data source.</p>
<h3>PHP 5.2.0 or higher</h3>
<p>As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default, which makes life for you extremely easy. This is also a highly optimized implementation of JSON for PHP, so if you have this available to you, then use it.</p>
<p>Eleven lines of code and you have data ready to be used by many of the Ext objects.</p>
<p>
<textarea name="code" class="php" rows="15" cols="100"><br />
$link = mysql_pconnect(&#8220;server&#8221;, &#8220;user&#8221;, &#8220;password&#8221;) or die(&#8220;Could not connect&#8221;);<br />
mysql_select_db(&#8220;database&#8221;) or die(&#8220;Could not select database&#8221;);</p>
<p>$arr = array();<br />
$rs = mysql_query(&#8220;SELECT id, url, text FROM sample&#8221;);</p>
<p>while($obj = mysql_fetch_object($rs)) {<br />
	$arr[] = $obj;<br />
}</p>
<p>Echo &#8216;{&#8220;posts&#8221;:&#8217;.json_encode($arr).&#8217;}';<br />
</textarea>
</p>
<h3>Less than PHP 5.2.0 and no root access</h3>
<p>The PEAR JSON package will be the way to go if you cant use PHP 5.2.0 or greater and you do not have root access to install php extensions. There are other libraries available, but this librarys inclusion in PEAR makes it the clear choice for reliability.</p>
<p><a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198">http://pear.php.net/pepr/pepr-proposal-show.php?id=198</a></p>
<p>Just two more lines of code to include the library, create an instance of the JSON service, and change the call to encode (line 14).</p>
<p>
<textarea name="code" class="php" rows="15" cols="100"><br />
include_once(&#8216;JSON.php&#8217;);<br />
$json = new Services_JSON();</p>
<p>$link = mysql_pconnect(&#8220;server&#8221;, &#8220;user&#8221;, &#8220;password&#8221;) or die(&#8220;Could not connect&#8221;);<br />
mysql_select_db(&#8220;database&#8221;) or die(&#8220;Could not select database&#8221;);</p>
<p>$arr = array();<br />
$rs = mysql_query(&#8220;SELECT id, url, text FROM sample&#8221;);</p>
<p>while($obj = mysql_fetch_object($rs)) {<br />
	$arr[] = $obj;<br />
}</p>
<p>Echo &#8216;{&#8220;posts&#8221;:&#8217;.$json->encode($arr).&#8217;}';<br />
</textarea>
</p>
<h3>Less than PHP 5.2.0 with root access</h3>
<p>For those of you that need to keep your current version of PHP but want the speed gained by using the PHP JSON extension can download and install it yourself, granted you have root access to the box your on.</p>
<p><a href="http://www.aurore.net/projects/php-json/">http://www.aurore.net/projects/php-json/</a></p>
<p>This way you can use the native PHP JSON functions, so the example code for PHP 5.2.0 or higher will work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinylfox.com/creating-json-data-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
