{"id":338,"date":"2012-02-07T15:27:54","date_gmt":"2012-02-07T22:27:54","guid":{"rendered":"http:\/\/danieltharp.com\/weblog\/?p=338"},"modified":"2012-02-07T15:27:54","modified_gmt":"2012-02-07T22:27:54","slug":"the-toolbelt-some-of-my-most-used-code-snippets-for-php-mysql-html-and-css","status":"publish","type":"post","link":"https:\/\/danieltharp.com\/weblog\/2012\/02\/the-toolbelt-some-of-my-most-used-code-snippets-for-php-mysql-html-and-css\/","title":{"rendered":"The Toolbelt: Some of my most-used code snippets for PHP, MySQL, HTML and CSS."},"content":{"rendered":"<p>I&#8217;ve meant to work on this for quite a while.  These are some of my most used code snippets to shorten a process, handy workarounds, pieces of code I need all the time or other such things.<\/p>\n<p><strong>HTML: The Meta Redirect<\/strong><\/p>\n<p>Use case:  You need to redirect someone to another page, and don&#8217;t want to bother notifying them.<\/p>\n<p>Code: [code]<meta http-equiv=\"refresh\" content=\"0;url=http:\/\/www.absolutepath.to\/page.php\">[\/code]<\/p>\n<p>Advantages:  Silent, works cross-platform, timer can be set (in whole seconds) by adjusting the 0 in content, will work in the body even though it&#8217;s a meta tag.<br \/>\nDisadvantages:  Breaks back buttons.<\/p>\n<p><strong>PHP\/MySQL: Quickly process and sanitize form data.<\/strong><\/p>\n<p>Use case:  You&#8217;ve just accepted a form and want to easily work with the data, and escape the data to prevent SQL injection attacks.<\/p>\n<p>Code: [code]foreach($_POST as $key=>$value) { $$key=addslashes($value); }[\/code]<\/p>\n<p>Advantages:  Saves a lot of repetitive entering of $_POST[&#8216;element&#8217;].  Instead you just use $element.  Also escapes the data early on so we don&#8217;t forget further along in the code.<br \/>\nDisadvantages:  You create a lot of variables instead of one array.  Your array isn&#8217;t actually destroyed, just copied.  The idea is that the backend is working primarily with this POST data so making a lot of variables isn&#8217;t an unwanted thing.<\/p>\n<p><strong>PHP\/MySQL:  Save an if statement on every mysql_error() check.<\/strong><\/p>\n<p>Use case:  You need to use mysql_error() function to handle errors in your SQL statement.<\/p>\n<p>Old code: [code]$result = mysql_query($query);<br \/>\nif(!$result) { die(mysql_error()); }[\/code]<\/p>\n<p>New code: [code]$result = mysql_query($query) or die(mysql_error());[\/code]<\/p>\n<p>Advantages: Reduces risk of typos breaking your page, cleaner.<br \/>\nDisadvantages:  No known disadvantages.<\/p>\n<p><strong>PHP\/MySQL: One standardized method of DB querying.<\/strong><\/p>\n<p>Use case:  You want to get in the habit of one naming scheme for your MySQL queries, and don&#8217;t want to go the OOP route.<\/p>\n<p>Code: [code]function doQuery($query) {<br \/>\n$result = mysql_query($query) or die(mysql_error());<br \/>\nreturn $result; }<\/p>\n<p>&#8230;<\/p>\n<p>$q_descriptivename = &#8220;SELECT * FROM table WHERE 1&#8221;;<br \/>\n$descriptivename = doQuery($q_descriptivename);[\/code]<\/p>\n<p>Note:  No advantage or disadvantage here, I just thought I&#8217;d share the way I do my queries.  I usually have a functions.php file with things like doQuery, doConnect, and so forth.  I then make the file a require_once() in my header.php. There are more efficient ways to do it via object-oriented code, but there are certainly less efficient ways too.<\/p>\n<p><strong>PHP:  Regex out non-numerals from a phone number, then put them back for display purposes.<\/strong><\/p>\n<p>Use case:  You have a form input for a phone number, and want to be guaranteed it works in your database.  If you&#8217;re trying to optimize and want to use a CHAR(10), or just want a unified dataset to work against.  Really it&#8217;s common sense to have all your data be uniformly stored here.<\/p>\n<p>[code]$phone=preg_replace(&#8220;\/[^0-9.]\/&#8221;, &#8220;&#8221;, $phone); \/\/ strip non-numerals<br \/>\n\t\tif (strlen($phone) == 7) { $phone = &#8220;505&#8221;.$phone; } \/\/ if the user was too lazy to add their area code, add the expected one.  Handle this how you like.<\/p>\n<p>&#8230;<\/p>\n<p>$disp_phone = preg_replace(&#8220;\/^(\\d{3})(\\d{3})(\\d{4})$\/&#8221;, &#8220;($1) $2-$3&#8221;, $phone); \/\/present it in the standard (123) 456-7890 format.[\/code]<\/p>\n<p>Advantages:  Regular expressions take a lot of possible entries and catch them all.  One-line solutions are always preferable to a switch or some such.<br \/>\nDisadvantages:  The code above is a very crude handling of too few numbers, but the regex can be tweaked to accommodate that or you can do it via Javascript.<\/p>\n<p><strong>CSS:  3-column div-based website with a &#8220;sticky footer&#8221;<\/strong><\/p>\n<p>Use case:  You find tables too outdated and confining, and would rather use divs and CSS.<\/p>\n<p>Code: <a href=\"http:\/\/www.danieltharp.com\/dev\/3colstickyfooter.css\">3colstickyfooter.css<\/a><\/p>\n<p>Note:  The &#8220;sticky footer&#8221;, where the footer is on the bottom of the page even when the rest of the content doesn&#8217;t reach that far, is a surprisingly tricky behavior to get right.  You can see the result <a href=\"http:\/\/www.danieltharp.com\/dev\/projecthavana\">here<\/a>.  Notice how the footer stays stuck to the bottom of the window as you resize it.<\/p>\n<p><strong>PHP\/CSS\/HTML: Blind-friendly, captcha-free, spam-fighting input field.<\/strong><\/p>\n<p>Use case: I had a client that was opposed to captchas, yet one of his forms was constantly hammered by spammers and filled his mailbox.  Thus I designed a hidden text input field that spambots fill out, blind users are audibly instructed to leave empty, and sighted users never see.<\/p>\n<p>Code: [code]HTML:<\/p>\n<tr class=\"robotic\">\n<td class=\"label\">If you&#8217;re human leave this blank.<\/td>\n<td class=\"field\"><input name=\"url\" type=\"text\" id=\"url\" \/><\/td>\n<td class=\"status\"><\/td>\n<\/tr>\n<p>&#8230;<\/p>\n<p>CSS: tr .robotic { display: none; }<\/p>\n<p>&#8230;<\/p>\n<p>PHP: if(strlen($_POST[&#8216;url&#8217;]) > 0) { die(); }[\/code]<\/p>\n<p>Advantages:  The home-brewed nature of this solution has resulted in a 100% decrease in spam getting through. We thwart the spammer by knowing they don&#8217;t respect CSS; they just look at a page as raw HTML and fill in data wherever possible.  Naming the field &#8220;url&#8221; guarantees the spammer is going to put a link in it, and then the PHP sees there is data in the field and silently rejects it.  Just use a different input name for your real URL field.  This also eliminates false positives and frustrating re-validation attempts by end-users that can&#8217;t read the captcha.<br \/>\nDisadvantages:  No known disadvantages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve meant to work on this for quite a while. These are some of my most used code snippets to shorten a process, handy workarounds, pieces of code I need all the time or other such things. HTML: The Meta Redirect Use case: You need to redirect someone to another page, and don&#8217;t want to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,16],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-nerd-stuff","category-work"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RwV4-5s","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":6,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":344,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/posts\/338\/revisions\/344"}],"wp:attachment":[{"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danieltharp.com\/weblog\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}