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: totaluplist
function totalUpList($toAddString,$mathTitle='',$sortBy='',$width=398){/*
Input a List of Prices, and it will add them up, and return a total.
SortBy= ('','desc','amount'
Start the line with a hyphen, an It will be presented as a Title. Reminder to Turn off Sorting if using them.
Do *NOT* put Spaces / Tabs before each line!
Format: Number*tab*Description
Format Example:
175Bananas
122 Apples
21Grapes
*/
$toAddString=str_replace(' ',"\t", $toAddString );
$toAdd=explode("\n",$toAddString);
$outputArray=array();
$errors='';
$toReturn='';
$total=0;
$dollarAdding=false;
if( substr_count($toAddString,'$') >=3 ){//count number of Dollar Signs
$dollarAdding=true;
}
foreach($toAdd as $eachLineString){
if($eachLineString==''){//
//blank. Ignore
}else if(strpos($eachLineString, "\t") !==false){//good
$eachLine=explode("\t",$eachLineString);
$eachLine[0]=str_replace('$','',$eachLine[0]);
if(substr($eachLine[0],0,1)=='*' ){//A Title
//Do Nothing
//$errors.='Debug: Title: "'.$eachLine[0].'"
'."\n";
}else if(is_numeric($eachLine[0]) ){
$total+=$eachLine[0];
//$errors.='Debug: Total is now: "'.$total.'"
'."\n";
}else{
$errors.='Error Non-Numeric Value "'.$eachLine[0].'"
'."\n";
}
$outputArray[]=array('value'=>$eachLine[0],'desc'=>$eachLine[1]);
}else{
$errors.='Invalid Line. Does Not Contain a Tab Character: "'.$eachLineString.'"
'."\n";
}
}
switch($sortBy){
case 'desc': usort($outputArray, "compareByDesc");break;
case 'amount': arsort($outputArray);break;
default: break;
}
//
setlocale(LC_MONETARY, 'en_US');
$toReturn=$errors.'
'.$mathTitle.' ( ';
if($total>=0){ $toReturn.=''.($dollarAdding?formatDollars($total):$total); }else{ $toReturn.='-'.($dollarAdding?formatDollars($total*-1):$total*-1).''; } $toReturn.=' ) | |
---|---|
'.$eachLine['desc'].' | |
'.($dollarAdding?'$ ':'').$eachLine['value'].' | '.'.$eachLine['desc'].' |
Total: | ';
if($total>=0){ $toReturn.=($dollarAdding?formatDollars($total):$total).' |
return $toReturn;
}
***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}