
<?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>jtGraphic.net &#187; if</title>
	<atom:link href="http://jtgraphic.net/tag/if/feed/" rel="self" type="application/rss+xml" />
	<link>http://jtgraphic.net</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 01:15:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Tidbit Tuesday on PHP: Simple MySQL Database Insert Function</title>
		<link>http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tidbit-tuesday-php-simple-mysql-database-insert-function</link>
		<comments>http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 19:03:49 +0000</pubDate>
		<dc:creator>jt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[How]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[return]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[Tidbit Tuesday]]></category>

		<guid isPermaLink="false">http://www.jtgraphic.net/?p=460</guid>
		<description><![CDATA[<p>Tweet This builds on a function I did last week: db_query(). You can send any array straight to a MySQL database and it&#8217;ll even check to make sure the column actually exists.  Oh, and it returns the id of the &#8230; <a href="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>Originally posted on jtGraphic.net: <a href="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/">Tidbit Tuesday on PHP: Simple MySQL Database Insert Function</a></p>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjtgraphic.net%2Ftidbit-tuesday-php-simple-mysql-database-insert-function%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/"  data-text="Tidbit Tuesday on PHP: Simple MySQL Database Insert Function" data-count="horizontal" data-via="jtgraphic">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>This builds on a function I did last week: <a href="http://www.jtgraphic.net/2009/11/tidbit-tuesday-php-simple-mysql-database-query-function/" target="_blank">db_query()</a>.  You can send any array straight to a MySQL database and it&#8217;ll even check to make sure the column actually exists.  Oh, and it returns the id of the record you just added in case you needed it.</p>
<pre>&lt;?php
   function db_array_insert($cfg_array, $table, $array) {
      require_once("dbQuery.function");

      $sql = "show columns from ".$table
      $tableArray = db_query($cfg_array, $sql);
      $inputString = "";

      foreach($tableArray as $key =&gt; $value) {
         if (array_key_exists($value[0], $array) &amp;&amp; $value[0])
            $inputString .= "'".addslashes($array[$value[0]])."', ";
         else
            $inputString .= "'', ";
      }

      $inputString = substr($inputString, 0, -2);
      $sql = "insert into $table values(".$inputString.")"
      db_query($cfg_array, $sql);

      return mysql_insert_id();
   }

   $insert_array = array(
      "column_1" =&gt; "something_1",
      "column_2" =&gt; "something_2"
   );

   $cfg_array = array(
      "db_loc" =&gt; 'www.databaselocation.com',
      "db_user" =&gt; 'some user'
      "db_pass" =&gt; 'some password'
      "db_name" =&gt; 'database_name_here'
   );

   db_array_insert($cfg_array, "some_table", $insert_array); //and use it.
?&gt;
</pre>
<p>So what do you think?  Having a problem?  Just let me know in the comments.</p>
<div class="bottomcontainerBox" style="">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjtgraphic.net%2Ftidbit-tuesday-php-simple-mysql-database-insert-function%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/"  data-text="Tidbit Tuesday on PHP: Simple MySQL Database Insert Function" data-count="horizontal" data-via="jtgraphic">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Originally posted on jtGraphic.net: <a href="http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/">Tidbit Tuesday on PHP: Simple MySQL Database Insert Function</a></p>]]></content:encoded>
			<wfw:commentRss>http://jtgraphic.net/tidbit-tuesday-php-simple-mysql-database-insert-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tidbit Tuesday on PHP: Holiday Notices for Business Sites</title>
		<link>http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tidbit-tuesday-php-holiday-notices-business-sites</link>
		<comments>http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 20:47:48 +0000</pubDate>
		<dc:creator>jt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[2009]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[Custom Functions]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[strtotime]]></category>
		<category><![CDATA[Tidbit Tuesday]]></category>

		<guid isPermaLink="false">http://www.jtgraphic.net/?p=343</guid>
		<description><![CDATA[<p>Tweet This is a handy script for managing an out of office message on your website.  I find this very useful on business websites when you&#8217;re tired of making that one little change to let people know when the office &#8230; <a href="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>Originally posted on jtGraphic.net: <a href="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/">Tidbit Tuesday on PHP: Holiday Notices for Business Sites</a></p>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjtgraphic.net%2Ftidbit-tuesday-php-holiday-notices-business-sites%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/"  data-text="Tidbit Tuesday on PHP: Holiday Notices for Business Sites" data-count="horizontal" data-via="jtgraphic">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>This is a handy script for managing an out of office message on your website.  I find this very useful on business websites when you&#8217;re tired of making that one little change to let people know when the office will be shut down.  With about 15 minutes of extra work, you only need to do it once a year.</p>
<p>Let&#8217;s say for instance we&#8217;re doing all of the major U.S. Holidays in 2010, observing weekend Holidays with a day off on either side of the weekend.  This means first we need to determine what those dates are.  I just happen to have looked up the six most common, listed below:</p>
<ul>
<li>New Year&#8217;s Eve &#8211; January 1st</li>
<li>Memorial Day &#8211; May 31st</li>
<li>Independence Day &#8211; July 5th (July 4th is Sunday)</li>
<li>Labor Day &#8211; September 6th</li>
<li>Thanksgiving &#8211; November 25th and 26th</li>
<li>Christmas &#8211; December 24th (December 25th is a Saturday)</li>
</ul>
<p>Now that we know our dates, we can build them into an array:</p>
<pre>$date_array = array(
   "New Yearís Eve" =&gt;
      array("leaving" =&gt; "2010-12-31", "returning" =&gt; "2011-01-03"),
   "Memorial Day" =&gt;
      array("leaving" =&gt; "2010-05-31", "returning" =&gt; "2010-06-01"),
   "Independence Day" =&gt; // July 4th is a Sunday
      array("leaving" =&gt; "2010-07-05", "returning" =&gt; "2010-07-06"),
   "Labor Day" =&gt;
      array("leaving" =&gt; "2010-09-06", "returning" =&gt; "2010-09-07"),
   "Thanksgiving" =&gt;
      array("leaving" =&gt; "2010-11-025", "returning" =&gt; "2010-11-029"),
   "Christmas" =&gt; // December 25th is a Saturday
      array("leaving" =&gt; "2010-12-24", "returning" =&gt; "2010-12-27"),
 );</pre>
<p>The date array can easily be stored in a database or be made up of more complicated functions that would calculate the holidays across multiple years with some basic logic.  Maybe we&#8217;ll cover the latter part in another post some day.</p>
<p>Now we need to build the function itself.  This function will be run every time the page loads.  You can even include the function in a separate file to be included on multiple pages:</p>
<pre>function holiday_message($date, $date_array) {
   $date = strtotime($date); // Reformat the date so we can do math on it.
   foreach($date_array as $key =&gt; $value) {
      $leaving = strtotime($value['leaving']);
      $returning = strtotime($value['returning']);
      $early_warning = $leaving - 86400 * 7;
      // If the date is between (7 days before) leaving and returning
         if($date &gt; $early_warning &amp;&amp; $date &lt; $returning) {
            echo
               "We will be observing ".$key." from ".date("Y-m-d", $leaving).
               " until ". date("Y-m-d", $returning).".
               When we return we will be more than happy to assist you.";
         }
   }
}</pre>
<p>Now we just need to execute this function anywhere on the page:</p>
<pre>holiday_message(date("Y-m-d H:i:s"),$date_array);</pre>
<p>This code can be customized in numerous different ways, so the the message is more formal, or works for your specific situation.  You can also change the way days are stored or calculated, like the example above.  This is a core to get you started.  Where can you go from here?</p>
<div class="bottomcontainerBox" style="">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjtgraphic.net%2Ftidbit-tuesday-php-holiday-notices-business-sites%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/"  data-text="Tidbit Tuesday on PHP: Holiday Notices for Business Sites" data-count="horizontal" data-via="jtgraphic">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Originally posted on jtGraphic.net: <a href="http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/">Tidbit Tuesday on PHP: Holiday Notices for Business Sites</a></p>]]></content:encoded>
			<wfw:commentRss>http://jtgraphic.net/tidbit-tuesday-php-holiday-notices-business-sites/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

