<?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>Web Design Talk &#187; MySQL</title>
	<atom:link href="http://www.web-design-talk.co.uk/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-design-talk.co.uk</link>
	<description>Web Design &#38; Development Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:50:39 +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>Displaying a Breadcrumb Navigation for Multiple Sub Categories via PHP</title>
		<link>http://www.web-design-talk.co.uk/188/displaying-a-breadcrumb-navigation-for-multiple-sub-categories-via-php-2/</link>
		<comments>http://www.web-design-talk.co.uk/188/displaying-a-breadcrumb-navigation-for-multiple-sub-categories-via-php-2/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 10:57:43 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[phpmyadmin]]></category>
		<category><![CDATA[multiple sub categories]]></category>
		<category><![CDATA[php categories]]></category>
		<category><![CDATA[recursive functions]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=188</guid>
		<description><![CDATA[While making an ecommerce store I ran into the issue of displaying a category breadcrumb. Usually this is easy as I always recommend keeping the numbers of categories and sub categories to a maximum of 1 level deep. E.g. Tshirts &#62; Red tshirts. For this store, due to the sheer number of products the owner [...]]]></description>
			<content:encoded><![CDATA[<p>While making an ecommerce store I ran into the issue of displaying a category breadcrumb. Usually this is easy as I always recommend keeping the numbers of categories and sub categories to a maximum of 1 level deep. E.g. Tshirts &gt; Red tshirts. For this store, due to the sheer number of products the owner wanted to add up to seven category levels. While there are many tutorials on this floating about, they all seem tocus on displaying the whole tree &#8211; very useful for a sitemap page, but not a breadcrumb navigation. While it would have been possible to hard code several if statements into the category listing page, this seemed a bit messey and would cause problems if an eighth level was added.</p>
<p>For the breadcrumb naviagtion I needed a function to displaying the path to a given category ID, sometimes refered to as a single branch or node. I was using a simple parent child database table structure:</p>
<p><a href="http://www.web-design-talk.co.uk/wp-content/uploads/2010/03/php-multiple-sub-categories.jpg"><img class="size-full wp-image-189 alignnone" style="border: 1px solid #cccccc;" title="php-multiple-sub-categories" src="http://www.web-design-talk.co.uk/wp-content/uploads/2010/03/php-multiple-sub-categories.jpg" alt="php multiple=" /></a></p>
<p>While the latter may seem fairly trival I really couldn&#8217;t get my head around the problem. After a bit of Googling, I found a function that was a good starting point so adapted it to fit my problem (the function is part of a categoru class):</p>
<pre class="brush: php;">
function getCategoryTreeIDs($catID) {
		$row = DB::getInstance()-&gt;query(&quot;SELECT parent FROM categories WHERE ID = '$catID'&quot;)-&gt;fetch();
		$path = array();
		if (!$row['parent'] == '') {
			$path[] = $row['parent'];
			$path = array_merge($this-&gt;getCategoryTreeIDs($row['parent']), $path);
		}
		return $path;
	}
</pre>
<p>The function simply returns an array of category IDs. E.g 20, 28. So from the array I&#8217;d know that the tree would go as Home &gt; Cat ID 20 &gt; Cat ID 28.</p>
<h2 style="font-size:12px;border:0;padding-left:0;">Displaying the Breadcrumb Navigation</h2>
<p>To display the actual breadcrumb I simply added the following method, that loops through the array of ID we just generated. The getNameLink method simply generates an SEO feindly website URL for the category, inside the &lt;a&gt; tag.</p>
<pre class="brush: php;">
function showCatBreadCrumb($catID) {

		$array = $this-&gt;getCategoryTreeIDs($catID);

		$numItems = count($array);
		for ($i = 0; $i&lt;=$numItems-1; $i++) {
			echo $this-&gt;getNameLink($array[$i]) . ' &amp;raquo; ';
		}
	}
</pre>
<p>The result is a nicely formatted breadcrumb (to use our tshiorts example again):</p>
<p>Home &amp;rquao; Clothes &amp;rquao; tshirts &amp;rquao;  Mens &amp;rquao; Red tshirts &amp;rquao; Offensive &amp;rquao;</p>
<h2 style="font-size:12px;border:0;padding-left:0;">A recursive function inside a loop, are you insane?</h2>
<p>Some of you may have noticed that the function used to generate the category IDs is called recursively. This generally considered bad practice, for large data sets due to performance issues. However, for the current use this isn&#8217;t an issue. I know for a fact that client won&#8217;t be adding categories more than several levels deep, so performance really isn&#8217;t an issue in my eyes here. Maybe if we had hundreds of categories, but for several it&#8217;s really a non issue in my opinion.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 185px; width: 1px; height: 1px; overflow: hidden;">
<pre>getCategoryTreeIDs</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/188/displaying-a-breadcrumb-navigation-for-multiple-sub-categories-via-php-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Cheatsheet &#8211; Useful MySQL Queries</title>
		<link>http://www.web-design-talk.co.uk/128/mysql-cheatsheet-useful-mysql-queries/</link>
		<comments>http://www.web-design-talk.co.uk/128/mysql-cheatsheet-useful-mysql-queries/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 19:06:50 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[sample mysql queries]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=128</guid>
		<description><![CDATA[Page rank 5 Page rank 5 Page rank 5 Page rank 5 What follows is a list of some very useful MySQL queries for use in projects, enjoy. I won&#8217;t go into detail how and why they work, I&#8217;ll leave that up to you /*** select records from the previous day ***/ SELECT * FROM [...]]]></description>
			<content:encoded><![CDATA[<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit; border: 0px 1px 1px 0px none solid solid none -moz-use-text-color #e0e0e0 #e0e0e0 -moz-use-text-color;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit; border: 0px 0px 1px none none solid -moz-use-text-color -moz-use-text-color #e0e0e0;">
<div style="border: 0pt none; margin: 0pt; padding: 0pt; background-color: transparent;"></div>
</td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit; border: 1px 0px 1px 1px solid none solid solid #e0e0e0 -moz-use-text-color #e0e0e0 #e0e0e0;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit; border: 0px 0px 0px 1px none none none solid -moz-use-text-color -moz-use-text-color -moz-use-text-color #e0e0e0;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit; border: 0px 0px 0px 1px none none none solid -moz-use-text-color -moz-use-text-color -moz-use-text-color #e0e0e0;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit; border: 0px 0px 0px 1px none none none solid -moz-use-text-color -moz-use-text-color -moz-use-text-color #e0e0e0;" align="center">
<div style="border: 1px solid #e0e0e0; margin: 0px 0px 1px 1px; padding: 0px; width: 38px; height: 4px; visibility: inherit;"></div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit;"></td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit;" align="center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit;"></td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit;" align="center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<div id="rank_jit_com_main" style="border: 0px none; margin: 0px; padding: 0px; position: absolute; top: 126px; left: 519px; line-height: normal; background-color: transparent; z-index: 99999; width: 70px; height: 46px; visibility: hidden;">
<div style="visibility: inherit;">
<table style="margin: 0pt; padding: 0px; width: 70px; height: 36px; background-color: transparent; font-family: Lucida Grande,sans-serif; font-size: 9px; z-index: 200000; border-collapse: separate; visibility: inherit;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr style="background-color: transparent;">
<td style="margin: 0px; padding: 0px; height: 16px; background-color: transparent; text-align: center; vertical-align: middle; visibility: inherit;"></td>
<td style="margin: 0px; padding: 0px; width: 20px; height: 16px; text-align: center; vertical-align: middle; background-color: white; visibility: inherit;" colspan="2" align="center"><img id="rank_jit_pin" style="border: 0px none; margin: 0px; padding: 0px; display: inline; cursor: pointer; width: 16px; height: 16px; vertical-align: bottom; visibility: inherit; background-color: transparent;" src="http://www.rankjit.com/extension/img/pin.gif" alt="" /></td>
</tr>
<tr style="margin: 0px; padding: 0px; background-color: white; visibility: inherit; border: 1px 0px 0px solid none none #e0e0e0 -moz-use-text-color -moz-use-text-color;">
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; color: #000000; font-size: 9px; font-weight: normal; text-align: center; vertical-align: middle; height: 10px; background-color: white; visibility: inherit;" align="center">Page rank</td>
<td style="margin: 0px; padding: 0px; font-family: Lucida Grande,sans-serif; text-align: center; vertical-align: middle; background-color: white; border-spacing: 1px; font-size: 14px; width: 20px; height: 20px; color: white; visibility: inherit;" rowspan="2" align="center" valign="middle">
<div style="border: 0px none; margin: 1px; padding: 0px; width: 20px; height: 20px; background-color: #848484; visibility: inherit;">
<div id="rank_jit_resultContainer" style="border: 0px none; margin: 0px; padding: 0px; background-color: transparent; width: 20px; height: 20px; line-height: 20px; vertical-align: middle; color: white; visibility: inherit; text-align: center;">5</div>
</div>
</td>
</tr>
<tr>
<td style="margin: 0px; padding: 0px; height: 10px; vertical-align: middle; background-color: white; visibility: inherit;" align="center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border: 0pt none; margin: 0pt; padding: 0pt; overflow: hidden; z-index: 9999; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: auto; top: 39.5px; line-height: normal; background-color: transparent; visibility: inherit; width: 10px; height: 10px; right: 50px;"><img style="border: 0pt none; margin: 0pt; padding: 0pt; font-style: normal; font-weight: normal; font-family: trebuchet ms,arial,helvetica,sans-serif; float: none; position: absolute; left: 0px; top: 0px; line-height: normal; background-color: transparent; visibility: inherit; z-index: 99999;" src="http://www.rankjit.com/extension/img/pointer.gif" alt="" /></div>
</div>
<p>What follows is a list of some very useful MySQL queries for use in projects, enjoy. I won&#8217;t go into detail how and why they work, I&#8217;ll leave that up to you <img src='http://www.web-design-talk.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="brush: sql;">
/*** select records from the previous day ***/
SELECT * FROM users WHERE TO_DAYS(last_login) = ( TO_DAYS(NOW()) - 1 )

/*** select records from last 90 minutes ***/
SELECT* FROM users DATE_SUB(NOW(),INTERVAL 90 MINUTE);

/*** select records from last 1hr 5 mins ***/
SELECT DATE_ADD('1970-01-01 12:00:00', INTERVAL CAST(6/4 AS DECIMAL(3,1)) HOUR_MINUTE);

/*** select records from last hour ***/
select DATE_SUB(NOW(), INTERVAL 1 HOUR);

/*** using the SIGN function to mark a number as positive, negative or null ***/
SELECT backlist, SIGN(backlist) AS user_to_backlist
FROM users
WHERE user_banned IS NOT NULL;

/*** select records from last week ***/
select DATE_SUB(NOW(), INTERVAL 1 WEEK);

/*** get the last day of next month ***/
SELECT LAST_DAY('2006-03-06' + INTERVAL 1 MONTH) AS last_day;

/*** select unique records only ***/
SELECT user_name FROM users GROUP BY users HAVING ( COUNT(user_name) = 1 );

/*** select records from one table that are in another table i.e. all the customers that have placed an order ***/
SELECT DISTINCT cust.customer_id, cust.customer_name
FROM cust INNER JOIN orders ON cust.customer_id = orders.customer_id;

/*** insert data from one table into another ***/
INSERT INTO customers(customer_id, customer_name)
SELECT cus_key, cus_name
FROM customers_2 WHERE customer_name LIKE 'W%';

/*** update information based upon a seperate table ***/
UPDATE cust SET status = '1'
FROM orders WHERE orderdate &gt; '2009-01-01' and orders.customer_id = cus.customer_id;

/*** classic self join example - who is an emoployees manager ***/
SELECT emp.empID, emp.Name, emp.Salary, managers.Name AS manager_name
FROM emp
LEFT JOIN emp AS manager_name
ON emp.ManagerID = Manager.EmployeeID
WHERE (emp.empID= '123456');

/*** using UNION to combine results from multiple queries into a single table ***/
SELECT users.name
FROM users WHERE (users.name BETWEEN 'A%' AND 'M%')
UNION
SELECT banned_users.name FROM banned_users
WHERE (banned_users.name BETWEEN 'A%' AND 'M%');

/*** concatenate column data into a single column ***/
SELECT CONCAT(emp.firstname, '-', emp.lastname) AS emp_full_name FROM emp;

/*** select count of records for each hour ***/
SELECT HOUR(last_login) AS last_login_hour, COUNT(*) AS the_count FROM users GROUP BY HOUR(last_login);

/*** import a csv file ***/
LOAD DATA INFILE '/path/xxx.csv' INTO users_table csv_test_table FIELDS TERMINATED BY ',' LINES TERMINATED BY &quot;\n&quot; (user_name, access_level , user_email);

/*** display current mysql user ***/
SELECT USER();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/128/mysql-cheatsheet-useful-mysql-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding Unlimited Form Fields With JQuery and Saving to a Database</title>
		<link>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/</link>
		<comments>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 20:36:49 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[associative array]]></category>
		<category><![CDATA[dynamic form fields]]></category>
		<category><![CDATA[pgp]]></category>
		<category><![CDATA[sprintf]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=58</guid>
		<description><![CDATA[In this article I'll discuss how to add an unlimited number of additional form elements to a form and then save to a database. The latter part is the key here as a variety of tutorials exist on adding form elements, but I have yet to see anywhere that actually explains how to manipulate these added form fields. For example, how to get values to store them in a MySQL datbase. In the example we'll have a simple user signup form where the user can add multiple fields to describe their favourite websites.  The basic Form HTML is as follows (nothing amazing, just a simple html form):]]></description>
			<content:encoded><![CDATA[<p>In this article I&#8217;ll discuss how to add an unlimited number of additional form elements to a form and then save to a database. The latter part is the key here as a variety of tutorials exist on adding form elements, but I have yet to see anywhere that actually explains how to manipulate these added form fields. For example, how to get values to store them in a MySQL datbase. In the example we&#8217;ll have a simple user signup form where the user can add multiple fields to describe their favourite websites.  The basic Form HTML is as follows (nothing amazing, just a simple html form):</p>
<pre class="brush: xml; highlight: [10,11,12];">
&lt;script src=&quot;js/jquery.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;h1&gt;New User Signup&lt;/h1&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;

  &lt;label for=&quot;name&quot;&gt;Username:&lt;/label&gt;
  &lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
  &lt;label for=&quot;name&quot;&gt;Password:&lt;/label&gt;
  &lt;input id=&quot;password&quot; name=&quot;password&quot; type=&quot;text&quot; /&gt;

   &lt;div id=&quot;container&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;&lt;span&gt;» Add your favourite links.....&lt;/span&gt;&lt;/a&gt;
   &lt;/div&gt;

   &lt;input id=&quot;go&quot; class=&quot;btn&quot; name=&quot;btnSubmit&quot; type=&quot;submit&quot; value=&quot;Signup&quot; /&gt;
&lt;/form&gt;
</pre>
<p>The only part that isn&#8217;t standard is highlighted above. This is simply the link users click to add additional form fields on the fly. To make that happen we&#8217;ll need some <a href="http://www.JQuery" target="_blank">JQuery</a>:</p>
<pre class="brush: jscript;">
var count = 0;
$(function(){
	$('p#add_field').click(function(){
		count += 1;
		$('#container').append('&lt;strong&gt;Link #' + count + '&lt;/strong&gt;'+ '&lt;input id=&quot;field_' + count + '&quot; name=&quot;fields[]' + '&quot; type=&quot;text&quot; /&gt;' );
	});
});</pre>
<p><span id="more-58"></span></p>
<p>Jquery makes it very easy to add for elements using the <a href="http://docs.jquery.com/Manipulation/append" target="_blank">append </a>method. As we need to assign a unique name to each additional input (as we are allowing the user to add an indefinite amount of inputs) I&#8217;ll use a variable to keep track of the number of inputs added. Whenever an input is added, the counter variable increments by one. Another thing to notice is how the fields are named using &#8216;txt[]&#8216;. This creates an array of field names. So now when we click the &#8216;add_field&#8217; link, a new input field is appended to the contianer div &#8211; <a href="http://www.web-design-talk.co.uk/examples/2/1/" target="_self">view example</a>.</p>
<p>The only other order of business is to save this information to a database. This would be easy if we had static field names for the whole form, but we don&#8217;t. We have the following:</p>
<pre class="brush: xml;">&lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;password&quot; name=&quot;password&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_1&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_2&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_3&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
.
.
.
etc.</pre>
<p>To capture this dynamic fields we&#8217;ll need to use a bit of PHP to loop through our posted array and saving the data using a many-many relationships using the following database structure &#8211; allowing a user to be associated with an unlimited number of websites:</p>
<div class="wp-caption alignnone" style="width: 510px"><img title="Data Structure For Users And Sites" src="http://www.web-design-talk.co.uk/images/users_sites.jpg" alt="Many-Many Data Structure For Users And Sites" width="500" height="125" /><p class="wp-caption-text">Many-Many Data Structure For Users And Sites</p></div>
<p>The following PHP code will grab our static and dynamic form values and insert into the above data structure. We&#8217;ll be using a class for the database to minimise the amount of code written. Firstly we&#8217;ll check if the forma has actually been submitted, grab the static values (username and password) and insert these into our users table:</p>
<pre class="brush: php;">//If form was submitted
if (isset($_POST['btnSubmit'])) {

	//create instance of database class
	$db = new mysqldb();
	$db-&amp;gt;select_db();

	//Insert static values into users table
	$sql_user = sprintf(&quot;INSERT INTO users (Username, Password) VALUES ('%s','%s')&quot;,
						mysql_real_escape_string($_POST['name']),
						mysql_real_escape_string($_POST['password']) );
	$result_user = $db-&amp;gt;query($sql_user);</pre>
<p>PHP&#8217;s <a href="http://www.php.net/sprintf">sprintf</a> function was used to format the sql query. I also escape input to prevent <a href="http://en.wikipedia.org/wiki/SQL_injection">nasty things from happening</a>. Now we need to check if the user has actually added any favourite websites in order to prevent a php error if we try to find non-existant values, if they have added a field we&#8217;ll need the user id number previously inserted into the users table, as we&#8217;ll need to insert this elsewhere later on:</p>
<pre class="brush: php;">//Check if user has actually added additional fields to prevent a php error
	if ($_POST['fields']) {

		//get last inserted userid
		$inserted_user_id = $db-&amp;gt;last_insert_id();</pre>
<p>Next we loop through the added form field values. This is where naming the field as in our append function as &#8216;txt[]&#8216; comes in, as all fields we added are submiited as an array called txt. Using a for each loop we can assign values to all dynamically added fields and save them to database. The $key is simply the fields name (field_1) and the $key is our actual value entered for that field. Now we know this, we can easily generate the sql statements within the loop. Firstly we add the website into the websites table, get the id of the inserted website and insert this into the the link table, using the userid from before. To finish, we display a confirmation message and disconnect from the database:</p>
<pre class="brush: php;">//Loop through added fields
		foreach ( $_POST['fields'] as $key=&amp;gt;$value ) {

			//Insert into websites table
			$sql_website = sprintf(&quot;INSERT INTO websites (Website_URL) VALUES ('%s')&quot;,
						    	   mysql_real_escape_string($value) );
			$result_website = $db-&amp;gt;query($sql_website);
			$inserted_website_id = $db-&amp;gt;last_insert_id();

			//Insert into users_websites_link table
			$sql_users_website = sprintf(&quot;INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')&quot;,
						    	   mysql_real_escape_string($inserted_user_id),
								   mysql_real_escape_string($inserted_website_id) );
			$result_users_website = $db-&amp;gt;query($sql_users_website);

		}

	} else {

		//No additional fields added by user

	}
	echo &quot;
&lt;h1&gt;User Added, &lt;strong&gt;&quot; . count($_POST['fields']) . &quot;&lt;/strong&gt; website(s) added for this user!&lt;/h1&gt;
&quot;;

	//disconnect mysql connection
	$db-&amp;gt;kill();</pre>
<p>As a result, our database should be populated as follows (these are screenshots from phpmyadmin):</p>
<div class="wp-caption alignnone" style="width: 510px"><img class=" " title="Many to many relationship" src="http://www.web-design-talk.co.uk/images/users_websites_data.jpg" alt="All our data tables are populated and the correct website and user id are inserted into the link table" width="500" height="278" /><p class="wp-caption-text">All our data tables are populated and the correct website and user id are inserted into the link table</p></div>
<p>You can view the <a href="http://www.web-design-talk.co.uk/examples/2/index-source.php">source of php file here</a> or <a href="http://www.web-design-talk.co.uk/examples/2/adding-form-elements-with-jquery.zip">download the original source files</a> (you&#8217;ll need to open classes/db.class.php and populate the sql variables with your own database details). The MySQL source to create the tables is also included in the source files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>
