SQL Coding Standards
A Sample of My Coding Functions, for Quick Reference.Updated 1 year ago
Coding and Naming Standards:
On every project I've worked on, people have discussed coding & Naming Standards, but we always eventually realize that nobody has written these things down. So I have started these proposed standards:
- Names: like_this
- ID: Must be (INT) Named like: table_id
Coding Analogies
- Variable- Cooking ingredient
- Switch - Train switching station. or a choose your own adventure
- If - For in the road
- Break - Exit out of current Loop
- Exit (or Die)- Quit IMMEDIATELY, and print something to the screen
Regex GEdit:
Delete every other line.([^\n]*\n)[^\n]*\n\1
Regex tester for Search AND Replace:
RegExe.comMake all Links open in a New Window jquery.
<script>$("#content a[href^='http://']").attr("target","_blank");</script><script>$("#content a[href^='https://']").attr("target","_blank");</script>
SQL Queries
Count all Table Rows
SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = "adamwith_main";Hwo to assign an array to two variables
I always forget :Plist($first, $second)=array(1,2);
Geo IP Location:
You are in: Columbus, United StatesMy Commonly Used Functions:
View Function: quickparse
function quickParse($response,$title,$URI){$old_libxml_error = libxml_use_internal_errors(true);
$URI='https://www.imdb.com/title/tt0095016/';
$ch = curl_init();
if ($ch === false) {throw new Exception('failed to initialize'); }
//Required by Walmart.ca
/*
//CURLOPT_COOKIELIST
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie1.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie1.txt");
*/
curl_setopt($ch,CURLOPT_MAXREDIRS,40);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);//false
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
//curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch,CURLOPT_URL,$URI);
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$content = curl_exec($ch);
if ($content === false) { die( 'Error #234zd554s
CurlErrorNum:'.var_export(curl_errno($ch),true).'
'.var_export(curl_error($ch),true) ); }
if( (is_null($HTML) )||($HTML=='') ){
echo('Error #234sx5d: $HTML for '.$title.' is '.var_export($HTML,true).'
'/*.'
$URI='.$URI*/ );
return false;
}
$doc = new DOMDocument();
$doc->loadHTML($HTML);
libxml_use_internal_errors($old_libxml_error);
$tags = $doc->getElementsByTagName('meta');
if (!$tags || $tags->length === 0) {return false;}
echo 'Tags('.count($tags).'):
'."\n";
$pageValues=array();
$nonOgDescription = null;
foreach ($tags AS $tag) {
echo 'Tag Has Attributes: '.
($tag ->hasAttribute('value')?'Value "'.$tag->getAttribute('value').'"':' ').
($tag ->hasAttribute('property')?'Property "'.$tag->getAttribute('property').'"':' ').
($tag ->hasAttribute('name')?'Name "'.$tag->getAttribute('name').'"':' ').
'
';
if ($tag->hasAttribute('property') &&
strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
$pageValues[$key] = $tag->getAttribute('content');
echo 'Tag1:'.$tag->getAttribute('property').'-----'.$tag->getAttribute('content').'.
'."\n";
if($tag->getAttribute('property')==''){
echo '
Saving1
';$pageValues['image' ]=$tag->getAttribute('content');
$pageValues['image_src']=$tag->getAttribute('content');
}else if($tag->getAttribute('property')=='og:image'){
$content=str_replace('http://www.bestbuy.cah','h',$tag->getAttribute('content'));
echo '
Saving3: '.$content.'
';$pageValues['image' ]=$content;
$pageValues['image_src']=$content;
}else{
echo '
Unable to Process Property Tag: '.$tag->getAttribute('property').'
';}
}else{
echo 'No Tag Property: '.$tag->getAttribute('property').'---'.var_export($tag,true)."\n";
}
//Added this if loop to retrieve description values from sites like the New York Times who have malformed it.
if ($tag ->hasAttribute('value') && $tag->hasAttribute('property') &&
strpos($tag->getAttribute('property'), 'og:') === 0) {
//echo 'Tag2:'.$tag->getAttribute('property').'
'."\n";
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
$pageValues[$key] = $tag->getAttribute('value');
}
//Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') {
//echo 'Tag3:'.$tag->getAttribute('content').'
'."\n";
$nonOgDescription = $tag->getAttribute('content');
}
if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'twitter:image') {
echo '
Tag4:'.$title.'
'."\n";echo '
Saving2
';$pageValues['image' ]=$tag->getAttribute('content');
$pageValues['image_src']=$tag->getAttribute('content');
}
if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'og:image') {
echo '
Tag5:'.$tag->getAttribute('content').'
'."\n";echo '
Saving3
';$pageValues['image' ]=$tag->getAttribute('content');
$pageValues['image_src']=$tag->getAttribute('content');
}
if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'shareaholic:image') {
echo '
Tag6:'.$tag->getAttribute('content').'
'."\n";echo '
Saving3
';$pageValues['image' ]=$tag->getAttribute('content');
$pageValues['image_src']=$tag->getAttribute('content');
}
}
//die($HTML);
//Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
if (!isset($pageValues['title'])) {
$titles = $doc->getElementsByTagName('title');
if ($titles->length > 0) {
$pageValues['title'] = $titles->item(0)->textContent;
}
}
if (!isset($pageValues['description']) && $nonOgDescription) {
$pageValues['description'] = $nonOgDescription;
}
//Fallback to use image_src if ogp::image isn't set.
if (!isset($pageValues['image'])) {
$domxpath = new DOMXPath($doc);
$elements = $domxpath->query("//link[@rel='image_src']");
//echo '
Fallback'.$elements->length."
\n";if ($elements->length > 0) {
$domattr = $elements->item(0)->attributes->getNamedItem('href');
if ($domattr) {
echo 'Found Image Link A:'.$domattr->value.'
'."\n";
$pageValues['image'] = $domattr->value;
$pageValues['image_src'] = $domattr->value;
}
}else{
$elements = $domxpath->query("//img [@itemprop='image']");
if ($elements->length > 0) {
$domattr = $elements->item(0)->attributes->getNamedItem('src');
echo 'Found Image Link B:'.$domattr->value.'
'."\n";
if ($domattr) {
$pageValues['image'] = $domattr->value;
$pageValues['image_src'] = $domattr->value;
}
}else{
$elements = $domxpath->query("//img [@class='fileImage']");
if ($elements->length > 0) {
$domattr = $elements->item(0)->attributes->getNamedItem('src');
echo 'Found Image Link C:'.$domattr->value.'
'."\n";
if ($domattr) {
$pageValues['image'] = $domattr->value;
$pageValues['image_src'] = $domattr->value;
}
}else{
echo 'No Fallback-A. '.$elements->length.' Elements
'."\n";
}
}
}
}else{
echo 'No Fallback-B.
'."\n";
}
if (empty($pageValues)) { return false; }
return $page;
}
***Declared In:/home4/adamwith/public_html/includes/functions/adamwithers.ca.php
Add a hyphen to lists when Copying & Pasting
const prependChar = "-"; document.addEventListener("copy", function(e) { const selected = getSelectionHtml(); if (selected.html.includes("
- ${html}