<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Team Tutorials</title>
	<atom:link href="http://teamtutorials.com/feed" rel="self" type="application/rss+xml" />
	<link>http://teamtutorials.com</link>
	<description></description>
	<lastBuildDate>Fri, 23 Jul 2010 12:13:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Simple XML to XHTML Transformation</title>
		<link>http://teamtutorials.com/web-development-tutorials/simple-xml-to-xhtml-transformation</link>
		<comments>http://teamtutorials.com/web-development-tutorials/simple-xml-to-xhtml-transformation#comments</comments>
		<pubDate>Mon, 03 May 2010 21:38:37 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[Web Development Tutorials]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2581</guid>
		<description><![CDATA[In this tutorial we are going to use XSLT to transform a simple XML document into an XHTML web page.  XSLT stands for eXtensible Style Sheet Transformations. With our XSL document we will pretty much grab the values we need from the source XML and display them nicely in XHTML.]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we are going to use XSLT to transform a simple XML document into an XHTML web page.  XSLT stands for eXtensible Style Sheet Transformations. With our XSL document we will pretty much grab the values we need from the source XML and display them nicely in XHTML. In order to complete this tutorial you should have a basic understanding of XML documents, HTML/XHTML, and CSS.</p>
<p>First off you need some sample XML. I made a quick list of cars I have owned.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;autos&gt;
	&lt;car&gt;
		&lt;year&gt;1995&lt;/year&gt;
		&lt;make&gt;Eagle&lt;/make&gt;
		&lt;model&gt;Talon&lt;/model&gt;
		&lt;trim&gt;ESi&lt;/trim&gt;
	&lt;/car&gt;
	&lt;car&gt;
		&lt;year&gt;1998&lt;/year&gt;
		&lt;make&gt;Mitsubishi&lt;/make&gt;
		&lt;model&gt;Eclipse&lt;/model&gt;
		&lt;trim&gt;GS-T Spyder&lt;/trim&gt;
	&lt;/car&gt;
	&lt;car&gt;
		&lt;year&gt;2004&lt;/year&gt;
		&lt;make&gt;Cadillac&lt;/make&gt;
		&lt;model&gt;CTS&lt;/model&gt;
		&lt;trim&gt;Sport&lt;/trim&gt;
	&lt;/car&gt;
&lt;/autos&gt;
</pre>
<p>We are going to add an XML stylesheet to the document, similar to a CSS file with HTML. In order to add the file we need to add the xml-stylesheet tag to our root document.</p>
<pre class="brush: xml;">
&lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;output.xsl&quot;?&gt;
</pre>
<p>Which should give you this.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;output.xsl&quot;?&gt;
&lt;autos&gt;
	&lt;car&gt;
		&lt;year&gt;1995&lt;/year&gt;
		&lt;make&gt;Eagle&lt;/make&gt;
		&lt;model&gt;Talon&lt;/model&gt;
		&lt;trim&gt;ESi&lt;/trim&gt;
	&lt;/car&gt;
	&lt;car&gt;
		&lt;year&gt;1998&lt;/year&gt;
		&lt;make&gt;Mitsubishi&lt;/make&gt;
		&lt;model&gt;Eclipse&lt;/model&gt;
		&lt;trim&gt;GS-T Spyder&lt;/trim&gt;
	&lt;/car&gt;
	&lt;car&gt;
		&lt;year&gt;2004&lt;/year&gt;
		&lt;make&gt;Cadillac&lt;/make&gt;
		&lt;model&gt;CTS&lt;/model&gt;
		&lt;trim&gt;Sport&lt;/trim&gt;
	&lt;/car&gt;
&lt;/autos&gt;
</pre>
<p>Next you will need to create a new document that I called output.xsl. This file will be used to tell the browser how to display our XML. Here is a pretty standard starting template.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; doctype-public=&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
        &lt;head&gt;
            &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
            &lt;title&gt;My Cars&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
        &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>Now we need to create a typical HTML table to display our results in.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; doctype-public=&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
        &lt;head&gt;
            &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
            &lt;title&gt;My Cars&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
	        &lt;table&gt;
            	&lt;tr&gt;
			&lt;th&gt;Year&lt;/th&gt;
                    &lt;th&gt;Make&lt;/th&gt;
                    &lt;th&gt;Model&lt;/th&gt;
                    &lt;th&gt;Trim&lt;/th&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>Now we are going to determine which fields we want to display in the rest of the table rows. We will do this by using the xsl:for-each tag. This will pretty much create a for each loop and loop through which ever values you specify. We want whatever data is container withing the car tag within the autos tag. So you would do something like this:</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; doctype-public=&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
        &lt;head&gt;
            &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
            &lt;title&gt;My Cars&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
	        &lt;table&gt;
            	&lt;tr&gt;
					&lt;th&gt;Year&lt;/th&gt;
                    &lt;th&gt;Make&lt;/th&gt;
                    &lt;th&gt;Model&lt;/th&gt;
                    &lt;th&gt;Trim&lt;/th&gt;
                &lt;/tr&gt;
                &lt;xsl:for-each select=&quot;autos/car&quot;&gt;
                &lt;/xsl:for-each&gt;
            &lt;/table&gt;
        &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>Then we simple tell XSLT how to display the values (in tables and cells in this case) and what values we want.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; doctype-public=&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
        &lt;head&gt;
            &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
            &lt;title&gt;My Cars&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
	        &lt;table&gt;
            	&lt;tr&gt;
					&lt;th&gt;Year&lt;/th&gt;
                    &lt;th&gt;Make&lt;/th&gt;
                    &lt;th&gt;Model&lt;/th&gt;
                    &lt;th&gt;Trim&lt;/th&gt;
                &lt;/tr&gt;
                &lt;xsl:for-each select=&quot;autos/car&quot;&gt;
                &lt;tr&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;year&quot;/&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;make&quot;/&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;model&quot;/&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;trim&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;/xsl:for-each&gt;
            &lt;/table&gt;
        &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>Then to finish it off you can style your table with some CSS.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; doctype-public=&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; doctype-system=&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
        &lt;head&gt;
            &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
            &lt;title&gt;My Cars&lt;/title&gt;
            &lt;style&gt;
				td{
					border-bottom:1px dashed #ccc;
					padding-left:4px;
				}
				th{
					background: #bacfec;
					text-align:center;
					padding: 0 15px 0 15px;
				}
			&lt;/style&gt;
        &lt;/head&gt;
        &lt;body&gt;
	        &lt;table&gt;
            	&lt;tr&gt;
					&lt;th&gt;Year&lt;/th&gt;
                    &lt;th&gt;Make&lt;/th&gt;
                    &lt;th&gt;Model&lt;/th&gt;
                    &lt;th&gt;Trim&lt;/th&gt;
                &lt;/tr&gt;
                &lt;xsl:for-each select=&quot;autos/car&quot;&gt;
                &lt;tr&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;year&quot;/&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;make&quot;/&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;model&quot;/&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;xsl:value-of select=&quot;trim&quot;/&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;/xsl:for-each&gt;
            &lt;/table&gt;
        &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>Its that simple. We started with this <a href="http://teamtutorials.com/demos/xml_to_xhtml/original.xml">simple XML file</a> and ended up with the <a href="http://teamtutorials.com/demos/xml_to_xhtml/final.xml">output of an XHTML page</a>.</p>
<p><a href="http://teamtutorials.com/demos/xml_to_xhtml/xml.rar">Download Source</a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/hide-and-show-a-div-using-javascript" title="Hide and Show a Div Using Javascript">Hide and Show a Div Using Javascript</a></li><li><a href="http://teamtutorials.com/other-tutorials/receiving-dynamic-textbox-data" title="Receiving Dynamic Textbox Data">Receiving Dynamic Textbox Data</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/dynamically-add-textbox-to-site" title="Dynamically Add Textbox to Site">Dynamically Add Textbox to Site</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/styling-html-tables-with-tablecloth" title="Styling HTML Tables with TableCloth">Styling HTML Tables with TableCloth</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/installing-code-editor" title="Installing Code Editor">Installing Code Editor</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/web-development-tutorials/simple-xml-to-xhtml-transformation/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Features</title>
		<link>http://teamtutorials.com/site-news/new-features</link>
		<comments>http://teamtutorials.com/site-news/new-features#comments</comments>
		<pubDate>Thu, 08 Apr 2010 23:30:38 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[Site News and Updates]]></category>
		<category><![CDATA[new features]]></category>
		<category><![CDATA[updates]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2574</guid>
		<description><![CDATA[As most of you have probably noticed we have made some major changes to team tutorials. I created a new layout with the intention of making the site much easier to read. We added a preview image for each post that will be updated on all new posts. There is also a description box on [...]]]></description>
			<content:encoded><![CDATA[<p>As most of you have probably noticed we have made some major changes to team tutorials. I created a new layout with the intention of making the site much easier to read. We added a preview image for each post that will be updated on all new posts. There is also a description box on each post with more information about the post and the ability to print or share the post. Here are so more new features:</p>
<ul>
<li><a href="http://teamtutorials.com/forums">Team Tutorials Forums </a>- Check out the new forum section. Here you can ask question on pretty much anything tech related. We look forward to building a loyal forum following. Also if you have a question that is not related to a post you are reading please post it in the appropriate forum.</li>
<li>Comments &#8211; I added avatars to the comments and also nested comments. Now you can reply directly to a person. This should make the comments much easier to read. Also author comments will be highlighted in blue.</li>
<li>Tag Cloud &#8211; We now feature a tag cloud on the side bar so that you can browse post by tag instead of category.</li>
<li>Sub Categories &#8211; Sub categories have been removed from the side bar and moved to the top of each category page. This helped clean up the sidebar.</li>
<li><a href="http://teamtutorials.com/demo-page">Demo Section</a> &#8211; We will now feature all live demos, downloads, source code, etc  for each tutorial on the Demos page.</li>
<li><a href="http://teamtutorials.com/resources">Resource Section</a> &#8211; This page will serve as a one stop shop for the best sites on the web (other than our site). We are looking to add your favorite tutorial sites, free tools, wordpress plugin/layout sites. You get the idea. If you have a site you would like to add please contact us.</li>
<li>New Ad Placements &#8211; With the new layout we have also feature some new ad placements. You can now purchase 125&#215;125 side bar ads and top banners. We hope that the new ad formats will allow us to remove some of the more intrusive post level ads and provide our users with a better overall experience. If you are not using ad block, thank you. If you would like to purchase an ad please visit our <a href="http://teamtutorials.com/advertising">advertising section</a>.</li>
<li>Post Ratings (Coming Soon) &#8211; We also plan to add the ratings to each post. Users will be able to rate the post on different levels such as difficulty and helpfulness. We are having an issue with the rating system right now and will release that feature once we get the kinks worked out.</li>
</ul>
<p>I hope you guys enjoy the new layout. We have both been pretty busy lately and would like to start posting more frequently. That will probably lead to us posting some new simple tutorials. For example post that describe how to use a certain function in some programming language.  If anyone is interested in writing tutorials please <a href="http://teamtutorials.com/contact-information">contact us</a>.</p>
<p>If you see any problems with the new layout please let me know.</p>
<h2  class="related_post_title">Other Related Tutorials</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/replace-wordpress-search-with-google-search-box" title="Replace Wordpress Search With Google Search Box">Replace Wordpress Search With Google Search Box</a></li><li><a href="http://teamtutorials.com/photoshop-tutorials/easy-abstract-photoshop-tutorial" title="Easy Abstract Photoshop Tutorial">Easy Abstract Photoshop Tutorial</a></li><li><a href="http://teamtutorials.com/windows-tutorials/eliminating-spy-ware-and-ad-ware-with-super-anti-spy-ware" title="Eliminating Spy-ware and Ad-ware with Super Anti-Spy-ware">Eliminating Spy-ware and Ad-ware with Super Anti-Spy-ware</a></li><li><a href="http://teamtutorials.com/photoshop-tutorials/blue-circle-button-tutorial" title="Blue Circle Button Tutorial">Blue Circle Button Tutorial</a></li><li><a href="http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql" title="Referential Integrity with MySQL">Referential Integrity with MySQL</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/site-news/new-features/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Site Improvements</title>
		<link>http://teamtutorials.com/site-news/site-improvements</link>
		<comments>http://teamtutorials.com/site-news/site-improvements#comments</comments>
		<pubDate>Wed, 07 Apr 2010 01:35:25 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[Site News and Updates]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2548</guid>
		<description><![CDATA[We have been working on a new look and feel for the site. The new layout should be a little more clean than the old one. We tried to keep it simple and improve the readability.  We will be upgrading over the next few days and will update with more details. Other Related TutorialsReplace Wordpress Search With [...]]]></description>
			<content:encoded><![CDATA[<p>We have been working on a new look and feel for the site. The new layout should be a little more clean than the old one. We tried to keep it simple and improve the readability.  We will be upgrading over the next few days and will update with more details.</p>
<h2  class="related_post_title">Other Related Tutorials</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database" title="Creating a Form that will Search a MySQL Database">Creating a Form that will Search a MySQL Database</a></li><li><a href="http://teamtutorials.com/windows-tutorials/retrieving-xp-cd-key-from-the-registry" title="Retrieving XP CD-Key from the Registry">Retrieving XP CD-Key from the Registry</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/password-verification-and-strength-checker" title="Password Verification and Strength Checker">Password Verification and Strength Checker</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/create-mysql-databse-with-cpanel-and-phpmyadmin" title="Create MySQL Databse with cPanel and phpMyAdmin">Create MySQL Databse with cPanel and phpMyAdmin</a></li><li><a href="http://teamtutorials.com/windows-tutorials/sharing-a-printer-on-a-windows-network" title="Sharing a Printer on a Windows Network">Sharing a Printer on a Windows Network</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/site-news/site-improvements/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Referential Integrity with MySQL</title>
		<link>http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql</link>
		<comments>http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql#comments</comments>
		<pubDate>Fri, 04 Dec 2009 03:28:11 +0000</pubDate>
		<dc:creator>Mike Maguire</dc:creator>
				<category><![CDATA[Database Tutorials]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[auto increment]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Heidi]]></category>
		<category><![CDATA[insertion]]></category>
		<category><![CDATA[integrity]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[relationship]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2503</guid>
		<description><![CDATA[Referential Integrity is when the rows from one table are “cross-referenced” and verified with those in another table. This tutorial will walk you through using MySQL to enforce this rule.]]></description>
			<content:encoded><![CDATA[<p>Referential Integrity is when the rows from one table are “cross-referenced” and verified with those in another table. This tutorial will walk you through using MySQL to enforce this rule. For example, let’s say you want to have a customer table that contains all the information about your customers and then you want to have an invoice table that will keep the information for invoices. This invoice table will need to be associated to the customer table through the customer id. This relationship would allow you to get all invoices for a customer by searching for their ID. Enforcing referential integrity also will prevent the insertion of a invoice with a customer ID that isn’t in the customer table. Let’s get started.</p>
<p>First, we need to decide what our tables are going to look like. These could vary based on the application you are working with but for this tutorial, we will keep them basic. First, the customer table which doesn’t need to be anything special except ensuring that the type is INNODB and that you set the primary key. </p>
<pre class="brush: sql;">
CREATE TABLE customers
(
   customer_id INT NOT NULL AUTO_INCREMENT,
   name 			VARCHAR(50),
	address  	VARCHAR(60),
	city 			VARCHAR(45),
	state			VARCHAR(2),
	zip 			INT,
   PRIMARY KEY (customer_id)
) TYPE = INNODB;
</pre>
<p>This statement will create a table called customers that contains customer_id, name, address, city, state, and zip columns. It sets the customer id field to the primary key and makes it auto increment for each insert. Next we need to build the invoice table that will house the information about an invoice. (*Note: Normally an invoice would reference an item table to house the items for each invoice, but for sake of remaining simple, we will just have an items field that contains a varchar with comma separated items)</p>
<pre class="brush: sql;">
CREATE TABLE invoice
(
   invoice_id		INT NOT NULL AUTO_INCREMENT,
   customer_id	   INT,
   submit_date    TIMESTAMP NOT NULL,
	total				DECIMAL(10,2),
	tax				DECIMAL(10,2),
	shipping			DECIMAL(10,2),
	items				VARCHAR(100),
   PRIMARY KEY(invoice_id),
   INDEX (customer_id),
   FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
) TYPE = INNODB;
</pre>
<p>This statement will create the invoice table containing invoice id, customer id, submit_date, total, tax, shipping, and items, columns. It sets the primary key to the invoice_id. INDEX tells it to index that column so that when we search the DB for that value it will be faster to return the results. Then the foreign key sets the customer_id of this table to look at the customer_id of the customer table to make sure it exists. </p>
<p>Let’s go ahead and make sure everything works as it is supposed to. First, we will try to insert an invoice for a customer ID that we haven’t inserted yet. Use the following insert statement.</p>
<pre class="brush: sql;">
 insert into invoice (customer_id,submit_date,total,tax,shipping,items) values (1,current_timestamp,54.22,4.50,10.25,'RAM, Power Supply, Case');
</pre>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/12/referential_integrity_with_mysql_01.jpg" alt="referential_integrity_with_mysql_01" title="referential_integrity_with_mysql_01" width="675" height="122" class="alignnone size-full wp-image-2506" /></p>
<p>You should receive an error similar to the above picture. This just means that there is no customer with that ID so you can’t insert an invoice for them. Next, lets add the customer to the customer table.</p>
<pre class="brush: sql;">
insert into customers (name,address,city,state,zip) values ('Mike','100 That One Place','Anywhere','TX',22222);
</pre>
<p>Once you insert that row you should be able to successfully insert the invoice without any issues. The final thing that will referential integrity will enforce is to ensure you can’t delete customers that have invoice attached to them. Let’s try to ensure it works.</p>
<pre class="brush: sql;">
delete from customers where customer_id = 1;
</pre>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/12/referential_integrity_with_mysql_02.jpg" alt="referential_integrity_with_mysql_02" title="referential_integrity_with_mysql_02" width="698" height="122" class="alignnone size-full wp-image-2507" /></p>
<p>You should receive the error above. If you delete the invoice from the invoice table first, you will then be able to delete the customer. That concludes this intro to referential integrity. I hope this is clear to you and thanks for reading.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql" title="Downloading and Installing Heidi SQL">Downloading and Installing Heidi SQL</a></li><li><a href="http://teamtutorials.com/off-topic/launch-of-wootstat-the-top-woot-tracker-on-facebook" title="Launch of WootStat &#8211; The Best Woot Tracker on Facebook">Launch of WootStat &#8211; The Best Woot Tracker on Facebook</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/editing-mysql-data-using-php" title="Editing MySQL Data Using PHP">Editing MySQL Data Using PHP</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/creating-checkboxes-based-on-sql-results" title="Creating Checkboxes Based on SQL Results">Creating Checkboxes Based on SQL Results</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Downloading and Installing Heidi SQL</title>
		<link>http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql</link>
		<comments>http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql#comments</comments>
		<pubDate>Tue, 24 Nov 2009 23:58:23 +0000</pubDate>
		<dc:creator>Mike Maguire</dc:creator>
				<category><![CDATA[Database Tutorials]]></category>
		<category><![CDATA[Windows Tutorials]]></category>
		<category><![CDATA[Heidi]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2480</guid>
		<description><![CDATA[Recently, John and I have been doing a lot of database work using MySQL and have had to connect to several large databases and manage them correctly. This can be a daunting task without a tool to easily do so. This is where Heidi SQL comes in. This tutorial will walk you through downloading, installing, and configuring Heidi SQL to use against your MySQL databases.]]></description>
			<content:encoded><![CDATA[<p>Recently, John and I have been doing a lot of database work using MySQL and have had to connect to several large databases and manage them correctly. This can be a daunting task without a tool to easily do so. This is where Heidi SQL comes in. This tutorial will walk you through downloading, installing, and configuring Heidi SQL to use against your MySQL databases.</p>
<p>Let’s begin by going to <a href="http://www.heidisql.com/download.php">Heidi’s download page</a> and downloading the installer.  </p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_01.jpg" alt="installing_heidi_sql_01" width="368" height="102" class="alignnone size-full wp-image-2481" /></p>
<p>Once the site loads, you will see the link to download the installer as shown above. Click on that link and save the file somewhere on your computer so that you can install it.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_02.jpg" alt="installing_heidi_sql_02" width="73" height="69" class="size-full wp-image-2482" /></p>
<p>This is what the downloaded installer should look like. Double-click on it to start the installation process.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_03.jpg" alt="installing_heidi_sql_03" width="503" height="385" class="size-full wp-image-2483" /></p>
<p>This is the welcome screen. Click on Next.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_04.jpg" alt="installing_heidi_sql_04" width="503" height="385" class="size-full wp-image-2484" /></p>
<p>Read the agreement and check the radial button to accept the terms and then click on next. Click on Next.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_05.jpg" alt="installing_heidi_sql_05" width="503" height="385" class="size-full wp-image-2485" /></p>
<p>This is where to install the files for the application. Leave this default if you are unsure of where to put them. Click on Next.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_06.jpg" alt="installing_heidi_sql_06" height="385" class="size-full wp-image-2486" /></p>
<p>This is where you can pick what it names the start folder in the Start Menu where the application shortcuts will be located. Click on Next.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_07.jpg" alt="installing_heidi_sql_07" width="503" height="385" class="size-full wp-image-2487" /></p>
<p>This screen will allow you to select the additional options you would like such as desktop and quick launch icons. Click on Next.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_08.jpg" alt="installing_heidi_sql_08" width="503" height="385" class="size-full wp-image-2488" /></p>
<p>This is the final screen before installation will occur. Review the settings and ensure they are all correct. Click on next to start the installation.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_09.jpg" alt="installing_heidi_sql_09" width="503" height="385" class="size-full wp-image-2489" /></p>
<p>You will see the above screen while the installation completes. It should only take a few seconds.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_10.jpg" alt="installing_heidi_sql_10" width="503" height="385" class="size-full wp-image-2490" /></p>
<p>Once installation is complete, you will get the screen. Keep the box checked to launch as soon as you hit finish.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_11.jpg" alt="installing_heidi_sql_11" width="464" height="320" class="size-full wp-image-2491" /></p>
<p>This is the connection manager. You can add several databases to the application and manage them by selecting them here. You can also enter the information for a one time connection. To create a connection that you will save, click on New at the top of the screen.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_12.jpg" alt="installing_heidi_sql_12" width="231" height="128" class="size-full wp-image-2492" /></p>
<p>This box will pop up asking what you want to name the session. I named mine Tutorial. Click on OK to continue. It will now take you back to the main screen to fill in the options for the connection.</p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_13.jpg" alt="installing_heidi_sql_13" width="464" height="320" class="size-full wp-image-2493" /></p>
<p>Fill in the fields with the information. The default port for MySQL is 3306. Once the information is all filled out, click on Save + Connect and it will connect you to the database as well as save it for the next time.<br />
*Note: If you are unable to connect, you will need to configure your server to allow incoming connections and/or contact your host. MySQL does not allow remote connection by default and will need to be configured to do so. </p>
<p><img src="http://teamtutorials.com/wp-content/uploads/2009/11/installing_heidi_sql_14.jpg" alt="installing_heidi_sql_14" width="570" height="576" class="size-full wp-image-2494" /></p>
<p>This is the main window of Heidi. The left pane is the server you are connected to as well as the databases it contains. The pane on the right it the information pane that will change based on what you have selected on the left. It will allow you to browse the data, view the settings, and write queries to run against the databases. The bottom pane is the log pane and it will show you what the application is currently during. </p>
<p>That concludes this tutorial. Heidi is a powerful tool with everything you need to manage, manipulate, and update your MySQL database. Play around with it and get used to it and you won’t regret it. Thanks for reading.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/database-tutorials/referential-integrity-with-mysql" title="Referential Integrity with MySQL">Referential Integrity with MySQL</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database" title="Creating a Form that will Search a MySQL Database">Creating a Form that will Search a MySQL Database</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up" title="Sending E-Mail to validate User Sign-up">Sending E-Mail to validate User Sign-up</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/editing-mysql-data-using-php" title="Editing MySQL Data Using PHP">Editing MySQL Data Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql/feed</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Introducing TutorialGrad</title>
		<link>http://teamtutorials.com/other-tutorials/introducing-tutorialgrad</link>
		<comments>http://teamtutorials.com/other-tutorials/introducing-tutorialgrad#comments</comments>
		<pubDate>Thu, 05 Nov 2009 03:44:23 +0000</pubDate>
		<dc:creator>Mike Maguire</dc:creator>
				<category><![CDATA[Off Topic]]></category>
		<category><![CDATA[Other Tutorials]]></category>
		<category><![CDATA[tutorials]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2142</guid>
		<description><![CDATA[Sorry for the lack of posts that we have had recently, but John and I have been working on some new projects and are proud to be able to finally release one of the many active projects that we are working on.]]></description>
			<content:encoded><![CDATA[<p><!--noadsense--><br />
Sorry for the lack of posts that we have had recently, but John and I have been working on some new projects and are proud to be able to finally release one of the many active projects that we are working on. </p>
<p><a href="http://tutorialgrad.com">TutorialGrad</a> is a site that will allow others to post tutorials from their own sites automatically through an RSS feed submission. You go to the site, type in your RSS feed and we do the rest. </p>
<p>We have worked long and thought all features of the process through to make the site the best it can be for users as well as site owners. Some features include:</p>
<ul>
<li><strong>No Framed Content</strong>- We will not frame any content. Leaving the users the full ability to see your tutorials.</li>
<li><strong>No Content Stealing</strong>- We simply post the description on the tutorial and then give the users a direct link to it. They will all land directly on your page. This will give you more legitimate back links.</li>
<li><strong>Auto Posting</strong>- We will look at your RSS feed daily (currently, may change to more frequent with time once we get all the kinks out) and scrape any tutorial in it and post it to the site. </li>
<li><strong>Feed Support</strong>- We actively support RSS and ATOM feeds. If you use something else that you would like to be supported, please let us know.  </li>
<li><strong>Auto Thumbnails</strong>- We take a &#8220;snapshot&#8221; of your site upon submission for the users to see the thumbnail next to the tutorials. </li>
</ul>
<h4>Related Blogs</h4>
<ul class="pc_pingback">
<li class="hdl" style="list-style: none">Related Blogs on <b>tutorials</b></li>
<li><a href="http://www.instantshift.com/2009/11/03/ultimate-round-up-of-60-excellent-gimp-tutorials/">Ultimate Round-Up Of 60 Excellent Gimp <b>Tutorials</b> | <b>Tutorials</b> <b>&#8230;</b></a></li>
<li><a href="http://blogof.francescomugnai.com/2009/11/20-most-wanted-web-design-tutorials/">FrancescoMugnai.com » 20 Most Wanted Web Design <b>Tutorials</b></a></li>
</ul>
<h2  class="related_post_title">Other Related Tutorials</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/html-tutorials/javascript-progress-bar" title="JavaScript Progress Bar">JavaScript Progress Bar</a></li><li><a href="http://teamtutorials.com/programming-tutorials/build-your-first-application-with-vb-express-2005" title="Build your first application with VB Express 2005">Build your first application with VB Express 2005</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/replace-wordpress-search-with-google-search-box" title="Replace Wordpress Search With Google Search Box">Replace Wordpress Search With Google Search Box</a></li><li><a href="http://teamtutorials.com/site-news/update-new-syntax-highlighting-feature" title="Update: New Syntax Highlighting Feature">Update: New Syntax Highlighting Feature</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-access-a-mysql-database-using-php" title="How to Access a MySQL Database Using PHP">How to Access a MySQL Database Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/other-tutorials/introducing-tutorialgrad/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Using jQuery to Reorder a List and Update a Database</title>
		<link>http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database</link>
		<comments>http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database#comments</comments>
		<pubDate>Wed, 30 Sep 2009 21:05:23 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Development Tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Reorder]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2126</guid>
		<description><![CDATA[This tutorial will show you how to use jQuery to allow as list (UL) to be reordered and will also make an AJAX call to a PHP file that will update the database when an item is dropped. I found this useful for a project I was working on recently. The user wanted to be able to change the order that categories displayed in a report. The items are stored in a database with a field called display_order. When the report is generated we simply order by that field.]]></description>
			<content:encoded><![CDATA[<p>This tutorial will show you how to use jQuery to allow as list (UL) to be reordered and will also make an AJAX call to a PHP file that will update the database when an item is dropped. I found this useful for a project I was working on recently. The user wanted to be able to change the order that categories displayed in a report. The items are stored in a database with a field called display_order. When the report is generated we simply order by that field.</p>
<p><strong>This tutorial requires:</strong></p>
<ul>
<li>A web server that handles PHP and MySql or <a href="http://teamtutorials.com/web-development-tutorials/setting-up-a-wamp-server">setup a local  Windows Apache MySql Php web server.</a></li>
<li> <a href="http://jqueryui.com/download"> jQuery and jQuery UI</a>.
<li>Basic HTML/CSS/Javascript knowledge.</li>
</ul>
<p>First you are going to need to <a href="http://jqueryui.com/download" target=="blank">download jQuery Ui.</a> There are plenty of feature within jQuery User Interface that I will not be touching in this tutorial, but for sake of the tutorial download the entire bundle. Also I will be using the default (UI Lightness) theme.<br />
Create a folder for this project on your server. You will need to unzip the jQuery files and put them all in this directory.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-01.gif"><img src="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-01.gif" alt="jquery-reorder-tutorial-01" title="jquery-reorder-tutorial-01" width="700" height="417" class="alignnone size-full wp-image-2129" /></a></p>
<p>Since jQuery comes with an example called index.html I am going to name my file test.php. You can call it whatever you want. Create the file. We will use it later. We&#8217;ll set the database up first.</p>
<p>I like to use <a href=" http://www.heidisql.com/" target="blank">HeidiSQL</a> when working with MySQL. Now on the database server I am going to create a database call test_tutorials, with a table called report_items. Here are the field properties for the report_items table:<br />
<a href="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-02.gif"><img src="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-02.gif" alt="jquery-reorder-tutorial-02" title="jquery-reorder-tutorial-02" width="624" height="159" class="alignnone size-full wp-image-2130" /></a></p>
<p>Now populate your table with some test data. I&#8217;ll insert 6 rows. Also make sure you insert the display order starting from 0.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-03.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-03.jpg" alt="jquery-reorder-tutorial-03" title="jquery-reorder-tutorial-03" width="576" height="235" class="alignnone size-full wp-image-2131" /></a></p>
<p>You can save the changes to your data base and get back to working on test.php. Open the file. We&#8217;ll start with the basic layout. </p>
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Reorder Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Next we&#8217;ll include the stylesheet from our jQuery theme. I&#8217;ll also add some CSS from the example on the jQuery UI website. Then we&#8217;ll include the jQuery javascript files.</p>
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Reorder Test&lt;/title&gt;
&lt;/head&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;development-bundle/themes/base/ui.all.css&quot;/&gt;
&lt;style type=&quot;text/css&quot;&gt;
	#sortable {
		list-style-type: none;
		margin: 0;
		padding: 0;
		width: 60%;
	}
	#sortable li {
		margin: 0 3px 3px 3px;
		padding: 0.4em;
		padding-left: 1.5em;
		font-size: 1.4em;
		height: 18px;
	}
	#sortable li span {
		position: absolute;
		margin-left: -1.3em;
	}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.7.2.custom.min.js&quot;&gt;&lt;/script&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>We are going to pull the intial data from the database. In order to do this we&#8217;ll need to make a database connection. Create a new file called connect.php and create a database connection (obvious added your credentials where needed.</p>
<pre class="brush: php;">
&lt;?php
$servername='localhost'; //no need to change if the file is located on the same server
$dbusername='username'; //your db username
$dbpassword='password'; //your db password
$dbname='database'; //for this tutorial I called my db test_tutorials
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword){
	global $link;
	$link=mysql_connect ($servername,$dbuser,$dbpassword);
	if(!$link){die(&quot;Could not connect to MySQL&quot;);}
	mysql_select_db($dbname,$link) or die (&quot;could not open db&quot;.mysql_error());
}
?&gt;
</pre>
<p>Save connect.php and include it into your test.php file:</p>
<pre class="brush: php;">
&lt;?php include('connect.php');?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Reorder Test&lt;/title&gt;
&lt;/head&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;development-bundle/themes/base/ui.all.css&quot;/&gt;
&lt;style type=&quot;text/css&quot;&gt;
	#sortable {
		list-style-type: none;
		margin: 0;
		padding: 0;
		width: 60%;
	}
	#sortable li {
		margin: 0 3px 3px 3px;
		padding: 0.4em;
		padding-left: 1.5em;
		font-size: 1.4em;
		height: 18px;
	}
	#sortable li span {
		position: absolute;
		margin-left: -1.3em;
	}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.7.2.custom.min.js&quot;&gt;&lt;/script&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Now we need to retrieve the database results and dsiplay them. The items will be display in an un-ordered list. We&#8217;ll add this code to the body.</p>
<pre class="brush: php;">
&lt;body&gt;
	&lt;ul id=&quot;sortable&quot;&gt;
    &lt;?php
	$sql = &quot;select * from report_items order by Category_Display_Order ASC&quot;;
	$query = mysql_query($sql);
	while($row = mysql_fetch_array($query))
	{
		$catid = $row['Category_ID'];
		$catname = $row['Category_Name'];
		echo &quot;&lt;li id='item_$catid' class='ui-state-default'&gt;&lt;span class='ui-icon ui-icon-arrowthick-2-n-s'&gt;&lt;/span&gt;$catname&lt;/li&gt;&quot;;
	}
	?&gt;
    &lt;/ul&gt;
&lt;/body&gt; </pre>
<p>Take not above. When we echo the li we are adding the ui-state-default class which is just a jQuery theme class and the span that we included is an arrow icon also provide with jQuery Ui. Where you see item_$catid, that should be your primary key from your database (this will be used later to reorder items.</p>
<p>Now save the file and load it in your browser. If you followed everything hopefully you will get something like this:<br />
<a href="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-04.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-04.jpg" alt="jquery-reorder-tutorial-04" title="jquery-reorder-tutorial-04" width="700" height="183" class="alignnone size-full wp-image-2132" /></a></p>
<p>Now we&#8217;ll enable the sortable function on our list. Remeber I gave the UL tag an id of sortable. Add this code in the head below your jquery javascript files:</p>
<pre class="brush: jscript;">
&lt;script type=&quot;text/javascript&quot;&gt;
	$(function() {
		$(&quot;#sortable&quot;).sortable({
			placeholder: 'ui-state-highlight',
		});
		$(&quot;#sortable&quot;).disableSelection();
	});
&lt;/script&gt;
</pre>
<p>The code above enables sorting of the UL with the id #sortable. The placeholder: &#8216;ui-state-highlight&#8217; tells jquery we want it to show a place holder of where we can drop the item. In the them I am using it is a yellow box. Make the changes above and save your page. You should now be able to drap and drop your list items.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-05.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-05.jpg" alt="jquery-reorder-tutorial-05" title="jquery-reorder-tutorial-05" width="700" height="206" class="alignnone size-full wp-image-2133" /></a></p>
<p>Now that you have that working we can move to the AJAX call. When an item is dropped, we want to call a php file to update the database. We do that by using the stop: action. So when dragging stops, the script will call the url (updatedb.php) we provided below. To make the AJAZ called, we let jQuery do the work.</p>
<pre class="brush: jscript;">
&lt;script type=&quot;text/javascript&quot;&gt;
	$(function() {
		$(&quot;#sortable&quot;).sortable({
			placeholder: 'ui-state-highlight',
			stop: function(i) {
				placeholder: 'ui-state-highlight'
				$.ajax({
					type: &quot;GET&quot;,
					url: &quot;updatedb.php&quot;,
					data: $(&quot;#sortable&quot;).sortable(&quot;serialize&quot;)});
			}
		});
		$(&quot;#sortable&quot;).disableSelection();
	});
&lt;/script&gt;
</pre>
<p>You see the ajax function above. We tell it what type of request, in this case wer&#8217;ll use get variables. We also tell it what URL, this is a php file we&#8217;ll create to update the database. Then we add the data that will be sent. </p>
<p>The code $(&#8220;#sortable&#8221;).sortable(&#8220;serialize&#8221;)}); will take the items within our ordered list and serialize it. It will take the items of the list, in whatever order they are in, and turn them into a get variable. So for each item it will add it to an array and format is properly to make the call to the PHP file. The data will look like this:<br />
item[]=1&#038;item[]=2&#038;item[]=3&#038;item[]=4&#038;item[]=5&#038;item[]=6</p>
<p>If you want to debug AJAX request I recommend  using Firefox with the Firebug plugin. If you want to see that the request is being attempted and that what the data looks like, you can add an alert box just below the placeholder like this: alert($(&#8220;#sortable&#8221;).sortable(&#8220;serialize&#8221;));<br />
Save the changes to you file we are done with test.php. Here is my full source of test.php up to this point.</p>
<pre class="brush: plain;">
&lt;?php include('connect.php');?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Reorder Test&lt;/title&gt;
&lt;/head&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;development-bundle/themes/base/ui.all.css&quot;/&gt;
&lt;style type=&quot;text/css&quot;&gt;
	#sortable {
		list-style-type: none;
		margin: 0;
		padding: 0;
		width: 60%;
	}
	#sortable li {
		margin: 0 3px 3px 3px;
		padding: 0.4em;
		padding-left: 1.5em;
		font-size: 1.4em;
		height: 18px;
	}
	#sortable li span {
		position: absolute;
		margin-left: -1.3em;
	}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.7.2.custom.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	$(function() {
		$(&quot;#sortable&quot;).sortable({
			placeholder: 'ui-state-highlight',
			stop: function(i) {
				placeholder: 'ui-state-highlight'
				$.ajax({
					type: &quot;GET&quot;,
					url: &quot;updatedb.php&quot;,
					data: $(&quot;#sortable&quot;).sortable(&quot;serialize&quot;)});
			}
		});
		$(&quot;#sortable&quot;).disableSelection();
	});
&lt;/script&gt;
&lt;body&gt;
	&lt;ul id=&quot;sortable&quot;&gt;
    &lt;?php
	$sql = &quot;select * from report_items order by Category_Display_Order ASC&quot;;
	$query = mysql_query($sql);
	while($row = mysql_fetch_array($query))
	{
		$catid = $row['Category_ID'];
		$catname = $row['Category_Name'];
		echo &quot;&lt;li id='item_$catid' class='ui-state-default'&gt;&lt;span class='ui-icon ui-icon-arrowthick-2-n-s'&gt;&lt;/span&gt;$catname&lt;/li&gt;&quot;;
	}
	?&gt;
    &lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Now we need to actually update the database. Create a file called updatedb.php. This file is pretty simple. We take each item in the array and then update the database based on the array position($key) and the primary key ($value)</p>
<pre class="brush: php;">
&lt;?php
include('connect.php');
foreach($_GET['item'] as $key=&gt;$value) {
	mysql_query(&quot;UPDATE report_items SET Category_Display_Order = '$key' WHERE Categoryt_ID ='$value';&quot;);
}
?&gt; </pre>
<p>Save that file and test the reorder. If you have problems and you think it is with the AJAX call. Check fire bug. Here is an example of a bad request in this case I tried to include a file that was not there.<br />
<a href="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-06.jpg"><img src="http://teamtutorials.com/wp-content/uploads/2009/09/jquery-reorder-tutorial-06.jpg" alt="jquery-reorder-tutorial-06" title="jquery-reorder-tutorial-06" width="700" height="277" class="alignnone size-full wp-image-2134" /></a></p>
<p><strong>I hope this tutorial helped someone out</strong></p>
<ul>
<li><a href="http://demos.teamtutorials.com/jquery_reoder_list/list_tutorial.rar">Download Source</a></li>
<li><a href="http://demos.teamtutorials.com/jquery_reoder_list/">View Live Demo (no database interaction)</a></li>
</ul>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/simple-php-website-templates" title="Simple PHP Website Templates">Simple PHP Website Templates</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/hide-and-show-a-div-using-javascript" title="Hide and Show a Div Using Javascript">Hide and Show a Div Using Javascript</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/installing-code-editor" title="Installing Code Editor">Installing Code Editor</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/creating-checkboxes-based-on-sql-results" title="Creating Checkboxes Based on SQL Results">Creating Checkboxes Based on SQL Results</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Site Downtime</title>
		<link>http://teamtutorials.com/site-news/site-downtime</link>
		<comments>http://teamtutorials.com/site-news/site-downtime#comments</comments>
		<pubDate>Fri, 18 Sep 2009 01:50:10 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[Site News and Updates]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=2001</guid>
		<description><![CDATA[As some of you may have noticed the site has been unavailable over the last few days. We are in the process of upgrading to a new server (with 2 quad core Xeon processors and 8gb RAM) and things have not gone as smooth as planned. We will get all the issue straightened out and [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you may have noticed the site has been unavailable over the last few days. We are in the process of upgrading to a new server (with 2 quad core Xeon processors and 8gb RAM) and things have not gone as smooth as planned. We will get all the issue straightened out and try to push the new server to it&#8217;s limits. If you are wondering why we haven&#8217;t posted in the last two month&#8217;s we have both been busy with work and side projects. We hope to post some new tutorials soon.</p>
<h2  class="related_post_title">Other Related Tutorials</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/alternating-table-row-color-with-css-classes" title="Alternating Table Row Color With CSS Classes">Alternating Table Row Color With CSS Classes</a></li><li><a href="http://teamtutorials.com/photoshop-tutorials/adobe-photoshop-cs3-style-icons" title="Adobe Photoshop CS3 Style Icons">Adobe Photoshop CS3 Style Icons</a></li><li><a href="http://teamtutorials.com/photoshop-tutorials/horizontal-menu-%e2%80%93-from-photoshop-to-the-web" title="Horizontal Menu – From Photoshop to the Web">Horizontal Menu – From Photoshop to the Web</a></li><li><a href="http://teamtutorials.com/photoshop-tutorials/red-horizontal-repeating-background" title="Red Horizontal Repeating Background">Red Horizontal Repeating Background</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/simple-xml-to-xhtml-transformation" title="Simple XML to XHTML Transformation">Simple XML to XHTML Transformation</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/site-news/site-downtime/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Sending E-Mail to validate User Sign-up</title>
		<link>http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up</link>
		<comments>http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up#comments</comments>
		<pubDate>Thu, 23 Jul 2009 01:06:51 +0000</pubDate>
		<dc:creator>Mike Maguire</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Development Tutorials]]></category>
		<category><![CDATA[email validation]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[web]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=1996</guid>
		<description><![CDATA[In on of our last tutorials we covered how to <a href="http://teamtutorials.com/web-development-tutorials/validating-email-address-with-php-and-ajax">verify that a user’s email address is formatted correctly</a> as well as verify that it goes to a valid domain name. That is great, but still not a great way to make sure a user actually owns or uses that e-mail address. This tutorial will walk you through one method of sending the user an e-mail upon signing up that gives them a link to activate their account.]]></description>
			<content:encoded><![CDATA[<p>In one of our last tutorials we covered how to <a href="http://teamtutorials.com/web-development-tutorials/validating-email-address-with-php-and-ajax">verify that a user’s email address is formatted correctly</a> as well as verify that it goes to a valid domain name. That is great, but still not a great way to make sure a user actually owns or uses that e-mail address. This tutorial will walk you through one method of sending the user an e-mail upon signing up that gives them a link to activate their account. This will require them to click on the link in the e-mail before they can actually login to the site. Let’s get started. First, let’s create a table to hold our user information. I made a database called teamtutorials on my local server and ran this to create the table.</p>
<pre class="brush: sql;">
CREATE TABLE `users` (
  `user_id` int(7) unsigned NOT NULL auto_increment,
  `display_name` varchar(20) NOT NULL,
  `password` varchar(255) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `email_address` varchar(255) NOT NULL,
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;
</pre>
<p>Now that the table is set up and configured the way we need it to be we can start creating the php files. We will start with the simple form file. This is adduser.php (it doesn’t have to be a php file but I do it so that If I add something later that is php it is already a php file.)</p>
<pre class="brush: xml;">
&lt;form method=&quot;post&quot; action=&quot;register.php&quot; name=&quot;createuser&quot;&gt;
	UserName:&lt;input type=&quot;text&quot; name=&quot;display_name&quot; id=&quot;display_name&quot;/&gt;&lt;br /&gt;
	First Name:&lt;input type=&quot;text&quot; name=&quot;fname&quot; id=&quot;name&quot;/&gt;&lt;br /&gt;
	Last Name:&lt;input type=&quot;text&quot; name=&quot;lname&quot; id=&quot;surname&quot;/&gt;&lt;br /&gt;
	E-mail:&lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot;/&gt;&lt;br /&gt;
	Password:&lt;input type=&quot;password&quot; name=&quot;pass&quot; id=&quot;pass&quot;/&gt;&lt;br /&gt;
	Confirm PW:&lt;input type=&quot;password&quot; name=&quot;pass2&quot; id=&quot;pass2&quot;/&gt;&lt;br /&gt;
	&lt;input type=&quot;submit&quot; value=&quot;Sign Me Up!&quot;/&gt;
&lt;/form&gt;
</pre>
<p>Next, we will need to create the register.php file that will be called when the form is submitted. </p>
<pre class="brush: php;">
&lt;?php
if ((!isset($_POST['display_name']))||(!isset($_POST['fname']))||(!isset($_POST['lname']))||(!isset($_POST['email']))||(!isset($_POST['pass']))){
		header('Location: http://www.teamtutorials.com');
	}
</pre>
<p>First, we start the php tag and then check for the POST variables to be set. If any of them are not set it will send them back to the creation form.</p>
<pre class="brush: php;">
else
	{
		$servername='localhost';
		$dbusername='dbusername';
		$dbpassword='dbpassword';
		$dbname='full db name';
		global $link;
		$link=mysql_connect ($servername,$dbuser,$dbpassword);
		if(!$link){die(&quot;Could not connect to MySQL&quot;);}
		mysql_select_db($dbname,$link) or die (&quot;could not open db&quot;.mysql_error());
</pre>
<p>If all of the POST variables are set we continue by setting the server variables and creating the database connection. Ensure that you change the variables to your server access information.</p>
<pre class="brush: php;">
		$display_name = $_POST['display_name'];
		$password = $_POST['pass'];
		$first_name = $_POST['fname'];
		$last_name = $_POST['lname'];
		$email_address = $_POST['email'];
		$sha_password = sha1($password);
		$hash_string = hash('md5',$display_name);
</pre>
<p>This just stores all the values into variables that are easier to work with so we don’t have to call the POST global every time. We sha1 the password because it is not reversible so it makes it the most secure. The hash_string is a md5 hash value of the display_name so that it isn’t easily readable. We use the display name because it will be unique and specific to the user. </p>
<pre class="brush: php;">
$query = &quot;insert into `users` values(Null,'$display_name','$sha_password','$first_name','$last_name','$email_address',0,1,CURRENT_TIMESTAMP,Null,'$company_name',8);&quot;;
		mysql_query($query) or die (&quot;Error in query: $query &quot; . mysql_error());
		$user_id = mysql_insert_id();
</pre>
<p>Next, we insert a row into our table that we made using the variables that were passed from the form. We also get the ID of the inserted row so we know what the user id will be.</p>
<pre class="brush: php;">
		$headers = &quot;From: activations@teamtutorials.com \r\n&quot;;
		$validate_link = &quot;http://teamtutorials.com/validate.php?id=$user_id&amp;string=$hash_string&quot;;
		$email_body = &quot;Thank-you for signing up on TeamTutorials. Click on the link below to complete your registration. If you have any issues completing the verfication please let us know. \n\n $validate_link \n\n TeamTutorials Staff&quot;;
</pre>
<p>This builds all the information needed to generate an e-mail using php. The headers sets the from field so that the email will be from a user (if you don’t set this it will be nobody (the user which apache runs under in linux). The validate link is a link to the file that will validate the user when they click on it. The \n command is the php new line command.</p>
<pre class="brush: php;">
		if (mail($email_address,&quot;TeamTutorials Sign-Up&quot;,$email_body,$headers)){
			echo &quot;Email has been sent to &quot;.$email_address.&quot;. Please check your e-mail for steps to activate your account. Check your spam folder as sometimes these 		e-mail get marked as spam. If you still do not see your e-mail, please &lt;a href='http://teamtutorials.com/sendemail.php?function=validation&amp;id=$user_id'&gt; Click Here&lt;/a&gt; to resend.&quot;;
		}
		else
		{
			echo &quot;There was an error sending an e-mail to your e-mail address. Please contact us to let us know of the issue.&quot;;
		}
	}
?&gt;
</pre>
<p>Finally we attempt to send the e-mail and echo success or failure. That concludes this file. Finally we need to make the file that will validate the user when they click on the link in the e-mail that we just sent them. This file is validate.php.</p>
<pre class="brush: php;">
&lt;?php
if (isset($_GET['id']))&amp;&amp;(isset($_GET['id'])){
	$id = $_GET['id'];
	$hashstring = $_GET['string'];
	$storedhashvalue = &quot;&quot;;
</pre>
<p>First we make sure the values are in the url that we are expecting. At the end of the file we will re-direct them to the home page if these values aren’t set.</p>
<pre class="brush: php;">
$query = &quot;select user_id,display_name from `users` where user_id=$id;&quot;;
	$result = mysql_query($query) or die (&quot;Error in query: $query &quot; . mysql_error());
	$row = mysql_fetch_assoc($result);
	$storedhashvalue = hash('md5',$row['display_name']);
	mysql_free_result($result);
</pre>
<p>These lines run a query against the table to get the information for the user based on the user id in the url. It then re-hashes the value so that we can compare the on in the url to match.</p>
<pre class="brush: php;">
	if ($storedhashvalue == $hashstring){
		$query = &quot;update `users` set active_flag=1 where user_id=$id;&quot;;
		$result = mysql_query($query) or die (&quot;Error in query: $query &quot; . mysql_error());
		echo &quot;Your account has been activated. Please &lt;a href='http://teamtutorials.com/login.php&gt;Click Here To Login&lt;/a&gt;&quot;;
	}
	else
	{
		echo &quot;Your account could not be verified. Please verify that the link has not been modified from the e-mail. If it still does not work, please contact us.&quot;;
	}
</pre>
<p>If the values match we update the database to see the user as active and if it doesn’t work we tell them that it failed. </p>
<pre class="brush: php;">
}
else
{
header('Location: http://www.teamtutorials.com');
}
?&gt;
</pre>
<p>Finally we re-direct the user if the variables are not in the url. Now if you go to adduser.php and fill out the form and hit submit. It will send whatever e-mail you put in the form an e-mail with a link in it. Click on the link to activate the user. That concludes this tutorial. If you have any questions, please leave it in the comments. Thanks for viewing.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/php-tutorials/creating-a-form-that-will-search-a-mysql-database" title="Creating a Form that will Search a MySQL Database">Creating a Form that will Search a MySQL Database</a></li><li><a href="http://teamtutorials.com/windows-tutorials/downloading-and-installing-heidi-sql" title="Downloading and Installing Heidi SQL">Downloading and Installing Heidi SQL</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database" title="Using jQuery to Reorder a List and Update a Database">Using jQuery to Reorder a List and Update a Database</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php" title="How to Parse a CSV File Using PHP">How to Parse a CSV File Using PHP</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>How to Parse a CSV File Using PHP</title>
		<link>http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php</link>
		<comments>http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php#comments</comments>
		<pubDate>Thu, 18 Jun 2009 22:07:06 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Development Tutorials]]></category>
		<category><![CDATA[comma separated value. fgetcsv]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[fopen]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[php]]></category>
		<guid isPermaLink="false">http://teamtutorials.com/?p=1850</guid>
		<description><![CDATA[In this tutorial you will learn a simple way to parse CSV files using PHP and output the text of the fields you need. This tutorial is not going to go into what you can do with the data once you get it from the CSV as the possibilities are endless. We will simply show you how to get the data into an array in PHP.]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you will learn a simple way to parse CSV files using PHP and output the text of the fields you need. This tutorial is not going to go into what you can do with the data once you get it from the CSV as the possibilities are endless. We will simply show you how to get the data into an array in PHP.</p>
<p><a href="http://teamtutorials.com/wp-content/uploads/2009/06/test.csv"><br />
First you can download our test.csv file here. </a></p>
<p>Now we are going to open the file for reading using the fopen() function in PHP. Will will specify the location of the file and also the mode in which we want to open it. In this case we are using the &#8220;R&#8221; mode which stands for read. Other possible uses of this function can be found on the <a href="http://us2.php.net/manual/en/function.fopen.php">manual page for fopen()</a>.</p>
<pre class="brush: php;">
&lt;?php
$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);
?&gt;
</pre>
<p>Next we are going to create a while loop. We will use this loop to parse each line of the CSV.</p>
<pre class="brush: php;">
&lt;?php
$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);
while () {
}
?&gt;
</pre>
<p>Next we are going to use the <a href="http://us3.php.net/manual/en/function.fgetcsv.php">fgetcsv() function </a>to &#8220;split&#8221; each line of our CSV file into an array called $data. When there are no lines left the condition will be false and will end the loop. In the fgetcsv() function we will pass the file handle, length (optional) and the separating character (which is a comma in this case.<br />
test</p>
<pre class="brush: php;">
&lt;?php
$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);
while (($data = fgetcsv($handle, 5000, &quot;,&quot;)) !== FALSE) {
}
?&gt;
</pre>
<p>*note: If you data fields are encapsulated using a character such as &#8211; (for ease of example) you can specify the enclosure filed like so: fgetcsv($handle, 5000, &#8220;,&#8221;,&#8221;-&#8221;). By default the enclosure character is a double quote, but the function will also handle no enclosure character.</p>
<p>Next all we have to do is echo the fields that we want to see. We will use the field number in the array to do this. I will echo the second value in the CSV.</p>
<pre class="brush: php;">
&lt;?php
$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);
$row = 1;
while (($data = fgetcsv($handle, 5000, &quot;,&quot;)) !== FALSE) {
	echo $data[2];
	echo &quot;&lt;/br&gt;&quot;;
}
?&gt;
</pre>
<p>Another note. If you do not know which value the field you are looking for will be in the array just print the array like this:</p>
<pre class="brush: php;">
&lt;?php
$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);
while (($data = fgetcsv($handle, 5000, &quot;,&quot;)) !== FALSE) {
	echo &quot;&lt;pre&gt;&quot;;
	print_r($data);
	echo &quot;&lt;pre&gt;&quot;;
}
?&gt;
</pre>
<p>As you can see it is pretty simple to parse csv using this method. You may have issue with memory if you try to open large files using fopen. in that case you will want to read the file line-by-line, but that is another tutorial.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database" title="Using jQuery to Reorder a List and Update a Database">Using jQuery to Reorder a List and Update a Database</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up" title="Sending E-Mail to validate User Sign-up">Sending E-Mail to validate User Sign-up</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/editing-mysql-data-using-php" title="Editing MySQL Data Using PHP">Editing MySQL Data Using PHP</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-get-a-users-geo-location" title="How to Get a Users Geo Location?">How to Get a Users Geo Location?</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
