<?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"
	>

<channel>
	<title>rhythmicalmedia.com</title>
	<atom:link href="http://rhythmicalmedia.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://rhythmicalmedia.com</link>
	<description></description>
	<pubDate>Thu, 13 Aug 2009 17:57:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<item>
		<title>jqGrid</title>
		<link>http://rhythmicalmedia.com/?p=89</link>
		<comments>http://rhythmicalmedia.com/?p=89#comments</comments>
		<pubDate>Thu, 13 Aug 2009 15:58:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[jqGrid]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=89</guid>
		<description><![CDATA[jqGrid is an excellent data grid plugin for jQuery which I wanted to use in my TV Schedule application. It went very smoothly except for the order in which the columns are displayed. jqGrid will display the columns in the order that it receives them. This means that you would have to define the column [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.trirand.com/blog/" target="_blank">jqGrid </a>is an excellent data grid plugin for <a href="http://jquery.com/" target="_blank">jQuery</a> which I wanted to use in my TV Schedule application. It went very smoothly except for the order in which the columns are displayed. jqGrid will display the columns in the order that it receives them. This means that you would have to define the column order in your data service. I did not want to do this because if the UI wanted to change the order it would have no way of doing it. My solution was to pass back an array of columns in the order that I wanted. I use this to sort my data. Not perfect but at least the sort is defined in the jqGrid definition. </p>
<p>In addition I have to convert the data from my model (API or DB) to an object in the format that jqGrid expects that I then json_encode to pass back.</p>
<p>The backend in the case is PHP(cakePHP).</p>
<p>Here is the jqGrid definition.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">    jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ppvList'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jqGrid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/tv_schedules/get_ppv_list'</span><span style="color: #339933;">,</span>
        mtype<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span>
        datatype<span style="color: #339933;">:</span> <span style="color: #3366CC;">'json'</span><span style="color: #339933;">,</span>
	    <span style="color: #006600; font-style: italic;">// This determines the order of columns.  It MUST match the colModel</span>
		postData<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>colOrder<span style="color: #339933;">:</span> <span style="color: #3366CC;">'title,genre,rating,price'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>        
		colModel<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
					<span style="color: #009900;">&#123;</span>label<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Title'</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'title'</span><span style="color: #339933;">,</span> index<span style="color: #339933;">:</span> <span style="color: #3366CC;">'title'</span><span style="color: #339933;">,</span> sortable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
					<span style="color: #009900;">&#123;</span>label<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Genre'</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'genre'</span><span style="color: #339933;">,</span> index<span style="color: #339933;">:</span> <span style="color: #3366CC;">'genre'</span><span style="color: #339933;">,</span> sortable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #3366CC;">'50'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
					<span style="color: #009900;">&#123;</span>label<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Rating'</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'rating'</span><span style="color: #339933;">,</span>index<span style="color: #339933;">:</span> <span style="color: #3366CC;">'rating'</span><span style="color: #339933;">,</span> sortable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #3366CC;">'50'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
					<span style="color: #009900;">&#123;</span>label<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Price'</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'price'</span><span style="color: #339933;">,</span> index<span style="color: #339933;">:</span> <span style="color: #3366CC;">'price'</span><span style="color: #339933;">,</span> sortable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #3366CC;">'25'</span><span style="color: #339933;">,</span> align<span style="color: #339933;">:</span> <span style="color: #3366CC;">'right'</span><span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        pager<span style="color: #339933;">:</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#pager'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        rowNum<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
        rowList<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        sortname<span style="color: #339933;">:</span> <span style="color: #3366CC;">'title'</span><span style="color: #339933;">,</span>
        sortorder<span style="color: #339933;">:</span> <span style="color: #3366CC;">'asc'</span><span style="color: #339933;">,</span>
        viewrecords<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		autowidth<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		height<span style="color: #339933;">:</span> <span style="color: #3366CC;">'auto'</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Here is the data converter.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DataPrepComponent <span style="color: #000000; font-weight: bold;">extends</span> Object
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * This method converts an array in the following format to an object that can be
	 * JSON encoded and passed to jqGrid
	 * 
	 *  Array
		(
		    [0] =&gt; Array
		        (
		            [ModelName] =&gt; Array
		                (
		                    [id] =&gt; 83
		                    [field1] =&gt; value1
		                    [field2] =&gt; value2
		                    [field3] =&gt; value3
		                    [field4] =&gt; value4
		                    [field5] =&gt; value5
		                )
		    [1] =&gt; Array
		        (
		            [ModelName] =&gt; Array
		                (
		                    [id] =&gt; 84
		                    [field1] =&gt; value1
		                    [field2] =&gt; value2
		                    [field3] =&gt; value3
		                    [field4] =&gt; value4
		                    [field5] =&gt; value5
		                )
&nbsp;
&nbsp;
		)
&nbsp;
		This array will be converted to the following for jQgrid.
&nbsp;
		object(stdClass)
		  public 'total' =&gt; int
		  public 'page' =&gt; int
		  public 'records' =&gt; int
		  public 'rows' =&gt; 
		    array
		      0 =&gt; 
		        object(stdClass)[20]
		          public 'id' =&gt; string
		          public 'cell' =&gt; 
		            array
		              0 =&gt; string 
		              1 =&gt; string 
		              2 =&gt; string 
		              3 =&gt; string 
		              4 =&gt; string 
		      1 =&gt; 
		        object(stdClass)
		          public 'id' =&gt; string 
		          public 'cell' =&gt; 
		            array
		              0 =&gt; string 
		              1 =&gt; string 
		              2 =&gt; string 
		              3 =&gt; string 
		              4 =&gt; string 
&nbsp;
&nbsp;
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> prep_jqgrid<span style="color: #009900;">&#40;</span><span style="color: #990000;">Array</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> StdClass <span style="color: #000088;">$pager</span><span style="color: #339933;">,</span> <span style="color: #990000;">Array</span> <span style="color: #000088;">$col_order</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//$this-&gt;log($data, LOG_DEBUG);</span>
		<span style="color: #666666; font-style: italic;">/*
			THESE ARE THE PARAMETERS SENT BY JQGRID FOR THE PAGER
&nbsp;
			total	total pages for the query
			page	current page of the query
			records	total number of records for the query
			rows	an array that contains the actual data
				id	the unique id of the row
				cell	an array that contains the data for a row
		*/</span>
		<span style="color: #000088;">$jqgrid_data</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;		
		<span style="color: #000088;">$jqgrid_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">total</span>;
		<span style="color: #000088;">$jqgrid_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page</span>;
		<span style="color: #000088;">$jqgrid_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">records</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">records</span>;
		<span style="color: #000088;">$jqgrid_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$model</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$model_name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$db_row</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db_row</span><span style="color: #009900;">&#91;</span><span style="">'id'</span><span style="color: #009900;">&#93;</span>;
				<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_row</span><span style="color: #009900;">&#91;</span><span style="">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
				<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$col_order</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$col</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cell</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db_row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$col</span><span style="color: #009900;">&#93;</span>;
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$jqgrid_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span>;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$jqgrid_data</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is how I call this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">function</span> get_ppv_list<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">//	Configure::write('debug', 0);</span>
	    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageTitle</span> <span style="color: #339933;">=</span> <span style="">'Order PPV/VOD'</span>;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadModel</span><span style="color: #009900;">&#40;</span><span style="">'TvSchedule'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000088;">$order</span> <span style="color: #339933;">=</span> <span style="">'TvSchedule.title DESC'</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'sidx'</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: #000088;">$order</span> <span style="color: #339933;">=</span> <span style="">'TvSchedule.'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'sidx'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'sord'</span><span style="color: #009900;">&#93;</span>;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #666666; font-style: italic;">//'conditions'=&gt; array ('Model.field'=&gt;$thisValue), //array of conditions</span>
        <span style="color: #666666; font-style: italic;">//'recursive'=&gt;1, //int</span>
        <span style="">'fields'</span><span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="">'TvSchedule.id'</span><span style="color: #339933;">,</span> <span style="">'TvSchedule.title'</span><span style="color: #339933;">,</span> <span style="">'TvSchedule.genre'</span><span style="color: #339933;">,</span> <span style="">'TvSchedule.rating'</span><span style="color: #339933;">,</span> <span style="">'TvSchedule.price'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//array of field names</span>
        <span style="">'order'</span><span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$order</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//string or array defining order</span>
        <span style="color: #666666; font-style: italic;">//'group'=&gt; array ('Model.field'), //fields to GROUP BY</span>
        <span style="">'limit'</span><span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'rows'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//int</span>
        <span style="">'page'</span><span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #666666; font-style: italic;">//int</span>
        <span style="color: #666666; font-style: italic;">//'callbacks'=&gt;true //other possible values are false, 'before', 'after'</span>
        <span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000088;">$record_total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">TvSchedule</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="">'count'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">total</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record_total</span><span style="color: #339933;">/</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'rows'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'page'</span><span style="color: #009900;">&#93;</span>;
		<span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">records</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$record_total</span>;
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="">'PPVdata'</span><span style="color: #339933;">,</span> json_encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DataPrep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prep_jqgrid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">TvSchedule</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="">'all'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">,</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="">'form'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'colOrder'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="">'ppv_list'</span><span style="color: #339933;">,</span> <span style="">'ajax'</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=89</wfw:commentRss>
		</item>
		<item>
		<title>Apache, SSL, Red Hat 4</title>
		<link>http://rhythmicalmedia.com/?p=78</link>
		<comments>http://rhythmicalmedia.com/?p=78#comments</comments>
		<pubDate>Thu, 02 Jul 2009 17:46:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[Apache]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[red hat]]></category>

		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=78</guid>
		<description><![CDATA[A new requirement came down for the Solo Tech application the other day. We need to serve it using SSL and in addition the SOAP calls to the API need to use SSL as well. There was a lot of hoop jumping in order to get this working. I had a previous install of Apache [...]]]></description>
			<content:encoded><![CDATA[<p>A new requirement came down for the Solo Tech application the other day. We need to serve it using SSL and in addition the SOAP calls to the API need to use SSL as well. There was a lot of hoop jumping in order to get this working. I had a previous install of Apache using DSO and I was hoping to load mod_ssl dynamically along with my other modules. However I could not get apxs to create the mod_ssl module. So I ended up recompiling Apache and PHP from scratch.</p>
<p>First I installed the latest version of openSSl from <a href="http://www.openssl.org/" target="_blank">here.</a><br />
(remember to do installs as root)</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># gzip -d openssl.tar.gz</span>
<span style="color: #666666; font-style: italic;"># tar -xvf openssl.tar</span>
<span style="color: #666666; font-style: italic;"># cd openssl</span>
<span style="color: #666666; font-style: italic;"># ./configure -fPIC os/compiler:gcc</span>
<span style="color: #666666; font-style: italic;"># make</span>
<span style="color: #666666; font-style: italic;"># make test</span>
<span style="color: #666666; font-style: italic;"># make install</span></pre></div></div>

<p>Then compile and install Apache to use ssl, php, rewrite and so (shared objects). Unzip and untar as above and switch into the new directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># ./configure --enable-ssl=shared --with-ssl=/usr/local/ssl/ --enable-rewrite=shared --enable-setenvif --enable-so</span>
<span style="color: #666666; font-style: italic;"># make</span>
<span style="color: #666666; font-style: italic;"># make install</span></pre></div></div>

<p>I then created a key and certificate by doing the following and storing them in apache2/conf/certs</p>
<p>The last line creates a self signed certificate.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># openssl genrsa -out hostname.key 1024</span>
<span style="color: #666666; font-style: italic;"># openssl req -new -key hostname.key -out hostname.csr</span>
<span style="color: #666666; font-style: italic;"># openssl x509 -req -days 365 -in hostname.csr -signkey hostname.key -out hostname.crt</span></pre></div></div>

<p>Open up apache2/conf/httpd.conf and uncomment the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #b1b100;">Include</span> conf<span style="color: #339933;">/</span>extra<span style="color: #339933;">/</span>httpd<span style="color: #339933;">-</span>ssl<span style="color: #339933;">.</span>conf</pre></div></div>

<p>Then open up apache2/conf/extra/httpd-ssl.conf and un-comment and set the paths to the certificate and key you just created.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">SSLCertificateFile <span style="color: #0000ff;">&quot;/usr/local/apache2/conf/certs/hostname.crt&quot;</span>
SSLCertificateKeyFile <span style="color: #0000ff;">&quot;/usr/local/apache2/conf/certs/hostname.key&quot;</span></pre></div></div>

<p>Restart Apache. (I have apachectl in my path)</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># apachectl -k restart</span></pre></div></div>

<p>If everything is cool then you should be able to request a page from your server through https. The browser will complain about the certificate. You can accept the certificate and then you should be communicating through SSL.</p>
<p>The next step is setting up PHP. So stop Apache.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># apachectl -k stop</span></pre></div></div>

<p>Repeat the unzip and untar process from above on the downloaded PHP archive and proceed with configuration and installation.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#  ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql --with-curl --enable-soap --with-openssl=/usr/local/ssl</span>
<span style="color: #666666; font-style: italic;"># make</span>
<span style="color: #666666; font-style: italic;"># make test</span>
<span style="color: #666666; font-style: italic;"># make install</span></pre></div></div>

<p>Your path to openssl may differ to just verify its location with:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># whereis openssl</span></pre></div></div>

<p>Move the PHP ini file to its final location:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># cp php.ini-dist /usr/local/lib/php.ini</span></pre></div></div>

<p>Again this may differ on your system.<br />
Open apache2/conf/httpd.conf again and add the following lines at the end.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">          SetHandler application<span style="color: #339933;">/</span>x<span style="color: #339933;">-</span>httpd<span style="color: #339933;">-</span>php</pre></div></div>

<p>Make sure the php module is being loaded by the conf file. You should see this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">LoadModule ssl_module modules<span style="color: #339933;">/</span>mod_ssl<span style="color: #339933;">.</span>so
LoadModule rewrite_module modules<span style="color: #339933;">/</span>mod_rewrite<span style="color: #339933;">.</span>so
LoadModule php5_module        modules<span style="color: #339933;">/</span>libphp5<span style="color: #339933;">.</span>so</pre></div></div>

<p>Restart the server.</p>
<p>For the SOAP stuff I set this in my app config.php file.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'SOAP_URL'</span><span style="color: #339933;">,</span> <span style="">'https:&lt;URL&gt;?wsdl'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'SOAP_ORGNAME'</span><span style="color: #339933;">,</span> <span style="">'ORG1002'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'SOAP_API_VERSION'</span><span style="color: #339933;">,</span> <span style="">'3.2'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'SOAP_WAIT'</span><span style="color: #339933;">,</span> <span style="">'0'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'SSL_CERT_PATH'</span><span style="color: #339933;">,</span> <span style="">'/usr/local/apache2/certs/triad.pem'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>And then when I instantiate the SOAP client I do this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">&nbsp;
        try
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span>
	            SOAP_URL<span style="color: #339933;">,</span>
		            <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
		            <span style="color: #0000ff;">&quot;trace&quot;</span><span style="color: #339933;">=&gt;</span>true
		            <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;exceptions&quot;</span><span style="color: #339933;">=&gt;</span>true
		            <span style="color: #339933;">,</span> <span style="">'features'</span><span style="color: #339933;">=&gt;</span>SOAP_SINGLE_ELEMENT_ARRAYS
					<span style="color: #339933;">,</span> <span style="">'local_cert'</span> <span style="color: #339933;">=&gt;</span> SSL_CERT_PATH
		            <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
        catch<span style="color: #009900;">&#40;</span>SoapFault <span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</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: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=78</wfw:commentRss>
		</item>
		<item>
		<title>Installing Apache and PHP on Red Hat 5</title>
		<link>http://rhythmicalmedia.com/?p=73</link>
		<comments>http://rhythmicalmedia.com/?p=73#comments</comments>
		<pubDate>Tue, 23 Jun 2009 18:19:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[Apache]]></category>

		<category><![CDATA[Linux]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=73</guid>
		<description><![CDATA[As part of building up my dev environment at ETI I had to install Apache2.2 and PHP5 on Red Hat 5. Here are my install notes.
Do this as root.
First download the source for Apache and PHP. This command will get the file from the URL and put it at your present location.

#wget

Unzip and untar the [...]]]></description>
			<content:encoded><![CDATA[<p>As part of building up my dev environment at ETI I had to install Apache2.2 and PHP5 on Red Hat 5. Here are my install notes.</p>
<p>Do this as root.</p>
<p>First download the source for Apache and PHP. This command will get the file from the URL and put it at your present location.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#wget</span></pre></div></div>

<p>Unzip and untar the Apache Archive</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#gzip -d  </span>
<span style="color: #666666; font-style: italic;">#tar xvf</span></pre></div></div>

<p>Switch into the apache directory created from untarring the file.</p>
<p>Configure Apache with Dynamic Object support so that you add new modules easily at a later time.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#./configure ./configure --enable-so</span>
<span style="color: #666666; font-style: italic;">#make</span>
<span style="color: #666666; font-style: italic;">#make install</span></pre></div></div>

<p>Done. Now you can test by starting and stopping the server</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#/usr/local/apache2/bin/apachectl -k [start | stop]</span></pre></div></div>

<p>If there are no errors when running it you should be able to browse to the test page:</p>
<p>http://&lt;server ip&gt;</p>
<p>PHP</p>
<p>Repeat the unzip and untar instructions above for the PHP archive.<br />
Configure PHP with options you need. In my case I need mysql, cURL and SOAP. Notice the first option with the path to apxs. This is actually going to compile the PHP module and move it to Apache&#8217;s module directory. This is also used later when want to build and install new Apache modules.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql --with-curl --enable-soap</span>
<span style="color: #666666; font-style: italic;">#make</span>
<span style="color: #666666; font-style: italic;">#make install</span></pre></div></div>

<p>Then you want to move the distributed PHP config file to the final location. In my case it was this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#cp php.ini-dist /usr/local/lib/php.ini</span></pre></div></div>

<p>You also need to edit the Apache config file to tell it to process PHP files. I put this at the end of my httpd.conf file.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">    SetHandler application<span style="color: #339933;">/</span>x<span style="color: #339933;">-</span>httpd<span style="color: #339933;">-</span>php</pre></div></div>

<p>Finally I wanted to install mod_rewrite. In the directory that contains the source files look for the modules directory that contains the module you need. In my case it is mappers. Then I ran this command which compiles and installs the mod_rewrite module.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#/usr/local/apache2/bin/apxs -i -a -c mod_rewrite.c</span></pre></div></div>

<p>Restart Apache and off you go.</p>
<p>Useful Links<br />
<a href="http://httpd.apache.org/docs/2.0/programs/apxs.html" target="_blank">http://httpd.apache.org/docs/2.0/programs/apxs.html</a><br />
<a href="http://httpd.apache.org/docs/2.2/dso.html" target="_blank">http://httpd.apache.org/docs/2.2/dso.html</a><br />
<a href="http://www.php.net/manual/en/install.unix.apache2.php" target="_blank">http://www.php.net/manual/en/install.unix.apache2.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=73</wfw:commentRss>
		</item>
		<item>
		<title>Class Loader</title>
		<link>http://rhythmicalmedia.com/?p=70</link>
		<comments>http://rhythmicalmedia.com/?p=70#comments</comments>
		<pubDate>Mon, 16 Feb 2009 01:59:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Lab]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=70</guid>
		<description><![CDATA[I needed a class loader for my little framework as I dont want to include all the files in on every request which is what listing them in a long list of includes will do. So I checked out the PHP.net site which has this code sample. I used it as a starting point for [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a class loader for my little framework as I dont want to include all the files in on every request which is what listing them in a long list of includes will do. So I checked out the <a href="http://php.net/" target="_blank">PHP.net </a>site which has <a href="http://www.php.net/~helly/php/ext/spl/autoload_8inc.html" target="_blank">this code sample</a>. I used it as a starting point for this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __load_class<span style="color: #009900;">&#40;</span><span style="color: #000088;">$classname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dir</span> <span style="color: #339933;">.</span> <span style="">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$classname</span> <span style="color: #339933;">.</span> <span style="">'.php'</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$classname</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$inc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="">'../fwork'</span>;
	<span style="color: #000088;">$inc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="">'../fwork/controllers'</span>;
	<span style="color: #000088;">$inc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="">'../fwork/models'</span>;
	<span style="color: #000088;">$inc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="">'../app'</span>;
	<span style="color: #000088;">$inc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="">'../app/controllers'</span>;
	<span style="color: #000088;">$inc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="">'../app/models'</span>;
&nbsp;
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inc</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>__load_class<span style="color: #009900;">&#40;</span><span style="color: #000088;">$classname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>DEBUG_LEVEL <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">echo</span> <span style="">'Loading class('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$classname</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;)&lt;br&gt;&quot;</span>;
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">return</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I need to do some more testing and abstraction but its a good start for what I need.</p>
]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=70</wfw:commentRss>
		</item>
		<item>
		<title>Mod_rewrite</title>
		<link>http://rhythmicalmedia.com/?p=67</link>
		<comments>http://rhythmicalmedia.com/?p=67#comments</comments>
		<pubDate>Mon, 16 Feb 2009 01:54:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Lab]]></category>

		<category><![CDATA[mod_rewrite]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=67</guid>
		<description><![CDATA[I wanted to start a small MVC framework to help build my next project. I needed to get mod_rewrite working and also build a simple class loader.
For mod_rewrite I setup the following .htaccess file (xp.htaccess to get around Windows&#8217; filename issue)

RewriteEngine on
RewriteCond %&#123;REQUEST_URI&#125; !\.&#40;php&#124;css&#124;js&#124;gif&#124;png&#124;jpe?g&#41;$
RewriteRule &#40;.*&#41;$ /index.php &#91;L&#93;

Then in my dispatcher file I can read the [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to start a small MVC framework to help build my next project. I needed to get <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html" target="_blank">mod_rewrite</a> working and also build a simple class loader.</p>
<p>For mod_rewrite I setup the following .htaccess file (<a href="http://httpd.apache.org/docs/2.2/platform/windows.html" target="_blank">xp.htaccess to get around Windows&#8217; filename issue</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">RewriteEngine on
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_URI<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>php|css|js|gif|png|jpe?g<span style="color: #009900;">&#41;</span>$
RewriteRule <span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span>index<span style="color: #339933;">.</span>php <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></div></div>

<p>Then in my dispatcher file I can read the contents of the query string like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$request_uri</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_uri</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: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_uri</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$action</span> <span style="color: #339933;">==</span> <span style="">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="">'index'</span>; <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$param</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_uri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span> <span style="color: #339933;">==</span> <span style="">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$param</span> <span style="color: #339933;">=</span> <span style="">''</span>; <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is just temporary code and used to prove that it would work the way I expected. Sort of like tracer bullets described in <a href="http://www.pragprog.com/the-pragmatic-programmer" target="_blank">The Pragmatic Programmer</a>. But now I can use a URL like:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//recipemanager/ingredient_type/update/5</span></pre></div></div>

<p>Which will then let me know that I need to access the ingredient_type controller pass the parameter 5 to the update method.</p>
]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=67</wfw:commentRss>
		</item>
		<item>
		<title>Zend Framework</title>
		<link>http://rhythmicalmedia.com/?p=65</link>
		<comments>http://rhythmicalmedia.com/?p=65#comments</comments>
		<pubDate>Wed, 11 Feb 2009 13:31:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Lab]]></category>

		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=65</guid>
		<description><![CDATA[Anthony suggested that I try out the Zend Framework. It&#8217;s a pretty simple installation but I had a hard time findinga decent tutorial. The simple starter on the Zend site is set up to use SQL Lite and I just did not feel like installing and learning that at the same time. Then I stumbled [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://suddendevelopment.com/" target="_blank">Anthony</a> suggested that I try out the <a href="http://framework.zend.com/" target="_blank">Zend Framework</a>. It&#8217;s a pretty simple installation but I had a hard time findinga decent tutorial. The simple starter on the Zend site is set up to use SQL Lite and I just did not feel like installing and learning that at the same time. Then I stumbled upon <a href="http://akrabat.com/wp-content/uploads/getting-started-with-zend-framework-152.pdf" target="_blank">this</a> one that is quite good. Once you have completed it you will have a working CRUD for a single table. I found a couple of problems in the documentation vs. the example code though.</p>
<p>The tutorial shows how to set up your Apache virtual server for the project</p>
<p>ServerName zf-tutorial.localhost<br />
DocumentRoot /var/www/html/zf-tutorial/public<br />
AllowOverride All</p>
<p>This means that the server would serve files from the public directory. It then goes on to say to put the bootstrap (index.php) under the public directory. I was having some problems so I downloaded the example code to compare it with what I had done. I noticed that in the example code the index.php file was under the parent directory of public. This means the virtual host entry is wrong as it should point to zf-tutorial not public. I decided to leave the bootstrap file in the public directory as I did not want to have public access to the root of the application.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">E_ALL</span>|E_STRICT<span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="">'display_errors'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
date_default_timezone_set<span style="color: #009900;">&#40;</span><span style="">'Europe/London'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="">'../helpers/print_var.php'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="">'../helpers/MyIterator.php'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// directory setup and class loading</span>
<span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span><span style="">'.'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="">'../library/'</span>
<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="">'../application/models'</span>
<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;Zend/Loader.php&quot;</span>;
Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">registerAutoload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// load configuration</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Ini<span style="color: #009900;">&#40;</span><span style="">'../application/config.ini'</span><span style="color: #339933;">,</span> <span style="">'general'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$registry</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="">'config'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// setup database</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span>;
Zend_Db_Table<span style="color: #339933;">::</span><span style="color: #004000;">setDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// setup controller</span>
<span style="color: #000088;">$frontController</span> <span style="color: #339933;">=</span> Zend_Controller_Front<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$frontController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">throwExceptions</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$frontController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerDirectory</span><span style="color: #009900;">&#40;</span><span style="">'../application/controllers'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Zend_Layout<span style="color: #339933;">::</span><span style="color: #004000;">startMvc</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'layoutPath'</span><span style="color: #339933;">=&gt;</span><span style="">'../application/layouts'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// run!</span>
<span style="color: #000088;">$frontController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=65</wfw:commentRss>
		</item>
		<item>
		<title>Apache Virtual Host on Windows XP</title>
		<link>http://rhythmicalmedia.com/?p=62</link>
		<comments>http://rhythmicalmedia.com/?p=62#comments</comments>
		<pubDate>Tue, 03 Feb 2009 00:22:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[Apache]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=62</guid>
		<description><![CDATA[I wanted to set up some virtual hosts in my local environment. I found this very helpful page. Here is the lesson in a nutshell.
Assuming Apache 2.2 is installed in the default location open the following file:

C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf

Find the lines that read:

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

and remove the comment (#) so that it looks [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to set up some virtual hosts in my local environment. I found <a href="http://foundationphp.com/tutorials/apache22_vhosts.php" target="_blank">this </a>very helpful page. Here is the lesson in a nutshell.</p>
<p>Assuming Apache 2.2 is installed in the default location open the following file:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">C<span style="color: #339933;">:</span>\Program Files\Apache Software Foundation\Apache2<span style="color: #339933;">.</span>2\conf\httpd<span style="color: #339933;">.</span>conf</pre></div></div>

<p>Find the lines that read:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Virtual hosts</span>
<span style="color: #666666; font-style: italic;">#Include conf/extra/httpd-vhosts.conf</span></pre></div></div>

<p>and remove the comment (#) so that it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Virtual hosts</span>
<span style="color: #b1b100;">Include</span> conf<span style="color: #339933;">/</span>extra<span style="color: #339933;">/</span>httpd<span style="color: #339933;">-</span>vhosts<span style="color: #339933;">.</span>conf</pre></div></div>

<p>This enables virtual hosting in Apache. Note that it also shuts down the default host.</p>
<p>Open the following Windows file:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\WINDOWS\system32\drivers\etc\hosts</pre></div></div>

<p>You should see something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">127<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>1       localhost</pre></div></div>

<p>Duplicate the line (keep the same IP number) and name a new host, for instance recipemanager and save the file.</p>
<p>Open the following file (this is the file you just enabled in Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">C<span style="color: #339933;">:</span>\Program Files\Apache Software Foundation\Apache2<span style="color: #339933;">.</span>2\conf\extra\httpd<span style="color: #339933;">-</span>vhosts<span style="color: #339933;">.</span>conf</pre></div></div>

<p>Added the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>Directory c<span style="color: #339933;">:/</span>vhosts<span style="color: #339933;">&gt;</span>
Options Indexes FollowSymLinks
AllowOverride All
Order Deny<span style="color: #339933;">,</span>Allow
Allow from all
<span style="color: #339933;">&lt;/</span>Directory<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This propagates the permissions you set in the directive to cascade to all foldes in c:/vhosts which means you can set up all your virtual servers under c:/vhosts.</p>
<p>Because the default localhost is disabled you need to create that like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>VirtualHost <span style="color: #339933;">*:</span><span style="color: #cc66cc;">80</span><span style="color: #339933;">&gt;</span>
DocumentRoot c<span style="color: #339933;">:/</span>htdocs
ServerName localhost
<span style="color: #339933;">&lt;/</span>VirtualHost<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This points localhost at c:/htdocs</p>
<p>Then you can create your recipemanager host:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>VirtualHost <span style="color: #339933;">*:</span><span style="color: #cc66cc;">80</span><span style="color: #339933;">&gt;</span>
DocumentRoot c<span style="color: #339933;">:/</span>vhosts<span style="color: #339933;">/</span>recipemanager<span style="color: #339933;">/</span>site
ServerName recipemanager
<span style="color: #339933;">&lt;/</span>VirtualHost<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Reboot Apache. Place a test file in c:/vhosts/recipemanager/site</p>
<p>Navigate with your browser to http://recipemanager/&lt;TESTFILENAME&gt;</p>
<p>And you should see the rendered file.</p>
]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=62</wfw:commentRss>
		</item>
		<item>
		<title>Autocomplete field with jQuery and PHP</title>
		<link>http://rhythmicalmedia.com/?p=42</link>
		<comments>http://rhythmicalmedia.com/?p=42#comments</comments>
		<pubDate>Sun, 11 Jan 2009 20:10:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Lab]]></category>

		<category><![CDATA[jquery]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=42</guid>
		<description><![CDATA[For the recipe manager project I need to allow the entering of ingredients that the system does not know about. I decided to use an auto complete field to allow users to easily see what was already in the system. If the ingredient does not exist then the one they entered will added to the [...]]]></description>
			<content:encoded><![CDATA[<p>For the recipe manager project I need to allow the entering of ingredients that the system does not know about. I decided to use an auto complete field to allow users to easily see what was already in the system. If the ingredient does not exist then the one they entered will added to the system.</p>
<p>To test this out I wrote a quick little program that assigns employees to an assigned employees table. The autocomplete does a look up in the employees table. If the entered employee is not found then the new employee is added to the employees table and then assigned.</p>
<p>I used the following jQuery stuff:</p>
<p>jQuery1.2.6<br />
Autocomplete - jQuery plugin 1.0.2 (copyright (c) 2007 Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, Jörn Zaefferer)</p>
<p>Main interface: index.php</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="">'config.php'</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000088;">$employees</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000088;">$assigned_employees</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp;
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from employees&quot;</span>;
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> MYSQL_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$employees</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'first_name'</span><span style="color: #009900;">&#93;</span>;
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from assigned_employees&quot;</span>;
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> MYSQL_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$assigned_employees</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'first_name'</span><span style="color: #009900;">&#93;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; 
                    &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;script src=&quot;../javascript/jquery-latest.js&quot;&gt;&lt;/script&gt;
 	&lt;link rel=&quot;stylesheet&quot; href=&quot;../javascript/jquery.autocomplete.css&quot; type=&quot;text/css&quot; /&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;../javascript/jquery.autocomplete.js&quot;&gt;&lt;/script&gt;
	&lt;script&gt;
		$(document).ready(function()
		{
			// var data = &quot;Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities&quot;.split(&quot; &quot;);
			$(&quot;#first_name&quot;).autocomplete('autocomplete.php');
&nbsp;
			$(&quot;#first_name&quot;).result(function() 
			{
				console.log(arguments);
			}
			);
		}
		);
  &lt;/script&gt;
&nbsp;
&lt;/head&gt;
&lt;body&gt;
	&lt;form action=&quot;insert.php&quot; method=&quot;post&quot;&gt;
		Assigned an employee: &lt;input id=&quot;first_name&quot; name=&quot;first_name&quot; /&gt;
		&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
	&lt;/form&gt;
&nbsp;
	&lt;h3&gt;Employees&lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$employees</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;$employee &lt;br&gt;&quot;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>	
&nbsp;
&lt;h3&gt;Assigned Employees&lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$assigned_employees</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;$employee &lt;br&gt;&quot;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div&gt;--------------------------------------------&lt;/div&gt;
&lt;a href=&quot;clear.php&quot;&gt;Remove all assignments&lt;/a&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Auto Complete Function: autocomplete.php</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="">'config.php'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="">'q'</span><span style="color: #009900;">&#93;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Performing SQL query</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, first_name FROM employees&quot;</span>;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> MYSQL_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'first_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">echo</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'first_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span><span style="">'|'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Insert Function: insert.php</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="">'config.php'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//get the first_name from the form</span>
<span style="color: #000088;">$first_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'first_name'</span><span style="color: #009900;">&#93;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// see if we have it in the database. If so then grab the ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM employees WHERE first_name = '$first_name'&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//print &quot;$query&lt;br&gt;&quot;;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</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: #666666; font-style: italic;">// If not then insert it into the employees  table. Then get the ID of the new employee from the employees table.</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO employees(first_name) VALUES('$first_name')&quot;</span>;
	<span style="color: #666666; font-style: italic;">//print &quot;$query&lt;br&gt;&quot;;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_insert_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="">'id'</span><span style="color: #009900;">&#93;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Now insert into the ingredient list table.</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO assigned_employees(id, first_name) VALUES($id, '$first_name')&quot;</span>;
<span style="color: #666666; font-style: italic;">//print &quot;$query&lt;br&gt;&quot;;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span> <span style="">'Location: http://localhost/lab/autocomplete/index.php'</span> <span style="color: #009900;">&#41;</span> ;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Clear Function: clear.php</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="">'config.php'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;delete FROM assigned_employees&quot;</span>;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span> <span style="">'Location: http://localhost/lab/autocomplete/index.php'</span> <span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Database Schema</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #808080; font-style: italic;">/*!32312 IF NOT EXISTS*/</span><span style="color: #ff0000;">`test`</span> <span style="color: #808080; font-style: italic;">/*!40100 DEFAULT CHARACTER SET latin1 */</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #ff0000;">`test`</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*Table structure for table `assigned_employees` */</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`assigned_employees`</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`assigned_employees`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">ZEROFILL</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`first_name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">9</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;
&nbsp;
<span style="color: #808080; font-style: italic;">/*Data for the table `assigned_employees` */</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span>  <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`assigned_employees`</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`first_name`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>0000000002<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'sam'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*Table structure for table `employees` */</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`employees`</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`employees`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">ZEROFILL</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`first_name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">9</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;
&nbsp;
<span style="color: #808080; font-style: italic;">/*Data for the table `employees` */</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span>  <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`employees`</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`first_name`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>0000000001<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'steve'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000002<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'sam'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000003<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'heather'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000004<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'heath'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000005<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'shamus'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000006<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'jennifer'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000007<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'geronimo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>0000000008<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'gerald'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*!40101 SET SQL_MODE=@OLD_SQL_MODE */</span>;
<span style="color: #808080; font-style: italic;">/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=42</wfw:commentRss>
		</item>
		<item>
		<title>jQuery solution to the add ingredient row problem</title>
		<link>http://rhythmicalmedia.com/?p=29</link>
		<comments>http://rhythmicalmedia.com/?p=29#comments</comments>
		<pubDate>Sun, 04 Jan 2009 19:28:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Recipe Manager]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=29</guid>
		<description><![CDATA[Due to the fact that I could not find a reasonable solution within the CakePHP framework to add a new row of ingredient list inputs to recipe form I solved the problem using jQuery.
This is the script.

$&#40;'#add_ingredient_row'&#41;.click&#40;function&#40;&#41;
&#123;
	// Clone the last row of the table.
	var clonedRow = $&#40;&#34;#ingredient_list tr:last&#34;&#41;.clone&#40;&#41;;
&#160;
	// Generate an incremented index for the new [...]]]></description>
			<content:encoded><![CDATA[<p>Due to the fact that I could not find a reasonable solution within the CakePHP framework to add a new row of ingredient list inputs to recipe form I solved the problem using jQuery.</p>
<p>This is the script.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#add_ingredient_row'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Clone the last row of the table.</span>
	<span style="color: #003366; font-weight: bold;">var</span> clonedRow <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#ingredient_list tr:last&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Generate an incremented index for the new row. -1 because there is a table header row</span>
	<span style="color: #003366; font-weight: bold;">var</span> newIndex <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ingredient_list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">rows</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Set the ID of the row with the new index</span>
	clonedRow<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'ingredient_'</span> <span style="color: #339933;">+</span> newIndex;
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">//console.log('ingredient_' + newIndex);</span>
	<span style="color: #006600; font-style: italic;">// Loop through all of the inputs and select in the cloned row</span>
	<span style="color: #006600; font-style: italic;">// Find the name and ID of the input/select element and find the number in it abd replace it with the</span>
	<span style="color: #006600; font-style: italic;">// new index.</span>
	$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input, select'</span><span style="color: #339933;">,</span> clonedRow<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//console.log(val);</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//console.log('Index -1: ' + (newIndex -1) + '. Index: ' + newIndex);</span>
		val.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> val.<span style="color: #000066;">name</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>newIndex <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> newIndex<span style="color: #009900;">&#41;</span>;
		val.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> val.<span style="color: #660066;">id</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>newIndex <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> newIndex<span style="color: #009900;">&#41;</span>;
		val.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #006600; font-style: italic;">// put the row into the table</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#ingredient_list&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>clonedRow<span style="color: #009900;">&#41;</span>; 
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This is the table that it operates on.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;table id=&quot;ingredient_list&quot;&gt;
&lt;tr&gt;
	&lt;th&gt;Amount&lt;/th&gt;
&nbsp;
	&lt;th&gt;Measurement Type&lt;/th&gt;
	&lt;th&gt;Description&lt;/th&gt;
	&lt;th&gt;Ingredient&lt;/th&gt;
&lt;/tr&gt;
	&lt;tr id=&quot;ingredient_0&quot;&gt;
		&lt;td&gt;
			&lt;div class=&quot;input text&quot;&gt;&lt;input name=&quot;data[IngredientList][0][amount]&quot; type=&quot;text&quot; maxlength=&quot;5&quot; value=&quot;&quot; id=&quot;IngredientList0Amount&quot; /&gt;&lt;/div&gt;&lt;input type=&quot;hidden&quot; name=&quot;data[IngredientList][0][id]&quot; value=&quot;&quot; id=&quot;IngredientList0Id&quot; /&gt;		&lt;/td&gt;
&nbsp;
		&lt;td&gt;
			&lt;select name=&quot;data[IngredientList][0][measurement_type_id]&quot; id=&quot;IngredientList0MeasurementTypeId&quot;&gt;
				&lt;option value=&quot;&quot;&gt;&lt;/option&gt;
				&lt;option value=&quot;0000000002&quot;&gt;cup&lt;/option&gt;
				&lt;option value=&quot;0000000005&quot;&gt;fluid ounce&lt;/option&gt;
				&lt;option value=&quot;0000000001&quot;&gt;ounce&lt;/option&gt;
				&lt;option value=&quot;0000000004&quot;&gt;tablespoon&lt;/option&gt;
				&lt;option value=&quot;0000000003&quot;&gt;teaspoon&lt;/option&gt;
			&lt;/select&gt;		
		&lt;/td&gt;
&nbsp;
		&lt;td&gt;
			&lt;div class=&quot;input text&quot;&gt;&lt;input name=&quot;data[IngredientList][0][description]&quot; type=&quot;text&quot; maxlength=&quot;255&quot; value=&quot;&quot; id=&quot;IngredientList0Description&quot; /&gt;&lt;/div&gt;		&lt;/td&gt;
		&lt;td&gt;
			&lt;select name=&quot;data[IngredientList][0][ingredient_id]&quot; id=&quot;IngredientList0IngredientId&quot;&gt;
				&lt;option value=&quot;&quot;&gt;&lt;/option&gt;
				&lt;option value=&quot;0000000006&quot;&gt;broccoli&lt;/option&gt;
				&lt;option value=&quot;0000000008&quot;&gt;garlic&lt;/option&gt;
				&lt;option value=&quot;0000000009&quot;&gt;olive oil&lt;/option&gt;
				&lt;option value=&quot;0000000010&quot;&gt;peanut oil&lt;/option&gt;
&nbsp;
				&lt;option value=&quot;0000000012&quot;&gt;red onion&lt;/option&gt;
				&lt;option value=&quot;0000000007&quot;&gt;white onion&lt;/option&gt;
				&lt;option value=&quot;0000000011&quot;&gt;yellow onion&lt;/option&gt;
			&lt;/select&gt;		
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;</pre></div></div>

<p>Here is the final solution I came up with. I abstracted out the cloning and manipulation code into a separate function so that it would be DRY.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#add_recipe_row'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		add_row<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'recipes_table'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'menu_item_'</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; 
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#add_ingredient_row'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		add_row<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ingredient_list'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'ingredient_'</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// ends document.ready</span>
&nbsp;
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> add_row<span style="color: #009900;">&#40;</span>tableID<span style="color: #339933;">,</span> rowIDprefix<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Clone the last row of the table.</span>
	<span style="color: #003366; font-weight: bold;">var</span> clonedRow <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span> tableID <span style="color: #339933;">+</span> <span style="color: #3366CC;">' tr:last'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Generate an incremented index for the new row. -1 because there is a table header row</span>
	<span style="color: #003366; font-weight: bold;">var</span> newIndex <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>tableID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">rows</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Set the ID of the row with the new index</span>
	clonedRow<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> rowIDprefix <span style="color: #339933;">+</span> newIndex;
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">//console.log('ingredient_' + newIndex);</span>
	<span style="color: #006600; font-style: italic;">// Loop through all of the inputs and select in the cloned row</span>
	<span style="color: #006600; font-style: italic;">// Find the name and ID of the input/select element and find the number in it abd replace it with the</span>
	<span style="color: #006600; font-style: italic;">// new index.</span>
	$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input, select'</span><span style="color: #339933;">,</span> clonedRow<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//console.log(val);</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//console.log('Index -1: ' + (newIndex -1) + '. Index: ' + newIndex);</span>
		val.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> val.<span style="color: #000066;">name</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>newIndex <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> newIndex<span style="color: #009900;">&#41;</span>;
		val.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> val.<span style="color: #660066;">id</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>newIndex <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> newIndex<span style="color: #009900;">&#41;</span>;
		val.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #006600; font-style: italic;">// put the row into the table</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> tableID<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>clonedRow<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=29</wfw:commentRss>
		</item>
		<item>
		<title>MySQL Workbench</title>
		<link>http://rhythmicalmedia.com/?p=27</link>
		<comments>http://rhythmicalmedia.com/?p=27#comments</comments>
		<pubDate>Sun, 04 Jan 2009 02:36:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Dev Tools]]></category>

		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://rhythmicalmedia.com/?p=27</guid>
		<description><![CDATA[Today I was going crazy trying to figure out a foreign key issue. So I found this great tool that reverse engineers a graphic model from sql statements. Very cool! It even outputs the model as an image.
]]></description>
			<content:encoded><![CDATA[<p>Today I was going crazy trying to figure out a foreign key issue. So I found this great <a href="http://dev.mysql.com/downloads/gui-tools/5.0.html" target="_blank">tool </a>that reverse engineers a graphic model from sql statements. Very cool! It even outputs the model as an <a href="http://rhythmicalmedia.com/images/schema.png" target="_blank">image</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://rhythmicalmedia.com/?feed=rss2&amp;p=27</wfw:commentRss>
		</item>
	</channel>
</rss>
