<?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>Andy Weigel &#187; php</title>
	<atom:link href="http://andyweigel.com/blog/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://andyweigel.com/blog</link>
	<description>Web Development Tutorials</description>
	<lastBuildDate>Wed, 27 Oct 2010 00:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Export Parts of MySQL Table to Excel CSV File Using PHP</title>
		<link>http://andyweigel.com/blog/php-tutorials/export-parts-of-mysql-table-to-excel-csv-file-using-php/105</link>
		<comments>http://andyweigel.com/blog/php-tutorials/export-parts-of-mysql-table-to-excel-csv-file-using-php/105#comments</comments>
		<pubDate>Thu, 28 Jan 2010 20:45:48 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andyweigel.com/blog/?p=105</guid>
		<description><![CDATA[This is a quick snippet of code I created to dynamically pull a user-defined query from a MySQL database. &#60;?php include ('inc/connect.php'); //define the table that you want to pull data from $table = "table_name"; //define the variables for your query $query_variables = $_POST['id']; //build your dynamic query $query = "SELECT * FROM `".$table."` WHERE [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick snippet of code I created to dynamically pull a user-defined query from a MySQL database.<br />
<span id="more-105"></span></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpKeyword"><span class="phpKeyword">
include </span></span><span class="phpOperator">(</span><span class="phpString">'inc/connect.php'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpComment">//define the table that you want to pull data from
</span>$table <span class="phpOperator">=</span> <span class="phpString">"table_name"</span><span class="phpText">;</span>
<span class="phpComment">//define the variables<span class="phpKeyword"> for </span>your query
</span>$query_variables <span class="phpOperator">=</span> <span class="phpScriptVar">$_POST</span><span class="phpOperator">[</span><span class="phpString">'id'</span><span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpComment">//build your dynamic query
</span>$query <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM `"</span>.$table.<span class="phpString">"` WHERE "</span>.$query_variables<span class="phpOperator">.</span><span class="phpString">"";
$result = mysql_query("SHOW COLUMNS FROM ".$table.""</span><span class="phpOperator">)</span><span class="phpText">;</span>
$i <span class="phpOperator">=</span> 0;
<span class="phpKeyword">
if </span><span class="phpOperator">(</span><span class="phpFunction">mysql_num_rows</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span> <span class="phpOperator">&gt;</span> <span class="phpNumber">0</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
<span class="phpKeyword">
while </span><span class="phpOperator">(</span>$row <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_assoc</span><span class="phpOperator">(</span>$result<span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
$csv_output <span class="phpOperator">.=</span> $row<span class="phpOperator">[</span><span class="phpString">'Field'</span><span class="phpOperator">]</span>.<span class="phpString">", "</span><span class="phpText">;</span>
$i<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpOperator">}</span>
$csv_output <span class="phpOperator">.=</span> <span class="phpString">"\n"</span><span class="phpText">;</span>
$values <span class="phpOperator">=</span> <span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"$query"</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
while </span><span class="phpOperator">(</span>$rowr <span class="phpOperator">=</span> <span class="phpFunction">mysql_fetch_row</span><span class="phpOperator">(</span>$values<span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
<span class="phpKeyword">
for </span><span class="phpOperator">(</span>$j<span class="phpOperator">=</span>0;$j<span class="phpOperator">&lt;</span>$i<span class="phpText">;</span>$j<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
$csv_output <span class="phpOperator">.=</span> $rowr<span class="phpOperator">[</span>$j<span class="phpOperator">]</span>.<span class="phpString">", "</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
$csv_output <span class="phpOperator">.=</span> <span class="phpString">"\n"</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
$filename <span class="phpOperator">=</span> $file.<span class="phpString">"_"</span>.<span class="phpFunction">date</span><span class="phpOperator">(</span><span class="phpString">"Y-m-d_H-i"</span>,<span class="phpFunction">time</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">header</span><span class="phpOperator">(</span><span class="phpString">"Content-type<span class="phpOperator">:</span> application/vnd<span class="phpOperator">.</span>ms-excel"</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">header</span><span class="phpOperator">(</span><span class="phpString">"Content-disposition<span class="phpOperator">:</span> csv"</span> <span class="phpOperator">.</span> <span class="phpFunction">date</span><span class="phpOperator">(</span><span class="phpString">"Y-m-d"</span><span class="phpOperator">)</span> <span class="phpOperator">.</span> <span class="phpString">"<span class="phpOperator">.</span>csv"</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">header</span><span class="phpOperator">(</span> <span class="phpString">"Content-disposition<span class="phpOperator">:</span> filename="</span>.$filename.<span class="phpString">"<span class="phpOperator">.</span>csv"</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">print</span> $csv_output;
exit;
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andyweigel.com/blog/php-tutorials/export-parts-of-mysql-table-to-excel-csv-file-using-php/105/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Function to Generate a PHP Random String of Numbers and Letters</title>
		<link>http://andyweigel.com/blog/php-tutorials/function-to-generate-a-php-random-string-of-numbers-and-letters/73</link>
		<comments>http://andyweigel.com/blog/php-tutorials/function-to-generate-a-php-random-string-of-numbers-and-letters/73#comments</comments>
		<pubDate>Fri, 08 Jan 2010 14:41:03 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andyweigel.com/blog/?p=73</guid>
		<description><![CDATA[Here&#8217;s a quick snippet of code that allows you to quickly generate a random string of letters and/or numbers based on the variables you feed into the function. &#60;?php //Generate Random String function generateCode($numchars=5,$digits=1,$letters=1) { $dig = "012345678923456789"; $abc = "ABCDEFGHJKLMNOPQRSTUVWXYZ"; if($letters == 1) { $str .= $abc; } if($digits == 1) { $str .= [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick snippet of code that allows you to quickly generate a random string of letters and/or numbers based on the variables you feed into the function.<span id="more-73"></span></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpComment">//Generate Random String
</span><span class="phpFunctionKeyword">function</span><span class="htmlText"> generateCode</span><span class="phpOperator">(</span>$numchars=<span class="phpNumber">5</span>,$digits=<span class="phpNumber">1</span>,$letters=<span class="phpNumber">1</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
$dig <span class="phpOperator">=</span> <span class="phpString">"012345678923456789"</span><span class="phpText">;</span>
$abc <span class="phpOperator">=</span> <span class="phpString">"ABCDEFGHJKLMNOPQRSTUVWXYZ"</span><span class="phpText">;</span>
<span class="phpKeyword">
if<span class="phpOperator">(</span></span>$letters <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpNumber">1</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
$str <span class="phpOperator">.=</span> $abc<span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpKeyword">
if<span class="phpOperator">(</span></span>$digits <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpNumber">1</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
$str <span class="phpOperator">.=</span> $dig;
<span class="phpOperator">}</span>
<span class="phpKeyword">
for<span class="phpOperator">(</span></span>$i<span class="phpOperator">=</span>0; $i <span class="phpOperator">&lt;</span> $numchars<span class="phpText">;</span> $i<span class="phpOperator"><span class="phpOperator">+</span><span class="phpOperator">+</span></span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
$randomized <span class="phpOperator">.=</span> $str<span class="phpOperator">{</span><span class="phpFunction">rand</span><span class="phpOperator">(</span><span class="phpOperator">)</span> % <span class="phpFunction">strlen</span><span class="phpOperator">(</span>$str<span class="phpOperator">)</span><span class="phpOperator">}</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpKeyword">
return </span>$randomized<span class="phpText">;</span>
<span class="phpOperator">}</span>
$code <span class="phpOperator">=</span><span class="htmlText"> generateCode</span><span class="phpOperator">(</span><span class="phpString">'<span class="phpNumber">5</span>'</span>,<span class="phpString">'<span class="phpNumber">0</span>'</span>,<span class="phpString">'<span class="phpNumber">1</span>'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> $code<span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andyweigel.com/blog/php-tutorials/function-to-generate-a-php-random-string-of-numbers-and-letters/73/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convert a String into a Date with PHP</title>
		<link>http://andyweigel.com/blog/php-tutorials/convert-a-string-into-a-date-with-php/47</link>
		<comments>http://andyweigel.com/blog/php-tutorials/convert-a-string-into-a-date-with-php/47#comments</comments>
		<pubDate>Wed, 30 Sep 2009 19:40:44 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andyweigel.com/blog/?p=47</guid>
		<description><![CDATA[This problem came up while working with the Allegheny County Property database. They have a field for the sale date of the property, only they&#8217;re storing the date like this &#8211; 3291982 for 03/29/1982. The problem is that there is no leading zero for the month, so using substr function and counting 2, 2 and [...]]]></description>
			<content:encoded><![CDATA[<p>This problem came up while working with the Allegheny County Property database. They have a field for the sale date of the property, only they&#8217;re storing the date like this &#8211; 3291982 for 03/29/1982. </p>
<p><span id="more-47"></span></p>
<p>The problem is that there is no leading zero for the month, so using<a href="http://www.php.net/substr" target="_blank"> substr function</a> and counting 2, 2 and 4 won&#8217;t work in every situation. Here&#8217;s what I did to create a human readable date (03/29/1982) as well as a UNIX time variable.</p>
<h3>The Functions</h3>
<p>For this conversion I&#8217;m going to use three functions -<a href="http://www.php.net/substr" target="_blank"> substr</a>, str_replace and <a href="http://www.php.net/manual/en/function.strtotime.php" target="_blank">strtotime</a>. Here&#8217;s a quick breakdown of how they work.</p>
<p><strong>substr ( string $string     , int $start     [, int $length    ] )</strong> &#8211; this returns a portion of the string based on the <em>start</em> and <em>length</em> parameters. You can use negative numbers for the<em> start</em> to pull from the end of the string, which is what we&#8217;ll be doing.</p>
<p><strong>str_replace ( <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">mixed</a> $search     , <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">mixed</a> $replace     , <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">mixed</a> $subject)</strong> &#8211; this function will return a string or an array with all occurences of the <em>search</em> parameter in the <em>subject</em> parameter replaced with the <em>replace</em> parameter.</p>
<p><strong>strtotime ( string $date )</strong> &#8211; this function takes a US English date format and will parse it into a UNIX timestamp. </p>
<h3>Down to Business</h3>
<pre class="php">
<span class="phpComment">// Takes a <span class="phpFunction">date</span> <span class="phpOperator">(</span>11222009<span class="phpOperator">)</span> and returns 11/22/2009
</span><span class="phpFunctionKeyword">function</span> fix_<span class="phpFunction">date</span><span class="phpOperator">(</span>$sale_<span class="phpFunction">date</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
	$sale_date_year <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_<span class="phpFunction">date</span>, -<span class="phpNumber">4</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$sale_date_day <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_<span class="phpFunction">date</span>, -<span class="phpNumber">6</span>, <span class="phpNumber">2</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$string_to_replace <span class="phpOperator">=</span> $sale_date_day <span class="phpOperator">.</span> $sale_date_year<span class="phpText">;</span>
	$sale_date_month <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span>$string_to_replace,<span class="phpString">''</span>,$sale_<span class="phpFunction">date</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$sale_date_clean <span class="phpOperator">=</span> <span class="phpString">"$sale_date_month/$sale_date_day/$sale_date_year"</span><span class="phpText">;</span>
<span class="phpKeyword">	if </span><span class="phpOperator">(</span>$sale_date_clean <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpString">"<span class="phpComment">//"</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>$sale_date_clean <span class="phpOperator">=</span> NULL<span class="phpText">;</span><span class="phpOperator">}</span>
</span>
<span class="phpKeyword">	return </span>$sale_date_clean<span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p>I pulled the sale date from the database and created the $sale_date variable out of it. For this example lets suppose that $sale_date = 3291982.</p>
<h5>Let&#8217;s Breakout the Year</h5>
<p>Next, I figured that the last 4 digits will always be the year. I was able to create a variable from them using the substr function that&#8217;s provided by PHP. What this function is doing is telling it to take the $sale_date and take the last 4 numbers off the end (-4)for a total of 4 numbers (4).</p>
<pre class="php">
$sale_date_year <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_date, -<span class="phpNumber">4</span>, <span class="phpNumber">4</span><span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p><strong>So at this point $sale_date_year = 1982.</strong></p>
<h5>Getting the Day of the Month</h5>
<p>The next step was to strip out the day of the month using the same substr function &#8211; this time telling it to take 6 numbers from the end (-6) for a total of 2 numbers (2).</p>
<pre class="php">
 $sale_date_day <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_date, -<span class="phpNumber">6</span>, <span class="phpNumber">2</span><span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p><strong>This function will return $sale_date_day = 29. </strong></p>
<h5>Now for the Tricky Part&#8230; </h5>
<p>I&#8217;m going to combine the 2 new variables together to create a string to use as my replace variable to use the str_replace function.</p>
<pre class="php">
$string_to_replace <span class="phpOperator">=</span> $sale_date_day <span class="phpOperator">.</span> $sale_date_year<span class="phpText">;</span>
</pre>
<p>At this point $string_to_replace = 291982. I&#8217;m going to use this in the str_replace function to get the month from the $sale_date variable.Here&#8217;s the function -</p>
<pre class="php">
$sale_date_month <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span>$string_to_replace,<span class="phpString">''</span>,$sale_date<span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>This will return $sale_date_month = 3</p>
<h5>Creating a User-Friendly Date</h5>
<p>The next step is to create a human readable date. To this, I simply combine all the separate variables into one variable, separated with a slash.</p>
<pre class="php">
$sale_date_clean <span class="phpOperator">=</span> <span class="phpString">"$sale_date_month/$sale_date_day/$sale_date_year"</span><span class="phpText">;</span>
</pre>
<p>The $sale_date_clean will now look like this &#8211; 3/29/1982.</p>
<h5>Make the Pretty Date into a UNIX Timestamp</h5>
<p>The final step will be to take the $sale_date_clean that we just created and make it into a UNIX timestamp. We&#8217;ll use the strtotime function of this.</p>
<pre class="php">
$unix_date <span class="phpOperator">=</span> <span class="phpFunction">strtotime</span><span class="phpOperator">(</span>$sale_date_clean<span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>At this point we have 2 clean variables to do whatever you want with.</p>
<p>Hopefully you&#8217;ve found this quick tutorial valuable. <a href="contact.php">Contact me</a> if you have any questions or would do it a different way. There&#8217;s 1,001 ways to do everything &#8211; that&#8217;s why I love coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://andyweigel.com/blog/php-tutorials/convert-a-string-into-a-date-with-php/47/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Prices in PHP</title>
		<link>http://andyweigel.com/blog/php-tutorials/working-with-prices-in-php/67</link>
		<comments>http://andyweigel.com/blog/php-tutorials/working-with-prices-in-php/67#comments</comments>
		<pubDate>Fri, 07 Aug 2009 16:08:23 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andyweigel.com/blog/?p=67</guid>
		<description><![CDATA[Take an array of numbers, strip out the dollar sign and format them correctly. This is useful when you are given prices as varchar or text with the dollar sign already included in the field. We&#8217;re going to start with an array of numbers, in this case we&#8217;ll use the array(&#8216;$1256.45&#8242;,&#8217;$5564.75&#8242;,&#8217;$7895.33&#8242;). Here is the foreach [...]]]></description>
			<content:encoded><![CDATA[<p>Take an array of numbers, strip out the dollar sign and format them correctly.</p>
<p>This is useful when you are given prices as varchar or text with the dollar sign already included in the field. We&#8217;re going to start with an array of numbers, in this case we&#8217;ll use the array(&#8216;$1256.45&#8242;,&#8217;$5564.75&#8242;,&#8217;$7895.33&#8242;).</p>
<p>Here is the foreach loop running: </p>
<p>Start with $1256.45<br />
Strip the $ &#8211; 1256.45<br />
Cleaned and formatted number with the dollar sign added back in as a character &#8211; $1,256.45</p>
<p>Start with $5564.75<br />
Strip the $ &#8211; 5564.75<br />
Cleaned and formatted number with the dollar sign added back in as a character &#8211; $5,564.75</p>
<p>Start with $7895.33<br />
Strip the $ &#8211; 7895.33<br />
Cleaned and formatted number with the dollar sign added back in as a character &#8211; $7,895.33</p>
<p>Here&#8217;s the code that performs the task above:</p>
<pre class="php"> <span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span> 
$number_<span class="phpFunction">array</span> <span class="phpOperator">=</span> <span class="phpFunction">array</span><span class="phpOperator">(</span><span class="phpString">'$1256<span class="phpOperator">.</span>45'</span>,<span class="phpString">'$5564<span class="phpOperator">.</span>75'</span>,<span class="phpString">'$7895<span class="phpOperator">.</span>33'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
foreach </span><span class="phpOperator">(</span>$number_<span class="phpFunction">array</span><span class="phpKeyword"> as </span>$number<span class="phpOperator">)</span> <span class="phpOperator">{</span>
<span class="phpFunction">echo</span> <span class="phpString">"Start with $number<span class="phpOperator">&lt;</span>br/<span class="phpOperator">&gt;</span>"</span><span class="phpText">;</span>
$number <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span><span class="phpString">'$'</span>,<span class="phpString">''</span>,$number<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> <span class="phpString">"Strip the $ - $number<span class="phpOperator">&lt;</span>br/<span class="phpOperator">&gt;</span>"</span><span class="phpText">;</span>
$formatted_number <span class="phpOperator">=</span> <span class="phpFunction">number_format</span><span class="phpOperator">(</span>$number, <span class="phpNumber">2</span>, <span class="phpString">'<span class="phpOperator">.</span>'</span>,<span class="phpString">','</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> <span class="phpString">"Cleaned and formatted number with the dollar sign added back in - <span class="phpOperator">&lt;</span><span class="htmlText">strong</span><span class="phpOperator">&gt;</span>$$formatted_number<span class="phpOperator">&lt;</span>/strong<span class="phpOperator">&gt;</span><span class="phpOperator">&lt;</span>br/<span class="phpOperator">&gt;</span><span class="phpOperator">&lt;</span>br/<span class="phpOperator">&gt;</span>"</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<h3>Simple Percent off a Price Using PHP and Simple Math</h3>
<p>The next thing we&#8217;ll do is take our newly formatted numbers and perform a simple calculation to get a percentage off. </p>
<p>We will use the price 1234.67 as our starting price and 45% off as the discount rate.</p>
<p>Here is the foreach loop running: </p>
<p>starting price is $1234.67<br />
45% off of $1234.67 = $555.60<br />
$1234.67 &#8211; $555.60 will equal $679.07 &#8211; the final sale price </p>
<pre class="php"> <span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
$price <span class="phpOperator">=</span><span class="htmlText"> 1234</span><span class="phpOperator">.</span><span class="htmlText">67</span><span class="phpText">;</span>
$percent_off <span class="phpOperator">=</span> <span class="phpOperator">.</span><span class="htmlText">45</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> <span class="phpString">"starting price is $$price<span class="phpOperator">&lt;</span>br/<span class="phpOperator">&gt;</span>"</span><span class="phpText">;</span>
$money_off <span class="phpOperator">=</span> $price * $percent_off<span class="phpText">;</span>
$money_off <span class="phpOperator">=</span> <span class="phpFunction">number_format</span><span class="phpOperator">(</span>$money_off, <span class="phpNumber">2</span>, <span class="phpString">'<span class="phpOperator">.</span>'</span>,<span class="phpString">','</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> <span class="phpString">"45% off of $$price <span class="phpOperator">=</span> $$money_off<span class="phpOperator">&lt;</span>br/<span class="phpOperator">&gt;</span>"</span><span class="phpText">;</span>
$sale_price <span class="phpOperator">=</span> $price - $money_off<span class="phpText">;</span>
$sale_price <span class="phpOperator">=</span> <span class="phpFunction">number_format</span><span class="phpOperator">(</span>$sale_price, <span class="phpNumber">2</span>, <span class="phpString">'<span class="phpOperator">.</span>'</span>,<span class="phpString">','</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpFunction">echo</span> <span class="phpString">"$$price - $$money_off will equal $$sale_price - the<span class="phpKeyword"> final </span>sale price"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andyweigel.com/blog/php-tutorials/working-with-prices-in-php/67/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Style WordPress Titles Based on Number of Letters</title>
		<link>http://andyweigel.com/blog/wordpress-how-tos/style-wordpress-titles-based-on-number-of-letters/56</link>
		<comments>http://andyweigel.com/blog/wordpress-how-tos/style-wordpress-titles-based-on-number-of-letters/56#comments</comments>
		<pubDate>Wed, 08 Jul 2009 20:11:50 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Wordpress How-To's]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://andyweigel.com/blog/?p=56</guid>
		<description><![CDATA[A common problem that I&#8217;ve encountered &#8211; how to control dynamic text based on how long it is. In this case I had titles ranging from 10 characters to 25 characters &#8211; this includes spaces. The goal is to make them centered regardless of how many letters and spaces are in the title. Here is [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem that I&#8217;ve encountered &#8211; <strong>how to control dynamic text based on how long it is</strong>. In this case I had titles ranging from 10 characters to 25 characters &#8211; this includes spaces. The goal is to make them centered regardless of how many letters and spaces are in the title.</p>
<p><strong>Here is an example of one of the shorter titles I was given:</strong><br />
	    <img src="http://andyweigel.com/images/single-line-title.jpg" width="455" height="111" alt="single-line-title" /></p>
<p><strong>Here is an example of one of the longer titles I was given:</strong><br />
	    <img src="http://andyweigel.com/images/double-line-title.jpg" width="460" height="114" />		</p>
<p>A series of quick if/else statements and use of the strlen function in PHP allows me to assign different classes depending on the length of the dynamic title. I figured out, based on the CSS on the H1 class, that 14 was my magic number.</p>
<p><strong>Here&#8217;s the quick snippet of code I used:</strong></p>
<pre class="php">
$title <span class="phpOperator">=</span> $post<span class="phpOperator">-<span class="phpOperator">&gt;</span></span>post_title<span class="phpText">;</span>
$char_title <span class="phpOperator">=</span> <span class="phpFunction">strlen</span><span class="phpOperator">(</span>$title<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpKeyword">
if </span><span class="phpOperator">(</span>$char_title <span class="phpOperator">&gt;</span> 14<span class="phpOperator">)</span> <span class="phpOperator">{</span>$header_style <span class="phpOperator">=</span> <span class="phpString">"double"</span><span class="phpText">;</span><span class="phpOperator">}</span>
<span class="phpKeyword">
else </span><span class="phpOperator">{</span>$header_style <span class="phpOperator">=</span> <span class="phpString">"single"</span><span class="phpText">;</span><span class="phpOperator">}</span></pre>
<p>I grab the post title using common WordPress calls and make it into the $title variable. The I use the strlen function to determine the number of characters (including spaces) in the title. If it&#8217;s greater than 14, it assigns the $header_style variable the class &#8220;double&#8221; or else it assigns it the &#8220;single&#8221; class.</p>
<p><strong>Here is how I used it within the WordPress loop:</strong></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="phpKeyword"> if </span><span class="phpOperator">(</span><span class="htmlText">have_posts</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">:</span><span class="phpKeyword"> while </span><span class="phpOperator">(</span><span class="htmlText">have_posts</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">:</span><span class="htmlText"> the_post</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
<span class="htmlOtherTag">&lt;div class=<span class="htmlAttributeValue">&quot;<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span> <span class="phpFunction">echo</span> $header_style <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span></span>&quot;</span>&gt;<span class="htmlOtherTag">&lt;h2&gt;</span><span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="htmlText"> the_title</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span><span class="htmlOtherTag">&lt;/h2&gt;</span><span class="htmlOtherTag">&lt;/div&gt;</span>
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span><span class="phpKeyword"> endwhile<span class="phpText">;</span></span><span class="phpKeyword"> endif<span class="phpText">;</span></span> <span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Hopefully you&#8217;ve found this quick tutorial valuable. <a href="contact.php">Contact me</a> if you have any questions or would do it a different way. There&#8217;s 1,001 ways to do everything &#8211; that&#8217;s why I love coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://andyweigel.com/blog/wordpress-how-tos/style-wordpress-titles-based-on-number-of-letters/56/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

