Public snipts »
robertbanh's
snipts
showing 1-20 of 35 snipts
-
∞ config.php for EE
<?php if ( ! defined('EXT')){ exit('Invalid file request'); } $conf['app_version'] = "168"; $conf['license_number'] = ""; $conf['debug'] = "1"; $conf['install_lock'] = "1"; $conf['db_hostname'] = "[mysql server]"; // Note: Must use the super admin since EE will // alter and add/remove columns. $conf['db_username'] = "xxxx"; $conf['db_password'] = "xxxx"; $conf['db_name'] = "xxx"; $conf['db_type'] = "mysql"; $conf['db_prefix'] = "exp"; $conf['db_conntype'] = "0"; $conf['system_folder'] = "[system folder name]"; $conf['cp_url'] = "http://[server]/[system]/index.php"; $conf['doc_url'] = "http://expressionengine.com/docs/"; $conf['cookie_prefix'] = ""; $conf['is_system_on'] = "y"; $conf['allow_extensions'] = "y"; $conf['multiple_sites_enabled'] = "n"; // rbanh: this is for migration: $conf['site_url'] = "http://[server]/"; $conf['tmpl_file_basepath'] = $conf['site_url'] . $conf['system_folder'] . "/templates/"; $conf['theme_folder_url'] = $conf['site_url'] . "themes/"; $conf['theme_folder_path'] = $conf['site_url'] . "themes/"; $conf['captcha_path'] = $conf['site_url'] . "images/captchas/"; $conf['captcha_url'] = $conf['site_url'] . "images/captchas/"; $conf['avatar_path'] = $conf['site_url'] . "images/members/avatars/"; $conf['avatar_url'] = $conf['site_url'] . "images/members/avatars/"; ?>
-
∞ Sum of 2 columns with alias
# remove the group by if you want 1 total row returned. # select *, sum(s1.score+s2.score) total from search_score s1 inner join search_word w1 on s1.word_id = w1.id inner join search_score s2 on s1.sp_id = s2.sp_id and s1.id != s2.id inner join search_word w2 on s2.word_id = w2.id where w1.word = 'zinc' and w2.word = 'soft' group by s1.id order by total desc limit 50
-
∞ Rewrite .htaccess to EE's engine if directory conflicts with existing directory
RewriteEngine On RewriteBase / # if the file/dir does not exist, append index.php # which will call EE and pull up it's structured url RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/interact/$1 [L]
-
∞ Dates Samples
<?php $currDate = date('Y-m-d h:i:s'); // convert timestamp from mysql $nextReviewDt = date('F d, Y',strtotime($nextReviewDt)); // Add 30 days $CommentStartDt = date('m/d/Y'); $CommentEndDt = date('m/d/Y', mktime(0,0,0,date("m"),date("d")+30,date("Y"))); // add 30 days // convert year to 4 digits if ($y > 60) $y = '19'.$y; else $y = '20'.$y; // split if there are slash or dash list($m, $d, $y) = split('[/|-]', $iDate); if (preg_match("/-/", $iDate)) list($m, $d, $y) = split('-', $iDate); else if (preg_match("/\//", $iDate)) list($m, $d, $y) = split('/', $iDate); $specDate = mktime(0, 0, 0, $m, $d, $y); $numOfDays = (time() - $specDate) / (24 * 60 * 60); // convert date from mysql db if (preg_match("/(.*)Dt$/", $dbDate)) $dbDate = date("M d, Y h:ia", strtotime($dbDate)); // output dates - walk the days $startDt = '2009-01-04'; $endDt = date('Y-m-d'); while ($startDt != $endDt) { echo "$startDt <br/>"; // increment the day $startDt = strtotime(date("Y-m-d", strtotime($startDt)) . " +1 day"); // convert back to readable format $startDt = date("Y-m-d",$startDt); } function count_days( $a, $b ) { // First we need to break these dates into their constituent parts: $gd_a = getdate( $a ); $gd_b = getdate( $b ); // Now recreate these timestamps, based upon noon on each day // The specific time doesn't matter but it must be the same each day $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] ); $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] ); // Subtract these two numbers and divide by the number of seconds in a // day. Round the result since crossing over a daylight savings time // barrier will cause this time to be off by an hour or two. return round( abs( $a_new - $b_new ) / 86400 ); }
-
∞ Count Monthly/Yearly Totals in MySQL
SELECT MONTHNAME(createDt) AS month, YEAR(createDt) AS year, COUNT(*) AS total FROM tracking GROUP BY MONTH(createDt), YEAR(createDt) ORDER BY id
-
∞ Use found_rows() in MySQL
// use found_rows() as a sql function mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name -> WHERE id > 100 LIMIT 10; mysql> SELECT FOUND_ROWS();
-
∞ Pagination for PHP
<div id='pagination'> <span class='curr'>Page <?php echo $page; ?> of <?php echo $totalPages; ?></span> <?php $range = 3; // build previous page if ($page > 1) echo "<span><a href='{$_SERVER['PHP_SELF']}?page=".($page-1)."'>Previous</a></span> "; // build range pages $stop = ($page + $range) + 1; for ($x=($page-$range); $x<$stop; $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalPages)) { if ($x == $page) echo "<span class='curr'>$x</span> "; else echo "<span><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></span> "; } } // build last page if ($stop <= $totalPages) echo "... <span><a href='{$_SERVER['PHP_SELF']}?page=$totalPages'>$totalPages</a></span> "; // build next page if ($page != $totalPages) echo "<span><a href='{$_SERVER['PHP_SELF']}?page=".($page+1)."'>Next</a></span>"; ?> </div>
-
∞ Set up mail forwarding on linux
# add a .forward file to your home directory touch .forward # add your email address vi .forward <user>@<mail>.com # change permission else it won't work chmod 600 .forward # test mail send, replace 'user' with your login mailx -s 'test from cluster' user < /dev/null
-
∞ My Custom .htaccess file
RewriteEngine On #comment out the options followsymlinks when in prod #Options +ExecCGI +FollowSymLinks RewriteBase /rbanh/blah/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,QSA] # prevent directory browsing Options -Indexes <FilesMatch "\.(htaccess|svn|xml|inc|.*sql|cache_|.*~)$"> deny from all </FilesMatch>
-
∞ remove newline chars in vi
:%s/^v<Carriage Return>//g *hit the ctrl key + v then hit the carriage return.
-
∞ linux AT command
// linux at command > at 03:30 -v > nohup php -f /x/x/x.php > /x/x/logs > nohup php -f /rg/repos/trunk/import/importxx-2009.php > ~rbanh/importlogs2009 > (Ctrl+D) > atq // to view queue > atrm 1 // cancel job 1 > at 1am tomorrow -v // tomorrow 1am
-
∞ recursive command to perform sudo actions
// linux: recursive command to perform sudo actions find . -print | sed -e 's/^/\"/' -e 's/$/\"/' | xargs sudo ...
-
∞ jQuery: make LI clickable
<ul> <li><a href="home">home</a></li> <li><a href="home">about</a></li> <li><a href="home">contact</a></li> </ul> ... //selector select all li within the ul and then we make them clickable. $("ul li").click(function(){ //get the url from href attribute and launch the url window.location=$(this).find("a").attr("href"); return false; });
-
∞ jquery event delegation
// =================== // jquery event delegation (degrade) // =================== // so instead of this: // $('#myTable TD').click(function(){ $(this).css('background', 'red'); }); // write this instead: // $('#myTable').click(function(e) { var clicked = $(e.target); alert(e.target.tagName); // rbanh added clicked.css('background', 'red'); }); // ==================== /* 'e' contains information about the event, including the target element that actually received the click. All we have to do is inspect it to see which cell was actually clicked. Much neater. Less event triggers. Your browser will actually slow down if it has to process all those event triggers. Event delegation has another benefit. Normally, When you bind a handler to a collection of elements it gets attached to those elements and those elements only. If you add new elements to the DOM which would have been matched by the selector then they don't have the event handler bound to them (are you following me?) then nothing will happen. (rbanh comment: this is now false with jQuery's live function) When using event delegation you can add as many matching elements to the DOM as you like after the event is bound and they work too. */ // =====================
-
∞ PHP Recursive Sort
<?php // Notice the & symbol in the args and in the FOREACH. // Omitting the & in the FOREACH will not save the sorts! private function recursSort(&$tree) { asort($tree); foreach ($tree as &$t) { if ($t['children'] != '') { $t['children'] = $this->recursSort($t['children']); } } return $tree; }
-
∞ Javscript: convert text so quotes and symbols will appear in input textfields correctly.
// Javscript: convert text so quotes and symbols will // appear in input textfields correctly. function htmlEntities(s) { var i,x,y=''; for(i=0;i<s.length;i++){ x = s[i].charCodeAt(0); if( (x > 47 && x < 58) || (x > 62 && x < 127) ){ y += s[i]; }else{ y += "&#" + x + ";"; } } return y; }
-
∞ Disable Enter Key in jQuery
$(document).ready(function(){ $('#targetId').keypress(function (event){ return event.keyCode == 13 ? false : true; }); });
-
∞ Correct way to do Pagination in Zend
<? //In other words, instead of this: $select= $this->db->select()->from( 'runz_miles', 'EMPL_NO')->where('empl_no = ?', 31713); $this->view->result = $this->db->fetchAll($sql); $paginator = Zend_Paginator::factory($this->view->result); //Do this: $select= $this->db->select()->from( 'runz_miles', 'EMPL_NO')->where('empl_no = ?', 31713); $paginator = Zend_Paginator::factory($select);
-
∞ Command line PHP with args
<?php // processing php arguments if (count($argv) < 2) die("Error 1: arg1 should be filename"); $filename = $argv[1]; // allow only letters and dot if (preg_match("/[^A-Za-z0-9.]/", $filename) || preg_match("/[.]{2,}/", $filename)) die('Error 2'); // Example usage: // /usr/bin/php -f <phpscript.php> file.txt
-
∞ Regex: Date format
<? // Date must match format: mm/dd/yyyy if (!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $due_date)) echo "Error !";



Programming Python