<?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>Doing My Best... a Web Developers Blog &#187; PHP</title>
	<atom:link href="http://www.iwilldomybest.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iwilldomybest.com</link>
	<description>Ranging from PHP &#38; MySQL to Random Crap from the internet, I got it all here.</description>
	<lastBuildDate>Sun, 31 Jan 2010 01:46:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP Tip: Handling Form Data</title>
		<link>http://www.iwilldomybest.com/2010/01/php-tip-handling-form-data/</link>
		<comments>http://www.iwilldomybest.com/2010/01/php-tip-handling-form-data/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 01:45:41 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/?p=94</guid>
		<description><![CDATA[One of the most common tasks of a Web Dev, has down, is handling form data. As a web developer you'll create hundreds of forms and in turn, write hundreds of pieces of code to handle your form data. Early on this became the worse part of my job. So I've create a super quick [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most common tasks of a Web Dev, has down, is handling form data. As a web developer you'll create hundreds of forms and in turn, write hundreds of pieces of code to handle your form data.<br />
<span id="more-94"></span><br />
Early on this became the worse part of my job. So I've create a super quick way to handle post data:</p>
<pre>

$valid = true; //innocent until proven guilty
$errors = array(); // errors array
$clean = array();

foreach($_POST as $key => $val)
{
	switch($key)
	{
		case 'first_name':
			// do validation
			if(validateString($val) == false)
			{
				$valid = false; // we're no longer valid.
				$errors[] = "You're missing your first name!"; // prime up an error message for our user
			}
			else
			{
				$clean[$key] = $val;
			}
			break;
		case 'last_name':
			// do validation
			break;
	}
}

if($valid == false)
{
	// show the form again, but with the error message
}
else
{
	// handle our clean array anyway we want (usually a DB insert)
}

?>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2010/01/php-tip-handling-form-data/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Modrewrite and Short Urls &#8211; What you really want to know.</title>
		<link>http://www.iwilldomybest.com/2009/01/modrewrite-and-short-urls-htaccess-file-tip/</link>
		<comments>http://www.iwilldomybest.com/2009/01/modrewrite-and-short-urls-htaccess-file-tip/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 11:07:44 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mod rewrite]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/?p=77</guid>
		<description><![CDATA[I recently opened up an old .htaccess and I was struck with the memory of how much it sucked trying to Google search for some real, working, answers on making short urls. So here you go, my quick tip on what you really want to know about mod_rewrite. If you want an actual explanation of [...]]]></description>
			<content:encoded><![CDATA[<p>I recently opened up an old <a title="WikiPedia - Htaccess" href="http://en.wikipedia.org/wiki/Htaccess" target="_blank">.htaccess</a> and I was struck with the memory of how much it sucked trying to Google search for some real, working, answers on making short urls.</p>
<p>So here you go, my quick tip on what you really want to know about <a title="ApacheDocs - Mod_rewrite" href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html" target="_blank">mod_rewrite</a>.</p>
<p><span id="more-77"></span></p>
<p>If you want an actual explanation of what is going on here it's at the bottom.</p>
<p><strong>Solution 1:</strong></p>
<p><code>File: .htaccess</code></p>
<pre>Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) /index.php [L]</pre>
<p><code>File: index.php</code><br />
<code><br />
&lt;?php<br />
$urlArray = explode("/",$_SERVER['REQUEST_URI']);<br />
array_shift($urlArray); // Gets rid of the blank first param.<br />
?&gt;<br />
</code></p>
<p>Now you have an array with all of your parts, and you can map them accordingly.</p>
<p><strong>Solution 1.a (more elegant):</strong><br />
<code>File: .htaccess</code></p>
<pre>&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
&lt;/IfModule&gt;</pre>
<p>That version just checks to make sure the module is installed.</p>
<p><strong>Solution 2:</strong><br />
If you have just a couple of pages that you want to look prettier:</p>
<p><code>File: .htaccess</code></p>
<pre>Options +FollowSymLinks
RewriteEngine on
RewriteRule ^home$ index.php
RewriteRule ^about$ about.php</pre>
<p>And you just keep adding <code>RewriteRule</code>'s for each page. This is really only helpful when you just have a set of static looking pages for a site.</p>
<p><strong>Explanation:</strong></p>
<dl>
<dt>Options +FollowSymLinks</dt>
<dd>Tells Apache to follow sym links (usually on anyways...)</dd>
<dt>RewriteEngine on</dt>
<dd>Tells apache to turn on the RewriteEngine for this folder</dd>
<dt>RewriteCond %{REQUEST_FILENAME} !-f</dt>
<dd>Read this as: Condtion, If the file we're requesting is not a file...</dd>
<dt>RewriteCond %{REQUEST_FILENAME} !-d</dt>
<dd>Read this as: Condtion, If the file we're requesting is not a directory...</dd>
<dt>RewriteRule . /index.php [L]</dt>
<dd>Then Rewrite this url to index.php ([L] forces it to be the last redirect)</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2009/01/modrewrite-and-short-urls-htaccess-file-tip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Professional PHP</title>
		<link>http://www.iwilldomybest.com/2009/01/professional-php/</link>
		<comments>http://www.iwilldomybest.com/2009/01/professional-php/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 16:05:18 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/?p=61</guid>
		<description><![CDATA[It's important to remember what sets a professional PHP developer apart from the pack that floods your average help channel. Too often I run into reviewing or helping a "professional" developer, and I can't help but mutter "you're doing it wrong..." I feel the most important attribute that separates one developer from another is when [...]]]></description>
			<content:encoded><![CDATA[<p>It's important to remember what sets a professional PHP developer apart from the pack that floods your average help channel.</p>
<p>Too often I run into reviewing or helping a "professional" developer, and I can't help but mutter "you're doing it wrong..."</p>
<p><span id="more-61"></span></p>
<p>I feel the most important attribute that separates one developer from another is when they write for usability and maintainability.</p>
<p>So let's take a look at some extremely simple steps you can take to make your project's code more flexible.</p>
<p>1. For the love of all that there is, seperate your header and footer files. Yes, I'm still seeing this happen, often. All content that is above your generated page needs to go into a header file, and all the content below it into a footer file. So when you need to modify one line, you do it only once.</p>
<pre>&lt;?php
require_once(dirname(__FILE__).'/includes/header.php');
?&gt;
&lt;div id='content'&gt;
&lt;?php
	// script runs
?&gt;
&lt;/div&gt;
&lt;?php
require_once(dirname(__FILE__).'/includes/footer.php');
?&gt;</pre>
<p>2. Create a function to generate your navigation. And when I say generate, I mean you give it some flexibility as well. This can be done even with the smallest of sites. Example:</p>
<pre>&lt;?php
$site_pages = array();
$site_pages[] = array('text' =&gt; 'home', 'link' =&gt; 'home.php');
$site_pages[] = array('text' =&gt; 'about', 'link' =&gt; 'about.php');
$site_pages[] = array('text' =&gt; 'partner', 'link' =&gt; 'http://www.example.domain.com/');

function buildNav($links)
{
	$output = '';
	foreach($links as $page)
	{
		$output .= "&lt;a href=\"{$page['link']}\" title=\"{$page['text']}\"&gt;{$page['text']}&lt;/a&gt;\n";
	}
 	return $output;
}
?&gt;</pre>
<p>The example above could easily be modified to allow optional values to the array, like <code>'enabled' =&gt; false</code> to turn off a link. Or you could define a link "template", and use <code>str_replace()</code> to substitute values.</p>
<p>This means if you, or the customer (with any know how) can quickly update the links at any time. For more advanced sites/projects this can obviously become more complicated, but it saves even more time.</p>
<p>3. If you're not going to use OOP, at least use functions. Any common task or specific sequence should be placed in it's own function. An example would be our navigation function from #2. Another example would be running a query for a specific item. If you ever need to modify your table structure you'll regret having to hunt down all of your inline queries.</p>
<p>4. When you create 'admin' features, take the 5 minutes to create a function that actually checks if the person accessing that "super hidden" directory filled with scripts that just accept anything.</p>
<p>I've seen this happen even on large global community sites.</p>
<p>Some developers will stuff their admin scripts in a folder such as /admin/, and just assume that if data is posted there then it's ok.</p>
<p>The least you should do is place a session check, and place it in a function.</p>
<pre>&lt;?php
function isAdmin()
{
	if(!isset($_SESSION['user_name']) || !isset($_SESSION['user_id']))
	{
		return false;
	}
	else
	{
		$sql = "SELECT COUNT(*) as cnt FROM user_table WHERE user_id = ".intval($_SESSION['user_id']);
		// do the query, check it, etc.
		return true;
	}
}
?&gt;</pre>
<p>Then when it comes time to run your script, simply:</p>
<pre>&lt;?php
if(isAdmin() === false)
{
	die('Log back in');
}
?&gt;</pre>
<p>5. Create a wrapper for handling escape strings. If you're not using a database class (like PDO) to handle this, you'll need to do this.</p>
<p>Just assume that your client could have their PHP configuration change, or they change hosts, or any possible combination of changes take place.</p>
<p>Create a simple wrapper that will check of magic quotes, and escape for your database insert.</p>
<pre>&lt;?php
function cleanString($input)
{
	if(get_magic_quotes_gpc())
	{
		$input = stripslashes($input);
	}
	$output = mysql_real_escape_string(trim($input));

	return $output;
}
?&gt;</pre>
<p>6. Create a configuration file so you can update common yet critical variables. An example of this would be paths. Your system quickly gains adaptability if you just create a defines.php file, and place in there your simple variables.</p>
<p>The overall approach you should have to any project you take is that you should use an establish tool chain you have developed. It doesn't take much time or effort to create a simple set of classes that can handle just about any thing you need to create. If you wish to use functions, you can do the same. The idea is that you write once, and write it in such a manner that you can tweak it later on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2009/01/professional-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Screen Scraping made too easy</title>
		<link>http://www.iwilldomybest.com/2008/12/screen-scraping-made-too-easy/</link>
		<comments>http://www.iwilldomybest.com/2008/12/screen-scraping-made-too-easy/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 08:40:27 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[screen scrape]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/?p=63</guid>
		<description><![CDATA[Recently I had some free time and I decided I wanted to automate some common tasks of mine. And let me tell you honestly, I hate having to do screen scraping. It's an annoying, tedious task. Making regex for this, and for that, and then to find out my hours were wasted as that regex [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had some free time and I decided I wanted to <a title="WikiPedia - Automation" href="http://en.wikipedia.org/wiki/Automation" target="_blank">automate</a> some common tasks of mine. And let me tell you honestly, I hate having to do screen scraping. It's an annoying, tedious task. Making regex for this, and for that, and then to find out my hours were wasted as that regex won't work on another site.</p>
<p>That's a thing of the past.</p>
<p><span id="more-63"></span></p>
<p>I'm super excited about this find. Maybe I'm the last to discover it, but it's just too awesome to pass up.</p>
<p>The project is called: <a title="PHP Simple HTML DOM Parser" href="http://simplehtmldom.sourceforge.net/" target="_blank">PHP Simple HTML DOM Parser</a>.</p>
<p>Literally, this takes almost all of the magic out of screen scraping. Here's an example from a quick and dirty login and grab my stats for <a title="e-Sports Entertainment" href="http://esportsea.com" target="_blank">ESEA</a>.</p>
<pre>require_once('simple_html_dom.php'); // get our class

define('COOKIE_JAR','./cookie/cookie'); // cookie jar for cURL
/**
* We need to set up our referrer and user agent, for cURL
**/
$referrer = "http://www.esportsea.com/";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4";
/**
* str = our direct link to our user page
**/
$str = "http://www.esportsea.com/users/&lt;your user id&gt;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$str);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referrer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_JAR);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_JAR);
curl_setopt($ch, CURLOPT_TIMEOUT, '10');
$result = curl_exec ($ch);
curl_close ($ch);
// done with cURL
$html = new simple_html_dom(); // create our HTML object

$html-&gt;load($result);

$pug_stats = $html-&gt;find('#body-matches-pug table tr'); // load up pug stats

// this loops through each &lt;tr&gt;
$output = array();
foreach($pug_stats as $stat)
{
	$row['game'] 	= trim($stat-&gt;find('img',0)-&gt;title);
	$row['link'] 	= trim($stat-&gt;find('a',0)-&gt;href);
	$row['score']	= trim($stat-&gt;find('a',0)-&gt;plaintext);
	$row['srv'] 	= trim($stat-&gt;find('a',1)-&gt;href);
	$row['srv_txt'] = trim($stat-&gt;find('a',1)-&gt;plaintext);
	$output[] = $row;

}
echo "&lt;pre&gt;",print_r($output,true),"&lt;/pre&gt;";
exit;

?&gt;</pre>
<p>And that's it.</p>
<p>Now take a look at that, and realize how much stuff I'm not forced to do.</p>
<p>I know this isn't some great new invention, loading the source into a DOM object and parsing it, but man, this almost eliminates the need to think about screen scraping entirely.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/12/screen-scraping-made-too-easy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Reference Card &#8211; Failed?</title>
		<link>http://www.iwilldomybest.com/2008/10/php-reference-card-failed/</link>
		<comments>http://www.iwilldomybest.com/2008/10/php-reference-card-failed/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 17:48:49 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ref card]]></category>
		<category><![CDATA[reference card]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/?p=53</guid>
		<description><![CDATA[While taking a look at my 'Tech Blogs' tab in NetVibes I saw the headline for a reference card available for PHP. So, I hopped on over to dZone.com, got an account (it's free), and downloaded it. I'm not really impressed. I believe the beginner may benefit from this, but there's just so many gaping [...]]]></description>
			<content:encoded><![CDATA[<p>While taking a look at my 'Tech Blogs' tab in <a title="NetVibes" href="http://www.netvibes.com/" target="_blank">NetVibes</a> I saw the headline for a <a title="http://refcardz.dzone.com/refcardz/php?oid=ban00022-15" href="http://refcardz.dzone.com/refcardz/php?oid=ban00022-15" target="_blank">reference card available</a> for PHP. So, I hopped on over to dZone.com, got an account (it's free), and downloaded it.</p>
<p>I'm not really impressed.</p>
<p>I believe the beginner may benefit from this, but there's just so many gaping holes in the reference card... Like what about the GD library? Or perhaps cURL? File handling? XML?</p>
<p>Honestly, how could you call a reference card a PHP Reference Card without the most common uses listed? I think the card may be off to a good start, but please, release a new more complete version soon.</p>
<p>Final Verdict:</p>
<p>While it may be great for beginners, it's still lacking for the rest of us. I'll pass and keep php.net as my reference card.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/10/php-reference-card-failed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &amp; MySQL Tip #3 &#8211; EAV Modeling w/ PHP &amp; MySQL</title>
		<link>http://www.iwilldomybest.com/2008/08/php-mysql-tip-3/</link>
		<comments>http://www.iwilldomybest.com/2008/08/php-mysql-tip-3/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 22:52:05 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[eav]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/2008/08/25/php-mysql-tip-3/</guid>
		<description><![CDATA[PHP &#38; MySQL Tip #3 I want to share with some of you an easy way to set up your database design in a very flexible and reliable EAV like model. EAV stands for Entity Attribute Value, which is a common design for complex database structures that require many different Entities using many different attributes [...]]]></description>
			<content:encoded><![CDATA[<p>PHP &amp; MySQL Tip #3</p>
<p>I want to share with some of you an easy way to set up your database design in a very flexible and reliable EAV like model.</p>
<p><span id="more-20"></span></p>
<p><a title="http://en.wikipedia.org/wiki/Entity-attribute-value_model" href="http://en.wikipedia.org/wiki/Entity-attribute-value_model" target="_blank">EAV</a> stands for Entity Attribute Value, which is a common design for complex database structures that require many different Entities using many different attributes with, again, many different values. It's very common to find this set up in medical offices or records.</p>
<p>Let's go ahead and assume a situation, and we'll work through it.</p>
<p>Let's say you're accepting a feed of some sort, and you need to save the items being fed to you into a database. The problem here, is that the content length of these items, attributes, and values varies.</p>
<p>So let's consider the following:</p>
<pre>Item 'Node1' -&gt;
	Attribute 'Main' -&gt;
		Value 1
		Value 2
		Value 3
		Value 4
Item 'Node2' -&gt;
	Attribute 'Main' -&gt;
		Value 1
		Value 2
		Value 3
		Value 4
	Attribute 'Other' -&gt;
		Value 1
		Value 2

Item 'Node3' -&gt;
	Attribute 'Main' -&gt;
		Value 1
		Value 2
		Value 3
		Value 4
		Value 5
		Value 6
		Value 7
		Value 8</pre>
<p>Now this is a very cheap and generic example of a data structure. But we'll work with it for now.</p>
<p>So let's go ahead and draw some conclusions.</p>
<p>The Item list will come in with a name, and have Attributes. These attributes will have values. But the number of attributes and the number of values varies.</p>
<p>So with that in mind, we shall come up with some SQL to create some times. For this example we're going to need three tables: items, item_attributes, and attribute_values.</p>
<p>So here we go:</p>
<p><code><br />
-- first our items table:<br />
CREATE TABLE `items` (<br />
`id` int(11) NOT NULL auto_increment,<br />
`item_name` varchar(50) default NULL,<br />
PRIMARY KEY  (`id`)<br />
);<br />
-- now our item attributes<br />
CREATE TABLE  `item_attributes` (<br />
`id` int(11) NOT NULL auto_increment,<br />
`item_id` int(11) NOT NULL default '0',<br />
`attribute_name` varchar(50) default NULL,<br />
PRIMARY KEY  (`id`),<br />
KEY `item_id_attribute_name` (`item_id`,`attribute_name`)<br />
);<br />
-- now finally our attribute values<br />
CREATE TABLE  `attribute_values` (<br />
`attribute_id` int(11) NOT NULL default '0',<br />
`attribute_value` varchar(100) default NULL,<br />
UNIQUE KEY `attribute_id` (`attribute_id`,`attribute_value`)<br />
);<br />
</code></p>
<p>Now that we have our layout, let's take a look at how this is going to work:</p>
<p>Let's assume the following PHP array is a reprentation of our data...</p>
<pre>// start up our array
$data = array();
//
// Now, let's just load it with some test data
$data['item_1'] = array();
$data['item_1']['attribute_1'] = array();
$data['item_1']['attribute_1'][] = 'value1';
$data['item_1']['attribute_1'][] = 'value2';
$data['item_1']['attribute_1'][] = 'value3';
$data['item_1']['attribute_1'][] = 'value4';
// that's good for now.
/**
* Now let's insert this int our new schema
*
* Please note, for example sake, I will not be double checking queries
* but you SHOULD check each query for an error.
**/
foreach($data as $item_name =&gt; $attributes)
{
	$sql = "INSERT INTO items (id, item_name) VALUES (NULL, '{$item_name}');";
	mysql_query($sql);
	$item_id = mysql_insert_id();
	// now let's loop through our attributes
	foreach($attributes as $attribute =&gt; $values)
	{
		// this is now our insert into the attributes...
		$sql = "INSERT INTO item_attributes (id, item_id, attribute_name) VALUES (NULL, {$item_id}, '{$attribute}');";
		mysql_query($sql);
		$attribute_id = mysql_insert_id();
		// now let's loop through the attribute values
		foreach($values as $value)
		{
			$sql = "INSERT INTO attribute_values (attribute_id, attribute_value) VALUES ({$attribute_id}, '{$value}');";
			mysql_query($sql);
		}
	}
}
?&gt;</pre>
<p>And there you have it - that's now how we can get our data into our database in a flexiable manner, without having to rely<br />
on an 'excel' like database.</p>
<p>To get it out, we'll simply use some joins...</p>
<p><code><br />
$sql =<br />
"SELECT<br />
items.item_name,<br />
ia.attribute_name,<br />
av.attribute_value<br />
FROM<br />
attribute_values AS av<br />
JOIN item_attributes AS ia<br />
ON (ia.id = av.attribute_id)<br />
JOIN items AS items<br />
ON (items.id = ia.item_id);<br />
";<br />
</code></p>
<p>You could also use a concat_ws to make a comma seperated list, but now that you have it in your database, you can do anything you want with it <img src='http://www.iwilldomybest.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/08/php-mysql-tip-3/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP &amp; MySQL Tip #2 &#8211; Better Way of Pagination</title>
		<link>http://www.iwilldomybest.com/2008/03/php-mysql-tip-2-better-way-of-pagination/</link>
		<comments>http://www.iwilldomybest.com/2008/03/php-mysql-tip-2-better-way-of-pagination/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 04:30:27 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/2008/03/15/php-mysql-tip-better-way-of-pagination/</guid>
		<description><![CDATA[I've seen this come up a few times in #php on irc.gamesurge.net - how to find the total number of rows found, while using the LIMIT clause on a query. I've seen people respond with some seriously incorrect solutions, such as 'Simply load all of the results into an array and only show X amount' [...]]]></description>
			<content:encoded><![CDATA[<p>I've seen this come up a few times in #php on irc.gamesurge.net - how to find the total number of rows found, while using the LIMIT clause on a query.</p>
<p>I've seen people respond with some seriously incorrect solutions, such as 'Simply load all of the results into an array and only show X amount' or 'Simple run another query without the LIMIT clause and count those rows.'</p>
<p>Those two, common, solutions are sadly not the best solution.</p>
<p>Read on to discover my solution.</p>
<p><span id="more-16"></span></p>
<p>Take for example the following query:</p>
<p><code>$sql = "SELECT id as id, name as name, full_title as title FROM articles ORDER BY id LIMIT 0,10";</code></p>
<p>Now, that looks all fine and dandy, you have probably used something very similar to that query many times. Now, say we want to actually show how many articles we have in our database?</p>
<p>We would simply change that query to:</p>
<p><code><br />
$sql = "SELECT SQL_CALC_FOUND_ROWS id as id, name as name, full_title as title FROM articles ORDER BY id LIMIT 0,10";<br />
$get = mysql_query($sql) or die(mysql_error());<br />
$sql_2 = "SELECT FOUND_ROWS();";<br />
$found_rows = mysql_query($sql_2) or die(mysql_error());<br />
</code></p>
<p>Now yes, we're running two queries, but we're not actually queries against any rows. So therefore, this solution is more elegant and ultimately more important, it's not attempting to scan any tables for data.</p>
<p>So next time you're looking to do a quick and pagination script, go ahead and try this technique out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/03/php-mysql-tip-2-better-way-of-pagination/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem Solving &#8211; guide to PHP programming and debugging.</title>
		<link>http://www.iwilldomybest.com/2008/03/problem-solving-guide-to-php-programming-and-debugging/</link>
		<comments>http://www.iwilldomybest.com/2008/03/problem-solving-guide-to-php-programming-and-debugging/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 07:15:21 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/2008/03/02/problem-solving-guide-to-php-programming-and-debugging/</guid>
		<description><![CDATA[Problem solving forms part of thinking. Interesting... so, if so many web devs and IT personnel have such a problem with problem solving, perhaps they're just not thinking? Let's explore some of the problems I've seen people encounter, and how I was baffled they lacked the problem solving skills to complete their tasks. All too [...]]]></description>
			<content:encoded><![CDATA[<p>Problem solving forms part of thinking.</p>
<p>Interesting... so, if so many web devs and IT personnel have such a problem with problem solving, perhaps they're just not thinking? Let's explore some of the problems I've seen people encounter, and how I was baffled they lacked the problem solving skills to complete their tasks.</p>
<p><span id="more-15"></span></p>
<p>All too often, a situation will appear in just one or two flavors. One being, "Hey, this should be working but it's not..." or, "Hey, I'm stuck." Sometimes you get the nice and juicy combo. But, that being said, let's explore how I most commonly find myself approaching these two generic scenarios.</p>
<p><strong>Number one, "Hey, this should be working but it's not..."</strong></p>
<p>Whenever you get to this point, that usually means a couple things:</p>
<ol>
<li>I have a pretty good idea on what <em>should </em>be happening</li>
<li>I know how it should be done</li>
<li>I've already solved this problem before, but this time it doesn't seem to be working</li>
</ol>
<p>Number 3 is always the worst, because as a programmer, you always think back to the last time you faced a similar problem, and basically copy the way you fixed it before. If that is the case, examine what's different this time... versions, updates, different variables, etc etc, these all have an impact.</p>
<p>And what it really comes down to, is back tracking all of your steps.</p>
<ul>
<li>Did I upload the latest version of the PHP script?
<ul>
<li>And if so, are the changes reflected on the server side?</li>
</ul>
</li>
<li>Has someone else made changes to the files in use?</li>
<li>Have a double checked the process against my flow charts?</li>
<li>Have I tried basic debugging?
<ul>
<li>This means, rather briefly:
<ul>
<li>Echo out the variables BEFORE they're passed or referenced</li>
<li>AND AFTER they are passed or referenced.</li>
<li>Have I done a var_dump()</li>
<li>Have I turned on error_reporting(E_ALL);</li>
<li>Have I checked my error_log</li>
<li>Have I checked my mysql_error();</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>If you go through a simple list like that in your mind, you can quickly determine where you're missing a step.</p>
<p>And please, for the love of all that is holy, please, don't be afraid to TRY something even if you're not sure it'll work. You should take the basic steps to plot out your thoughts before you begin to code. For me, that usually means getting out some paper and a pen, and sketch out my idea with some notes.</p>
<p>Then I begin to code.</p>
<p>If I ever get lost in this new idea of mine, I refer to my original plan. If I realize then that my original plan was FUBAR, I simply make the adjustments to my sketch, and keep on going.</p>
<p>I feel that most web developers somehow feel that they can just jump straight into a project without any planning. It simply doesn't happen. All "great" projects, they start with a little planning. Sure, maybe they didn't have a road map, version control, diagrams, database designs, ticketing systems, et cetera, but they at least had an idea. And I guarantee that idea was  <em>at least </em>written down in pseudo code.</p>
<p>Again, the idea here is that you HAVE a plan, and you know how things should work, so stick to your plan. You run into the problem of "It should work but it's not" when you've altered a step somewhere. Check your debugs, check your error logs, check your plan. Rinse repeat until you go mad. And don't be afraid to ask for some fresh eyes on the problem.</p>
<p><strong>Number Two, "I'm stuck."</strong></p>
<p>This comes down to just general programming knowledge. Or rather, how well you understand programming logic. I will often tell people, any one can be a programmer; in fact, everyone is a programmer. Programming is just solving a problem with at last one solution. You do this by breaking it down.</p>
<p><span style="text-decoration: underline;">Identify the problem</span> - <em>What do I need to accomplish?</em><br />
<span style="text-decoration: underline;">Determine the order</span> - <em>What needs to happen to solve this problem?</em><br />
<span style="text-decoration: underline;">How do I apply my solution</span> - <em>What's my plan of action?</em></p>
<p>Let's take for example, the problem of creating an E-Mail list manager.</p>
<p><span style="text-decoration: underline;">Identifying the problem</span> - I know what the solution is -- an email list manager. That's my solution, now what's the problem?Ã‚Â  Ok, this e-mail list manager is going to have a set of emails that it sends updates to everyone month. So... the problem is I need to find out how and where the e-mail addresses are stored, and I need to create a way for them to automatically be sent.</p>
<p><span style="text-decoration: underline;">Determine the order</span> - Well, I kind of already figured that out. I need to:</p>
<ol>
<li>Get the E-Mail Address
<ol>
<li>Make sure it's valid</li>
<li>Make sure they are opted in</li>
</ol>
</li>
<li>Add that Validated E-Mail address to the 'Send To' array</li>
<li>Create the body of my E-Mail
<ol>
<li>One for Plain Text</li>
<li>One for HTML</li>
</ol>
</li>
<li>Apply any templating system that may be needed</li>
<li>Do a foreach() on the 'Send To' array
<ol>
<li>Then send the emails</li>
</ol>
</li>
</ol>
<p><span style="text-decoration: underline;">How do I apply my solution</span> - Well, I'm going to need a CRON script that will run once a month. I will need to create a debugging / status message system so that I can record how many e-mails got sent out, and on what date, and to whom. It's pretty straight forward from there. Create the PHP script, be sure to apply the CLI items, and boom. Start.</p>
<p>What I'm trying to get across here, is that you need to approach things not as strictly code, but as a single thought. This is what I want, this is how I do it.</p>
<p>When you're running a program, you're not just running code, you're running someone's thoughts.Ã‚Â  That's the beauty in programming, it's not just about the code, it's about someone else getting to see into your mind.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/03/problem-solving-guide-to-php-programming-and-debugging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP and MySQL Tip #1 &#8211; Error Checks</title>
		<link>http://www.iwilldomybest.com/2007/04/php-and-mysql-tip-1-error-checks/</link>
		<comments>http://www.iwilldomybest.com/2007/04/php-and-mysql-tip-1-error-checks/#comments</comments>
		<pubDate>Tue, 01 May 2007 01:38:26 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.e-dirtylaundry.com/2007/09/28/php-and-mysql-tip-1-error-checks/</guid>
		<description><![CDATA[I'd like to share with all of you some PHP Tips. Use error checking when making an SQL query. All too often I see on IRC &#38; Forums people complaining/asking for help, regarding their mysql_query(). "It was working fine yesterday, now it''s broken." "Can someone tell me why this doesn''t work?" "Why doesn''t this work? [...]]]></description>
			<content:encoded><![CDATA[<p>I'd like to share with all of you some PHP Tips.<br />
<span id="more-3"></span><br />
Use error checking when making an SQL query.</p>
<p>All too often I see on IRC &amp; Forums people complaining/asking for help, regarding their mysql_query(). "It was working fine yesterday, now it''s broken." "Can someone tell me why this doesn''t work?" "Why doesn''t this work? It just doesn''t do anything."</p>
<p>People, you <strong>need</strong> to check for errors when you run a query. This is generally done like so:</p>
<p><code><br />
$get = mysql_query($sql) or die (mysql_error());<br />
</code></p>
<p>Now, that in itself is rather simple to understand. If there are any error's with your query ($sql), your script will die() and tell you the error.</p>
<p>An even better implementation of this would be:</p>
<p><code><br />
if(!$get = mysql_query($sql)) {<br />
die("Get query evaluated to false. SQL : {$sql} MySQL Error: ".mysql_error());<br />
}<br />
</code></p>
<p>Now, this is great for debugging purposes. It won't really fly on a live front end site. So you'd swap out the die() with:</p>
<p><code><br />
if(!$get = mysql_query($sql)) {<br />
error_log("Get query evaulted to false. SQL : {$sql} - MySQL Error: ".mysql_error());<br />
} else {<br />
/* Continue on with logic that relies upon this query */<br />
}<br />
</code></p>
<p>Now, what happens is if your query failed, you will get an error_log message, and you <span style="font-style: italic">do</span> check your error logs? Right? Of course! I thought so.<br />
Also, this ensures that only <span style="font-weight: bold">after</span> we know our query was successful do we continue on with our logic that relies upon it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2007/04/php-and-mysql-tip-1-error-checks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
