<?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>code-diesel &#187; data</title>
	<atom:link href="http://www.codediesel.com/category/data/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Migrating Access MDB to MySQL</title>
		<link>http://www.codediesel.com/data/migrating-access-mdb-to-mysql/</link>
		<comments>http://www.codediesel.com/data/migrating-access-mdb-to-mysql/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 09:46:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2917</guid>
		<description><![CDATA[A recent task needed me to import a large amount of data from a Access MDB database to MySQL. My first choice for the job was the mdb-tools set of utilities. mdb-tools provides a set of tools and applications to export MDB data and schema to other databases such as MySQL, Oracle, Sybase, PostgreSQL, and [...]]]></description>
			<content:encoded><![CDATA[<p>A recent task needed me to import a large amount of data from a Access MDB database to MySQL. My first choice for the job was the <a href="http://mdbtools.sourceforge.net/" rel="nofollow"  title="mdb-tools" target="_blank">mdb-tools</a> set of utilities. mdb-tools provides a set of tools and applications to export MDB data and schema to other databases such as MySQL, Oracle, Sybase, PostgreSQL, and others. </p>
<p>mdb-tools is available for Linux systems, and as I use Ubuntu, installation was a breeze using the package manager. To install is from the shell use the following.<br />
<span id="more-2917"></span></p>
<h4>Installing mdbtools</h4>

<div class="wp_codebox"><table><tr id="p291710"><td class="code" id="p2917code10"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> mdbtools</pre></td></tr></table></div>

<p>The following command lines tools are available for migrating a MDB to a different database.</p>
<table class="posts_table">
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top">mdb-tables</td>
<td align="left" valign="top">list tables in the specified file</td>
</tr>
<tr>
<td align="left" valign="top">mdb-schema</td>
<td align="left" valign="top">generate schema DDL for the specified file</td>
</tr>
<tr>
<td align="left" valign="top">mdb-export</td>
<td align="left" valign="top">generate CSV style output for a table</td>
</tr>
</tbody>
</table>
<p>Just to check if the correct tables are in the Access database, we will use the &#8216;mdb-tables&#8217; command which will list the table names in the database.</p>

<div class="wp_codebox"><table><tr id="p291711"><td class="code" id="p2917code11"><pre class="bash" style="font-family:monospace;">mdb-tables database.mdb</pre></td></tr></table></div>

<h4>Exporting schema</h4>
<p>Once we are satisfied that the appropriate tables are there we can generate a schema to be used with MySQL. For this we will use the &#8216;mdb-schema&#8217; command. mdb-schema produces data definition language output for the given database. This can be further passed to another database engine (MySQL, Oracle, Sybase, PostgreSQL etc) to create a replica of the original access table format.</p>

<div class="wp_codebox"><table><tr id="p291712"><td class="code" id="p2917code12"><pre class="bash" style="font-family:monospace;">mdb-schema database.mdb  <span style="color: #000000; font-weight: bold;">&gt;</span> schema.sql</pre></td></tr></table></div>

<p>The above will generate the schema for all the tables in the &#8216;database.mdb&#8217; database and save it to the schema.sql file. If you want to output the schema for a particular table rather than all the tables, you will need to use the &#8216;-T&#8217; flag. The &#8216;-T&#8217; flag will restrict the schema for a particular table.</p>

<div class="wp_codebox"><table><tr id="p291713"><td class="code" id="p2917code13"><pre class="bash" style="font-family:monospace;">mdb-schema <span style="color: #660033;">-T</span> table_name database.mdb <span style="color: #000000; font-weight: bold;">&gt;</span> schema.sql</pre></td></tr></table></div>

<p>Rather than saving the schema to a file we can directly import it into MySQL. The following will directly create a MySQL table using the schema from the MDB database by piping the output of mdb-schema to MySQL.</p>

<div class="wp_codebox"><table><tr id="p291714"><td class="code" id="p2917code14"><pre class="bash" style="font-family:monospace;">mdb-schema database.mdb <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-u</span> username <span style="color: #660033;">-p</span> database_name</pre></td></tr></table></div>

<h4>Exporting data</h4>
<p>mdb-tools lets you export the data in two formats &#8211; CSV or SQL. To export the data in a CSV format use the following command. Note that you need to specify the table name from which the data will be exported.</p>

<div class="wp_codebox"><table><tr id="p291715"><td class="code" id="p2917code15"><pre class="bash" style="font-family:monospace;">mdb-export database.mdb table_name <span style="color: #000000; font-weight: bold;">&gt;</span> export.csv</pre></td></tr></table></div>

<p>You can export the data as SQL &#8216;INSERT&#8217; statements you need to use the &#8216;-I&#8217; flag.</p>

<div class="wp_codebox"><table><tr id="p291716"><td class="code" id="p2917code16"><pre class="bash" style="font-family:monospace;">mdb-export <span style="color: #660033;">-I</span> database.mdb table_name <span style="color: #000000; font-weight: bold;">&gt;</span> export.sql</pre></td></tr></table></div>

<p>But there is one problem here. All the export statements end with a newline and not a semi-colon as MySQL expects, so we need to add one. The following line shows how with the help of the &#8216;-R&#8217; flag. This flag lets you specify a row delimiter.</p>

<div class="wp_codebox"><table><tr id="p291717"><td class="code" id="p2917code17"><pre class="bash" style="font-family:monospace;">mdb-export <span style="color: #660033;">-I</span> <span style="color: #660033;">-R</span> <span style="color: #ff0000;">';'</span> database.mdb table_name <span style="color: #000000; font-weight: bold;">&gt;</span> export.sql</pre></td></tr></table></div>

<p>This will now end all INSERT statements with a semi-colon instead of a newline. This is fine for small data, but for a huge number of INSERT statements this can be a problem as all the statements are now on one single line ( the newlines are replaced by the semi-colon). To solve this problem we will need to use &#8216;sed&#8217; to insert the semi-color before the ending newline and the closing parenthesis of the INSERT statement. This is shown below.</p>

<div class="wp_codebox"><table><tr id="p291718"><td class="code" id="p2917code18"><pre class="bash" style="font-family:monospace;">mdb-export <span style="color: #660033;">-I</span> database.mdb table_name <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/)$/)\;/'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> export.sql</pre></td></tr></table></div>

<h4>Using Toad for MySQL to import CSV</h4>
<p>For my data import job I needed to import a select few columns from a particular table to MySQL. mdb-export does not allow me to select columns from a table, so I needed to find another way to import the data to MySQL. As I already had <a href="http://toadformysql.com/index.jspa" rel="nofollow"  title="Toad for MySQL" target="_blank">Toad MySQL</a> installed I decided to use it.</p>
<p>First I exported the data from the MDB table to a CSV file and then used the Toad MySQL importer to import particular columns from the CSV file to my new MySQL database. The Toad importer lets you map columns from the CSV file to the MySQL schema as shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/09/toad-mysql-import.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/09/toad-mysql-import-300x201.gif" alt="" title="toad-mysql-import" width="300" height="201" class="aligncenter size-medium wp-image-2919" /></a><br />
<em>Click to enlarge</em></p>
<p>For large files the Toad importer can take a while to complete. If you have a Toad version prior to 6.0 then upgrade it to the latest version. On my previous version of 5.x the importer used to crash before writing the imported data to the disk; the bug seems to have been resolved in the new version.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/migrating-access-mdb-to-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Semantic markup for better search results</title>
		<link>http://www.codediesel.com/data/semantic-markup-for-better-search-results/</link>
		<comments>http://www.codediesel.com/data/semantic-markup-for-better-search-results/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 03:53:35 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[semantic web]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2867</guid>
		<description><![CDATA[Search-engines have reached a impasse with respect to understanding of HTML data. Although most sites, especially databases centric sites use structured data in the backend, once the data is rendered as HTML the data looses all its structure and makes it difficult for search engines to understand them correctly. Until now most search engines have [...]]]></description>
			<content:encoded><![CDATA[<p>Search-engines have reached a impasse with respect to understanding of HTML data. Although most sites, especially databases centric sites use structured data in the backend, once the data is rendered as HTML the data looses all its structure and makes it difficult for search engines to understand them correctly. Until now most search engines have relied on extracting keyword text from the web pages and ranking the pages accordingly, taking the context around the keyword into consideration. This obviously has its limitations and it can only take you so far.<br />
<span id="more-2867"></span><br />
Proper markup allows a web developer to add meaning or semantics to a page, but HTML has very little vocabulary which can help a person add semantics to a page. To help in this Endeavor, Microsoft, Yahoo and Google have released a shared vocabulary on <a href="http://schema.org" rel="nofollow" target="_blank" >schema.org</a>, which along with the <a href="http://dev.w3.org/html5/md/Overview.html" rel="nofollow" target="_blank" >microdata</a> format can help web developers add proper semantics to their data. This will help search engines to properly extract meaning from a web page which will enable them to provide richer search results, increased visibility of your web pages and possibly increased ranking. When three major search rivals collaborate on something you can be sure of the importance of it.</p>
<p>Below is a sample markup using the schema.org vocabulary and microdata.</p>
<p><strong>Original</strong>.</p>

<div class="wp_codebox"><table><tr id="p286721"><td class="code" id="p2867code21"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;dell-30in-lcd.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
Dell UltraSharp 30&quot; LCD Monitor
&nbsp;
87 out of 100 based on 24 user ratings
&nbsp;
$1250 to $1495 from 8 sellers
&nbsp;
Sellers:
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;save-a-lot-monitors.com/dell-30.html&quot;</span>&gt;</span>
  Save A Lot Monitors - $1250<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jondoe-gadgets.com/dell-30.html&quot;</span>&gt;</span>
  Jon Doe's Gadgets - $1350<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

<p><strong>With semantic markup added.</strong></p>

<div class="wp_codebox"><table><tr id="p286722"><td class="code" id="p2867code22"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> itemscope itemtype<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://schema.org/Product&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;dell-30in-lcd.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span>&gt;</span>Dell UltraSharp 30&quot; LCD Monitor<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;aggregateRating&quot;</span> </span>
<span style="color: #009900;">    itemscope itemtype<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://schema.org/AggregateRating&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ratingValue&quot;</span>&gt;</span>87<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
    out of <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bestRating&quot;</span>&gt;</span>100<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
    based on <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ratingCount&quot;</span>&gt;</span>24<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span> user ratings
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;offers&quot;</span> </span>
<span style="color: #009900;">    itemscope itemtype<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://schema.org/AggregateOffer&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lowPrice&quot;</span>&gt;</span>$1250<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
    to <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;highPrice&quot;</span>&gt;</span>$1495<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
    from <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;offerCount&quot;</span>&gt;</span>8<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span> sellers
&nbsp;
  Sellers:
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;offers&quot;</span> itemscope itemtype<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://schema.org/Offer&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;save-a-lot-monitors.com/dell-30.html&quot;</span>&gt;</span>
     Save A Lot Monitors - $1250<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;offers&quot;</span> itemscope itemtype<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://schema.org/Offer&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> itemprop<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jondoe-gadgets.com/dell-30.html&quot;</span>&gt;</span>
     Jon Doe's Gadgets - $1350<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  ...
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Although this may seem a lot of extra work and time, the efforts will pay handsomely over a period as more people adopt semantic markup and companies come up with better  tools and APIs to work with the new data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/semantic-markup-for-better-search-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>International Airport codes download</title>
		<link>http://www.codediesel.com/data/international-airport-codes-download/</link>
		<comments>http://www.codediesel.com/data/international-airport-codes-download/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 11:11:57 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/international-airport-codes/</guid>
		<description><![CDATA[During a recent project I needed a database of IATA airport codes. Airports around the world are universally known by a unique three-letter code: the &#8220;International Air Transport Association (IATA) Location Identifier&#8221;. It is much easier for pilots, travel agents, frequent flyers, baggage handlers and anyone having anything to do with airlines to say and [...]]]></description>
			<content:encoded><![CDATA[<p>During a recent project I needed a database of IATA airport codes. Airports around the world are universally known by a unique three-letter code: the &#8220;International Air Transport Association (IATA) Location Identifier&#8221;. It is much easier for pilots, travel agents, frequent flyers, baggage handlers and anyone having anything to do with airlines to say and write <em>LGA</em> then the full &#8220;<em>New York, NY &#8211; La Guardia Airport</em>&#8220;.<br />
<span id="more-2850"></span><br />
A sample is shown below.</p>

<div class="wp_codebox"><table><tr id="p285024"><td class="code" id="p2850code24"><pre class="text" style="font-family:monospace;">Airport			Code
Aalborg, Denmark 	AAL
Aalesund, Norway	AES
Aasiaat, Greenland 	JEG
Abadan, Iran 		ABD
Abakan, Russia		ABA
.
.</pre></td></tr></table></div>

<p>After scouting around for a while I stumbled upon a HTML list at <a href="http://www.orbitz.com/App/global/airportCodes.jsp" rel="nofollow" target="_blank" >orbitz</a>. To make it usable in PHP I further converted the data to various formats (CSV, SQL, XML) for ease of use. The complete list of files is available below for download. The files contain codes for 3592 international airports sorted by name. This can be useful if your web application needs to convert airport names to their respective IATA codes.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/airport-codes" rel="nofollow" >Download IATA Airport Codes (sql, csv, xml)</a><br />
<span>Downloads : 601  / File size : 120 kB</span>
</div>
<p>Further Reading:<br />
<a href="http://www.skygod.com/asstd/abc.html" rel="nofollow" target="_blank" >Airport ABCs: An Explanation of Airport Identifier Codes</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/international-airport-codes-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unpacking binary data in PHP</title>
		<link>http://www.codediesel.com/php/unpacking-binary-data/</link>
		<comments>http://www.codediesel.com/php/unpacking-binary-data/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 09:38:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2707</guid>
		<description><![CDATA[To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension [...]]]></description>
			<content:encoded><![CDATA[<p>To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension the file may have. We are not to use any GD library functions.</p>
<h4>A GIF file header</h4>
<p>With the requirement that we are not allowed to use any graphics functions, to solve the problem we need to get the relevant data from the GIF file itself. Unlike a HTML or XML or other text format files, a GIF file and most other image formats are stored in a binary format. Most binary files carry a header at the top of the file which provides the meta information regarding the particular file. We can use this information to find out the type of the file and other things, such as height an width in case of a GIF file. A typical raw GIF header is shown below, using a hex editor such as <a href="http://www.x-ways.net/winhex/index-m.html" rel="nofollow" target="_blank" >WinHex</a>.<br />
<span id="more-2707"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2010/09/winhex.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/09/winhex.gif" alt="" title="winhex" width="435" height="82" class="aligncenter size-full wp-image-2709" /></a></p>
<p>The detailed description of the header is given below.</p>

<div class="wp_codebox"><table><tr id="p270733"><td class="code" id="p2707code33"><pre class="text" style="font-family:monospace;">Offset   Length   Contents
  0      3 bytes  &quot;GIF&quot;
  3      3 bytes  &quot;87a&quot; or &quot;89a&quot;
  6      2 bytes  &lt;Logical Screen Width&gt;
  8      2 bytes  &lt;Logical Screen Height&gt;
 10      1 byte   bit 0:    Global Color Table Flag (GCTF)
                  bit 1..3: Color Resolution
                  bit 4:    Sort Flag to Global Color Table
                  bit 5..7: Size of Global Color Table: 2^(1+n)
 11      1 byte   &lt;Background Color Index&gt;
 12      1 byte   &lt;Pixel Aspect Ratio&gt;
 13      ? bytes  &lt;Global Color Table(0..255 x 3 bytes) if GCTF is one&gt;
         ? bytes  &lt;Blocks&gt;
         1 bytes  &lt;Trailer&gt; (0x3b)</pre></td></tr></table></div>

<p>So to check if the image file is a valid GIF, we need to check the starting 3 bytes of the header, which have the &#8216;GIF&#8217; marker, and the next 3 bytes, which give the version number; either &#8217;87a&#8217; or &#8217;89a&#8217;. It is for tasks such as these that the unpack() function is indispensable. Before we look at the solution, we will take a quick look at the unpack() function itself.</p>
<h4>Using the unpack() function</h4>
<p><a href="http://php.net/manual/en/function.unpack.php" rel="nofollow" target="_blank" >unpack()</a> is the complement of <a href="http://www.php.net/manual/en/function.pack.php" rel="nofollow" target="_blank" >pack()</a> &#8211; it transforms binary data into an associative array based on the format specified. It is somewhat along the lines of <em>sprintf</em>, transforming string data according to some given format. These two functions allow us to read and write buffers of binary data according to a specified format string. This easily enables a programmer to exchange data with programs written in other languages or other formats. Take a small example.</p>

<div class="wp_codebox"><table><tr id="p270734"><td class="code" id="p2707code34"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'C*'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'codediesel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will print the following, decimal codes for &#8216;codediesel&#8217; :</p>

<div class="wp_codebox"><table><tr id="p270735"><td class="code" id="p2707code35"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span>
  <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">99</span>
  <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">111</span>
  <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">100</span>
  <span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">101</span>
  <span style="color: #cc66cc;">5</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">100</span>
  <span style="color: #cc66cc;">6</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">105</span>
  <span style="color: #cc66cc;">7</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">101</span>
  <span style="color: #cc66cc;">8</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">115</span>
  <span style="color: #cc66cc;">9</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">101</span>
  <span style="color: #cc66cc;">10</span> <span style="color: #339933;">=&gt;</span> int <span style="color: #cc66cc;">108</span></pre></td></tr></table></div>

<p>In the above example the first argument is the format string and the second the actual data. The format string specifies how the data argument should be parsed. In this example the first part of the format &#8216;C&#8217;, specifies that we should treat the first character of the data as a unsigned byte. The next part &#8216;*&#8217;, tells the function to apply the previously specified format code to all the remaining characters.</p>
<p>Although this may look confusing, the next section provides a concrete example.</p>
<h4>Grabbing the header data</h4>
<p>Below is the  solution to our GIF problem using the unpack() function. The  <em>is_gif()</em> function will return true if the given file is in a GIF format.</p>

<div class="wp_codebox"><table><tr id="p270736"><td class="code" id="p2707code36"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_gif<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_file</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Open the image file in binary mode */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Read 20 bytes from the top of the file */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Create a format specifier */</span>
    <span style="color: #000088;">$header_format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A6version'</span><span style="color: #339933;">;</span>  <span style="color: #009933; font-style: italic;"># Get the first 6 bytes
</span>
    <span style="color: #009933; font-style: italic;">/* Unpack the header data */</span>
    <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$header_format</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'version'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF87a'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF89a'</span><span style="color: #009900;">&#41;</span>? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Run our example */</span>
<span style="color: #000000; font-weight: bold;">echo</span> is_gif<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aboutus.gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The important line to note is the format specifier. The &#8216;A6&#8242; characters specifies that the unpack() function is to get the the first 6 bytes of the data and interpret it as a string. The retrieved data is then stored in an associate array with the key named &#8216;version&#8217;.</p>
<p>Another example is given below. This returns some additional header data of the GIF file, including the image width and height.</p>

<div class="wp_codebox"><table><tr id="p270737"><td class="code" id="p2707code37"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_gif_header<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_file</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Open the image file in binary mode */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Read 20 bytes from the top of the file */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Create a format specifier */</span>
    <span style="color: #000088;">$header_format</span> <span style="color: #339933;">=</span> 
            <span style="color: #0000ff;">'A6Version/'</span> <span style="color: #339933;">.</span> <span style="color: #009933; font-style: italic;"># Get the first 6 bytes
</span>            <span style="color: #0000ff;">'C2Width/'</span> <span style="color: #339933;">.</span>   <span style="color: #009933; font-style: italic;"># Get the next 2 bytes
</span>            <span style="color: #0000ff;">'C2Height/'</span> <span style="color: #339933;">.</span>  <span style="color: #009933; font-style: italic;"># Get the next 2 bytes
</span>            <span style="color: #0000ff;">'C1Flag/'</span> <span style="color: #339933;">.</span>    <span style="color: #009933; font-style: italic;"># Get the next 1 byte
</span>            <span style="color: #0000ff;">'@11/'</span> <span style="color: #339933;">.</span>       <span style="color: #009933; font-style: italic;"># Jump to the 12th byte
</span>            <span style="color: #0000ff;">'C1Aspect'</span><span style="color: #339933;">;</span>    <span style="color: #009933; font-style: italic;"># Get the next 1 byte
</span>
    <span style="color: #009933; font-style: italic;">/* Unpack the header data */</span>
    <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$header_format</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Version'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF87a'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$ver</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'GIF89a'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$header</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Run our example */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>get_gif_header<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aboutus.gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above example will print the following when run.</p>

<div class="wp_codebox"><table><tr id="p270738"><td class="code" id="p2707code38"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>Version<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> GIF89a
    <span style="color: #009900;">&#91;</span>Width1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">97</span>
    <span style="color: #009900;">&#91;</span>Width2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #009900;">&#91;</span>Height1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">33</span>
    <span style="color: #009900;">&#91;</span>Height2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #009900;">&#91;</span>Flag<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">247</span>
    <span style="color: #009900;">&#91;</span>Aspect<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Below we will go into the details of how the format specifier works. I&#8217;ll split the format, giving the details for each character.</p>

<div class="wp_codebox"><table><tr id="p270739"><td class="code" id="p2707code39"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$header_format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A6Version/C2Width/C2Height/C1Flag/@11/C1Aspect'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p270740"><td class="code" id="p2707code40"><pre class="text" style="font-family:monospace;">A - Read a byte and interpret it as a string. 
    Number of bytes to read is given next
6 - Read a total of 6 bytes, starting from position 0
Version - Name of key in the associative array where data 
    retrieved by 'A6' is stored
&nbsp;
/ - Start a new code format
C - Interpret the next data as an unsigned byte
2 - Read a total of 2 bytes
Width - Key in the associative array
&nbsp;
/ - Start a new code format
C - Interpret the data as an unsigned byte
2 - Read a total of 2 bytes
Height- Key in the associative array
&nbsp;
/ - Start a new code format
C - Interpret the data as an unsigned byte
1 - Read a total of 2 bytes
Flag - Key in the associative array
&nbsp;
/ - Start a new code format
@ - Move to the byte offset specified by the following number.
      Remember that the first position in the binary string is 0. 
11 - Move to position 11
&nbsp;
/ - Start a new code format
C - Interpret the data as an unsigned byte
1 - Read a total of 1 bytes
Aspect - Key in the associative array</pre></td></tr></table></div>

<p>More format options can be found <a href="http://www.php.net/manual/en/function.pack.php" rel="nofollow" target="_blank" >here</a>. Although I&#8217;ve only shown a small example, the pack/unpack is capable of much complex work than presented here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/unpacking-binary-data/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Raw vs. cooked PHP $_POST variables</title>
		<link>http://www.codediesel.com/php/raw-vs-cooked-php-post-variables/</link>
		<comments>http://www.codediesel.com/php/raw-vs-cooked-php-post-variables/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 08:25:13 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[post data]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2703</guid>
		<description><![CDATA[A little quirk of PHP $_POST var I encountered while fixing a Salesforce web-to-Lead bug. A WordPress site was using a form to submit user requests to the Salesforce web-service. The form that submitted the data had the following fields along with the others. The problem was with the multi-select field, only the last value [...]]]></description>
			<content:encoded><![CDATA[<p>A little quirk of PHP $_POST var I encountered while fixing a Salesforce web-to-Lead bug. A WordPress site was using a form to submit user requests to the Salesforce web-service. The form that submitted the data had the following fields along with the others. The problem was with the multi-select field, only the last value selected in the multi-select was getting captured.<br />
<span id="more-2703"></span></p>

<div class="wp_codebox"><table><tr id="p270350"><td class="code" id="p2703code50"><pre class="html" style="font-family:monospace;">.
.
&lt;select  id=&quot;days&quot; multiple=&quot;multiple&quot; name=&quot;days&quot; &gt;
&lt;option value=&quot;Monday&quot;&gt;Monday&lt;/option&gt;
&lt;option value=&quot;Tuesday&quot;&gt;Tuesday&lt;/option&gt;
&lt;option value=&quot;Wednesday&quot;&gt;Wednesday&lt;/option&gt;
&lt;option value=&quot;Thursday&quot;&gt;Thursday&lt;/option&gt;
&lt;option value=&quot;Friday&quot;&gt;Friday&lt;/option&gt;
&lt;option value=&quot;Saturday&quot;&gt;Saturday&lt;/option&gt;
&lt;option value=&quot;Sunday&quot;&gt;Sunday&lt;/option&gt;
 &lt;/select&gt;
.
.</pre></td></tr></table></div>

<p>After looking at the php code I found the problem, it was sending the values for the &#8216;days&#8217; field using the following line.</p>

<div class="wp_codebox"><table><tr id="p270351"><td class="code" id="p2703code51"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;days&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$_POST</span>['days']; ?&gt;&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Which I naturally changed to the following. Note the array specifier for &#8216;days&#8217;.</p>

<div class="wp_codebox"><table><tr id="p270352"><td class="code" id="p2703code52"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'days'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$day</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>                
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;input type=&quot;hidden&quot; name=&quot;days[]&quot; value=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$day</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>and the html to:</p>

<div class="wp_codebox"><table><tr id="p270353"><td class="code" id="p2703code53"><pre class="html" style="font-family:monospace;">&lt;select  id=&quot;days&quot; multiple=&quot;multiple&quot; name=&quot;days[]&quot; &gt;
.
.</pre></td></tr></table></div>

<p>But which surprisingly <em>did not work</em>. Salesforce was still showing only the last value of the multi-select. After spending some confused time over the issue, it struck me that maybe the endpoint at Salesforce was using raw http post data. The post data that we get in the PHP $_POST variable is a &#8216;cooked&#8217; version of the actual data sent by the browser. I say &#8216;cooked&#8217; because PHP neatly stores the retrieved data in an associative array, ready for our easy consumption. I wrongly assumed that Salesforce was using the same $_POST variable (or the variants in whatever language they use for the backend) to access post data.</p>
<p>I removed the field array specifier [] as given below, and the code worked.</p>

<div class="wp_codebox"><table><tr id="p270354"><td class="code" id="p2703code54"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'days'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$day</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>                
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;input type=&quot;hidden&quot; name=&quot;days&quot; value=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$day</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The point here being that we are so accustomed to one way of doing things, that when something different comes along it takes us by surprise.</p>
<h4>Raw vs. cooked POST data in PHP</h4>
<p>Here is a little more on the php $_POST var. Say you have something like the following HTML:</p>
<p><strong>Example 1.</strong></p>

<div class="wp_codebox"><table><tr id="p270355"><td class="code" id="p2703code55"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;number&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;number&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;number&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;3&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sam&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>If you do a var_dump($_POST) on the above data, you will get the following.</p>

<div class="wp_codebox"><table><tr id="p270356"><td class="code" id="p2703code56"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span>
  <span style="color: #0000ff;">'number'</span> <span style="color: #339933;">=&gt;</span> string <span style="color: #0000ff;">'3'</span> <span style="color: #009900;">&#40;</span>length<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
  <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> string <span style="color: #0000ff;">'sam'</span> <span style="color: #009900;">&#40;</span>length<span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p> Notice that the &#8216;number&#8217; field takes the last value of the three. Because as you know, PHP converts the POST variables in an associative array. So the last value overwrites the previous values in the &#8216;number&#8217; field.</p>
<p>Whereas if you read the POST data using the following:</p>

<div class="wp_codebox"><table><tr id="p270357"><td class="code" id="p2703code57"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$postdata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php://input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$postdata</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You will get all the fields, although in an raw encoded form and not in an array. Below is the var_dump of the same.</p>

<div class="wp_codebox"><table><tr id="p270358"><td class="code" id="p2703code58"><pre class="php" style="font-family:monospace;">string <span style="color: #0000ff;">'number=1&amp;number=2&amp;number=3&amp;name=sam'</span> <span style="color: #009900;">&#40;</span>length<span style="color: #339933;">=</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>So in conclusion, if you are working with POST data, do not assume that the program on the other end will process the data the same way as you do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/raw-vs-cooked-php-post-variables/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Generating random data on the client side</title>
		<link>http://www.codediesel.com/data/generating-random-data-on-the-client-side/</link>
		<comments>http://www.codediesel.com/data/generating-random-data-on-the-client-side/#comments</comments>
		<pubDate>Fri, 21 May 2010 10:07:31 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2658</guid>
		<description><![CDATA[Automatically creating fake or sample data is a frequent requirement for front-end web developers. Although usually not tedious, there are times when you need to quickly and automatically generate structured data for your html forms or CMS systems for testing purposes. Faker.js is a JavaScript implementation inspired by Benjamin Curtis&#8217;s Ruby Gem Faker and Perl&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Automatically creating fake or sample data is a frequent requirement for front-end web developers. Although usually not tedious, there are times when you need to quickly and automatically generate structured data for your html forms or CMS systems for testing purposes.</p>
<p><a href="http://github.com/Marak/Faker.js" rel="nofollow" target="_blank" >Faker.js</a> is a JavaScript implementation inspired by Benjamin Curtis&#8217;s Ruby Gem <a href="http://faker.rubyforge.org/" rel="nofollow" >Faker</a> and Perl&#8217;s <a href="http://search.cpan.org/~jasonk/Data-Faker-0.07/lib/Data/Faker.pm" rel="nofollow" >Data::Faker</a> that lets you generate commonly required data quickly. You can check the <a href="http://maraksquires.com/Faker.js/" rel="nofollow" >demo page</a> to get an idea.<br />
<span id="more-2658"></span></p>
<h4>Getting Fake.js</h4>
<p>You can download the library form <a href="http://github.com/Marak/Faker.js" rel="nofollow" >github</a> and include in your pages where you need to generate the data.</p>

<div class="wp_codebox"><table><tr id="p265867"><td class="code" id="p2658code67"><pre class="javascript" style="font-family:monospace;">..
<span style="color: #339933;">&lt;</span>script src <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Faker/Faker.js&quot;</span> type <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
..</pre></td></tr></table></div>

<h4>Generating fake data</h4>
<p>The API is divided into various sections as show below:</p>

<div class="wp_codebox"><table><tr id="p265868"><td class="code" id="p2658code68"><pre class="text" style="font-family:monospace;"># Name
    * findName
# Address
    * zipCode
    * city
    * streetName
    * streetAddress
    * secondaryAddress
    * ukCounty
    * ukCountry
# PhoneNumber
    * phoneNumber
# Internet
    * email
    * userName
    * domainName
    * domainWord
...</pre></td></tr></table></div>

<p>So if you need to generate a random email address or a zip code you could do it like this:</p>

<div class="wp_codebox"><table><tr id="p265869"><td class="code" id="p2658code69"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> randomEmail <span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Internet</span>.<span style="color: #660066;">email</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> randomZip <span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Address</span>.<span style="color: #660066;">zipCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Some sample API methods are shown below, but there are more of them which you can find <a href="http://github.com/Marak/Faker.js" rel="nofollow" target="_blank" >here</a> in the API section.</p>

<div class="wp_codebox"><table><tr id="p265870"><td class="code" id="p2658code70"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> longParagraph <span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Lorem</span>.<span style="color: #660066;">paragraphs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> singleSentence <span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Lorem</span>.<span style="color: #660066;">sentence</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> companyName<span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Company</span>.<span style="color: #660066;">companyName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> catchPhrase <span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Company</span>.<span style="color: #660066;">catchPhrase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>One of the interesting methods is &#8216; Faker.Company.catchPhrase()&#8217;, which generates catchy technological phrases. A kind of a tag line generator for your new multi-million dollar company <img src='http://www.codediesel.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . A sample output is shown below.</p>

<div class="wp_codebox"><table><tr id="p265871"><td class="code" id="p2658code71"><pre class="text" style="font-family:monospace;">Stand-alone 5th generation utilisation
Ergonomic explicit focus group
Horizontal human-resource solution
Automated contextually-based knowledge base
Distributed multi-state encoding
Extended zero administration interface
Proactive coherent productivity
Streamlined national approach
Total transitional algorithm
Stand-alone fault-tolerant moderator</pre></td></tr></table></div>

<p>The API also includes several helper methods that lets you create bulk fake data using the single API methods. For example you can generate complete user information templates cards by using a single helper method <span class="method">&#8216;Faker.Helpers.userCard()&#8217;</span>. A sample run is shown below:</p>

<div class="wp_codebox"><table><tr id="p265872"><td class="code" id="p2658code72"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">name</span><span style="color: #339933;">:</span> Blair Nikolaus
username<span style="color: #339933;">:</span> Fernando_Olson
email<span style="color: #339933;">:</span> Jacklyn_Brekke<span style="color: #339933;">@</span>aurore.<span style="color: #000066;">name</span>
address<span style="color: #339933;">:</span>street<span style="color: #339933;">:</span> Kozey Meadow
suite<span style="color: #339933;">:</span> Apt. <span style="color: #CC0000;">664</span>
city<span style="color: #339933;">:</span> Felipastad
zipcode<span style="color: #339933;">:</span> <span style="color: #CC0000;">88071</span>
&nbsp;
phone<span style="color: #339933;">:</span> 571.540.8605 x136
website<span style="color: #339933;">:</span> reba.<span style="color: #660066;">co</span>.<span style="color: #660066;">uk</span>
company<span style="color: #339933;">:</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span> Kuhic and Sons
catchPhrase<span style="color: #339933;">:</span> Integrated solution<span style="color: #339933;">-</span>oriented Graphical User Interface
bs<span style="color: #339933;">:</span> seize scalable web services</pre></td></tr></table></div>

<p>The &#8216;Faker.Helpers.userCard()&#8217; method returns a nested object, so you will need to recursively traverse the object to extract the data. A simple function to do the same is given below:</p>

<div class="wp_codebox"><table><tr id="p265873"><td class="code" id="p2658code73"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #006600; font-style: italic;">// Recursively traverse a nested Javascript object and return it.</span>
<span style="color: #003366; font-weight: bold;">function</span> printObj<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> prop<span style="color: #339933;">,</span> res <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>prop <span style="color: #000066; font-weight: bold;">in</span> obj<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> obj<span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        res <span style="color: #339933;">+=</span> prop <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> printObj<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
        res <span style="color: #339933;">+=</span> prop <span style="color: #339933;">+</span> <span style="color: #3366CC;">': '</span> <span style="color: #339933;">+</span> obj<span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">return</span> res<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>So now you can do something like this:</p>

<div class="wp_codebox"><table><tr id="p265874"><td class="code" id="p2658code74"><pre class="javascript" style="font-family:monospace;">...
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> Faker.<span style="color: #660066;">Helpers</span>.<span style="color: #660066;">userCard</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> printObj<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...</pre></td></tr></table></div>

<h4>Another Randomizer library</h4>
<p><a href="http://code.google.com/p/clubajax/source/browse/" rel="nofollow" target="_blank" >Club AJAX Mock Data Randomizer</a> library is another library that lets you generate random data quickly. This library lets you generate random dates, colors, boolean values which is lacking in the Faker library. Check a simple <a href="http://www.codediesel.com/data/scripts/random/index.html">demo</a> using this library.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/generating-random-data-on-the-client-side/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sorting PHP arrays with substrings</title>
		<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/</link>
		<comments>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 12:35:17 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2141</guid>
		<description><![CDATA[Sorting PHP arrays using usort with substrings]]></description>
			<content:encoded><![CDATA[<p>A couple of days back I encountered the following request on a forum for sorting a single dimensional array. The programmer wanted to sort the following array by the substring after the colon. For example &#8216;CDF&#8217; in the string &#8217;66345:CDF&#8217;. Those values that do not have any colon should be ignored and pushed to the end of the array. Also, the length of the strings are not constant.</p>

<div class="wp_codebox"><table><tr id="p214175"><td class="code" id="p2141code75"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;66345:CDF&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;61179:HGT&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;64146:ABA&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;68768:BNG&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68015:ZCZ&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80231:LPO&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;64146:QWP&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68736:HHB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;86801:MNV&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;80178:OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80178:ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;88178:BRT&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;801782OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;801378ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;881578BRT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-2141"></span></p>
<p>A quick solution was the following. But I&#8217;m sure that this is not the fastest, as a matter of fact it is quite slow for larger arrays.</p>

<div class="wp_codebox"><table><tr id="p214176"><td class="code" id="p2141code76"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;66345:CDF&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;61179:HGT&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;64146:ABA&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;68768:BNG&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68015:ZCZ&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80231:LPO&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;64146:QWP&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68736:HHB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;86801:MNV&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;80178:OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80178:ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;88178:BRT&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;801782OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;801378ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;881578BRT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_cmp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pieces_a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pieces_b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #990000;">strcasecmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;my_cmp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what will be the fastest algorithm for the same?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Test data generation for MySQL</title>
		<link>http://www.codediesel.com/data/test-data-generation-for-mysql/</link>
		<comments>http://www.codediesel.com/data/test-data-generation-for-mysql/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 09:28:16 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1833</guid>
		<description><![CDATA[generating test data for mysql]]></description>
			<content:encoded><![CDATA[<p>One of the most time consuming steps in developing a database based application is test data generation. Imagine you have created a shopping cart application for your client, containing dozens of tables and hundreds of fields. Everything is ready for testing, but the problem is your database is empty. You at least require a modicum of test data in the database to proceed with the testing. You can choose to manually enter a small set of random data into the database, but you lose a couple of precious days in the process. What you require is a data generator that automatically generates test data for your database.<br />
<span id="more-1833"></span><br />
There are a couple of data generators available for free on the web, but they are only suitable if your database schema is small, with no more than three to four tables and total number of fields not exceeding  a dozen or so.  While browsing I came across <a href="http://www.upscene.com/products.adg.index.php" rel="nofollow" target="_blank" >Advanced Data Generator (ADG)</a>, a test data generator for MySQL. ADG allows you to generate a random test data for dates, URLS, E-mail addresses, cities, names, countries and more. </p>
<h4>Data Generation</h4>
<p>Once you connect ADG to your MySQL server and register a database, you are ready to go. Generating the test data is quite simple. First you create a project for your data generation work. Once done, you select the table and fields you want to generate data for, select the type of test data to generate – dates, urls, names etc. Set the total number of records to generate and then run the project. This will create the test data for your database project. The test data can be saved directly to the connected database or you can save it as a separate SQL script, YAML or CSV file.  Below is a sample screenshot of a data generation project.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/11/adg1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/11/adg1-300x267.gif" alt="adg1" title="adg1" width="300" height="267" class="aligncenter size-medium wp-image-1832" /></a></p>
<p>The Advanced Data Generator comes with a very useful repository of real-life like data, in order to generate meaningful test-data for the database. You can also use custom SQL scripts or Macros to generate custom string data.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/11/adg2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/11/adg2-295x300.gif" alt="adg2" title="adg2" width="295" height="300" class="aligncenter size-medium wp-image-1838" /></a></p>
<h4>In closing</h4>
<p>Although not free, its a small price to pay for the excellent automation it provides during database creation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/test-data-generation-for-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Beautifying XML documents</title>
		<link>http://www.codediesel.com/pear/beautifying-xml-documents/</link>
		<comments>http://www.codediesel.com/pear/beautifying-xml-documents/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 04:48:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1590</guid>
		<description><![CDATA[how to beautify and format xml documents]]></description>
			<content:encoded><![CDATA[<p>I frequently write php code to access various web services and the most common response data I encounter is in xml, which most of the time is not formatted. I use <a href="http://www.wmhelp.com/" rel="nofollow" target="_blank" >xmlPad</a> to format and analyze xml documents, but many times I need to format xml documents on the production server wherein xmlPad is of no use. What one needs is a library that would allow you to beautify your untidy xml documents within your php code. <a href="http://pear.php.net/manual/en/package.xml.xml-beautifier.php" rel="nofollow" target="_blank" >XML_Beautifier</a> provides that solution.<br />
<span id="more-1590"></span></p>
<h4>Installation</h4>
<p>XML_Beautifier being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than dowloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p159077"><td class="code" id="p1590code77"><pre class="text" style="font-family:monospace;">pear install XML_Beautifier</pre></td></tr></table></div>

<h4>Usage</h4>
<p>You can beautify a xml file or a on-the-fly generated xml string. The following will nicely format a untidy xml file. The input file must be a valid xml document or the parser will flag an error.</p>

<div class="wp_codebox"><table><tr id="p159078"><td class="code" id="p1590code78"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'unformated.xml'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beautified.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Formatting a xml string is quite as simple.</p>

<div class="wp_codebox"><table><tr id="p159079"><td class="code" id="p1590code79"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Unformatted xml string */</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;rootNode&gt;&lt;foo   bar = &quot;pear&quot;&gt;hello world!&lt;/foo&gt;&lt;/rootNode&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Formatted xml string.</p>

<div class="wp_codebox"><table><tr id="p159080"><td class="code" id="p1590code80"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rootNode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">bar</span>=<span style="color: #ff0000;">&quot;pear&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>hello world!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rootNode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you frequently need to format xml documents than it would be better to have a command line access to the beautifier as below.</p>

<div class="wp_codebox"><table><tr id="p159081"><td class="code" id="p1590code81"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/** xmlformat.php
 *  usage: php xmlformat.php untidy.xml beautified.xml
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$argc</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Usage: <span style="color: #006699; font-weight: bold;">$argv[0]</span> unformatted.xml formatted.xml<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Formatting <span style="color: #006699; font-weight: bold;">{$argv[1]}</span>...&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Options</h4>
<p>The <em>XML_Beautifier</em> class also accepts a array of options which you can pass to the constructor during initialization. The details of the options can be found <a href="http://pear.php.net/manual/en/package.xml.xml-beautifier.options.php" rel="nofollow" target="_blank" >here</a>.</p>

<div class="wp_codebox"><table><tr id="p159082"><td class="code" id="p1590code82"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">&quot;caseFolding&quot;</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;caseFoldingTo&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;uppercase&quot;</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;normalizeComments&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>One thing to note in closing is that the formatting of big xml documents can take some time depending on your machine configuration, so if you plan on formatting documents on a live server you need to take that into account.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/beautifying-xml-documents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using barcodes in your web application</title>
		<link>http://www.codediesel.com/php/using-barcodes-in-your-web-application/</link>
		<comments>http://www.codediesel.com/php/using-barcodes-in-your-web-application/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 04:56:56 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[barcodes]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1518</guid>
		<description><![CDATA[application of bar codes in your web programs]]></description>
			<content:encoded><![CDATA[<p>Brought into the mainstream by supermarket checkout systems, bar codes have become a ubiquitous element in our daily lives. Rarely will one come across any product that doesn&#8217;t have a barcode. The idea of a barcode originated in 1932 from the thesis of Wallace Flint at Harvard.<br />
<span id="more-1518"></span></p>
<h4>Varieties of barcodes</h4>
<p>Barcodes bascially come in two main type: linear (1-dimensional) and Matrix (2-dimensional) with each offering a variety of formats, depending on your application purpose. The barcodes we usually see on books and other standard products are of the linear type. These only encode data in one dimension, from left to right. The following shows a typical UPC barcode.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/09/bar2.png"><img src="http://www.codediesel.com/wp-content/uploads/2009/09/bar2.png" alt="bar2" title="bar2" width="109" height="70" class="aligncenter size-full wp-image-1543" /></a></p>
<p>Matrix or 2-dimensional barcode encode information in both directions (x-y axis). So they are able to store more information in the same space then linear barcodes. There are many type of matrix codes, the one shown below is that of <a href="http://en.wikipedia.org/wiki/QR_Code" rel="nofollow" target="_blank" >QR-Code</a> or Quick Response code. QR-Codes are mostly used for access with cell phones and have become a de-facto standard for Japanese cell phones. Other type of matrix codes include <a href="http://en.wikipedia.org/wiki/ShotCode" rel="nofollow" target="_blank" >ShotCode</a>, <a href="http://en.wikipedia.org/wiki/Semacode" rel="nofollow" target="_blank" >Semacode</a>. The QR-code below shows the string &#8216;codediesel.com&#8217; encoded.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/09/qr_code.png"><img src="http://www.codediesel.com/wp-content/uploads/2009/09/qr_code.png" alt="qr_code" title="qr_code" width="116" height="116" class="aligncenter size-full wp-image-1547" /></a></p>
<p>In this post we will see how to generate linear barcodes using a pear library.</p>
<h4>Installation</h4>
<p>We will be using the &#8216;Image_Barcode&#8217; Pear library to generate linear barcodes. You can download the library manually from <a href="http://pear.php.net/package/Image_Barcode/download" rel="nofollow" target="_blank" >here</a>, or use the pear installer.</p>

<div class="wp_codebox"><table><tr id="p151883"><td class="code" id="p1518code83"><pre class="txt" style="font-family:monospace;">pear install Image_Barcode-1.1.0</pre></td></tr></table></div>

<h4>Sample Code</h4>
<p>A sample code for generating a CODE128 barcode is shown below.</p>

<div class="wp_codebox"><table><tr id="p151884"><td class="code" id="p1518code84"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Barcode.php'</span><span style="color: #339933;">;</span>
   <span style="color: #009933; font-style: italic;">/* Data that will be encoded in the bar code */</span>
   <span style="color: #000088;">$bar_code_data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TRSD5656&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #009933; font-style: italic;">/* The third parameter can accept any from the following,
    * jpg, png and gif.
    */</span>
   Image_Barcode<span style="color: #339933;">::</span><span style="color: #004000;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bar_code_data</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'code128'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>By default, the image generated by the barcode is directly output to the browser. If you do not want this, you can pass &#8216;false&#8217; as the fourth parameter and it will return the GD image resource object; allowing you to do further things with it.</p>

<div class="wp_codebox"><table><tr id="p151885"><td class="code" id="p1518code85"><pre class="php" style="font-family:monospace;">Image_Barcode<span style="color: #339933;">::</span><span style="color: #004000;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bar_code_data</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'code128'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'png'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Supported Types</h4>
<p>The &#8216;Image_Barcode&#8217; library supports the following barcode types:</p>
<ul>
<li>Code 39</li>
<li>Code 128</li>
<li>EAN 13</li>
<li>INT 25</li>
<li>PostNet</li>
<li>UPCA</li>
</ul>
<h4>Some sample generated barcodes</h4>
<p>Some barcodes generated with the above library are shown below. The last one is the United States Postal Service format.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/09/barcode_example.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/09/barcode_example.gif" alt="barcode_example" title="barcode_example" width="557" height="103" class="aligncenter size-full wp-image-1534" /></a><br />
<br clear="left" /></p>
<h4>Google charts and QR-Codes</h4>
<p>Before closing this post we will take a look at generating QR-Codes using Google charts. The following line for example generates the QR-Code shown below. The &#8216;chl&#8217; parameter specifies the data that will be encoded (&#8216;www.codediesel.com&#8217; here).</p>

<div class="wp_codebox"><table><tr id="p151886"><td class="code" id="p1518code86"><pre class="html" style="font-family:monospace;">http://chart.apis.google.com/chart
?cht=qr&amp;chs=150x150&amp;chl=www.codediesel.com</pre></td></tr></table></div>

<p>Which you can embed in your html as below:</p>

<div class="wp_codebox"><table><tr id="p151887"><td class="code" id="p1518code87"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://chart.apis.google.com/chart
?cht=qr&amp;chs=150x150&amp;chl=www.codediesel.com&quot; /&gt;</pre></td></tr></table></div>

<p><a href="http://www.codediesel.com/wp-content/uploads/2009/09/google_chart_qrcode.png"><img src="http://www.codediesel.com/wp-content/uploads/2009/09/google_chart_qrcode.png" alt="google_chart_qrcode" title="google_chart_qrcode" width="150" height="150" class="aligncenter size-full wp-image-1554" /></a></p>
<h4>Testing bar codes</h4>
<p>Ok, now you have created all the barcodes you want, but the problem is you do not have a barcode reader handy; no sweat, <a href="http://www.qualitysoft.de/index_en.html" rel="nofollow" target="_blank" >Qualitysoft.de</a> offers a free barcode testing application for checking a plethora of barcode formats.<br />
<br/><br/></p>
<h4>Additional Resources</h4>
<p>1. <a href="http://qrcode.kaywa.com/" rel="nofollow" target="_blank" >http://qrcode.kaywa.com/</a><br />
2. <a href="http://www.denso-wave.com/qrcode/index-e.html" rel="nofollow" target="_blank" >http://www.denso-wave.com/qrcode/index-e.html</a><br />
3. <a href="http://en.wikipedia.org/wiki/Barcode" rel="nofollow" target="_blank" >http://en.wikipedia.org/wiki/Barcode</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/using-barcodes-in-your-web-application/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

