Skip to content

Instantly share code, notes, and snippets.

@khoand0000
khoand0000 / add-element.md
Last active December 3, 2015 19:41
add element into another element #append #prepend
  • .append() Insert content, specified by the parameter, to the end of each element in the set of matched elements. .appendTo() is same task
<h2>Greetings</h2>
<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>
@khoand0000
khoand0000 / lightbox.md
Created December 3, 2015 18:13
lightbox libraries
  • 1st way (I don't like it, it's long) but it suits for checking object has the field or not (2nd way is not applied)
var attr = $(this).attr('name');

// For some browsers, `attr` is undefined; for others, `attr` is false. Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
  // Element has this attribute
}

Wrap disabled <input> in <div> and catch click event on <div> instead of <input> directly

<div class="disabled"> 
  <input type="text" disabled/>
</div>
$('.disabled').click(function(e){});
// $('[disabled]').click(function(e){}); // not working
@khoand0000
khoand0000 / pagination.md
Last active December 3, 2015 09:57
pagination calculating
  • I want to show result (from-to) at current page. Example: each page having 50 results, will show: 1-50, 51-100, ..., 501-503 final page has not enough 50 results
var resultsPerPage = 50;
var countOfResults = 451; // count of total results  ==> max of page = 10
var countOfCurrentPage = 50; // results of current page, from 1 --> 50
var page = 1; // current page, page begin from 1, not 0
var from = Math.min(countOfResults, 1 + (page - 1) * resultsPerPage); // is `countOfResults` when from page > max of page
var to = (page - 1) * resultsPerPage + countOfCurrentPage;
if (to > countOfResults) to = countOfResults;
@khoand0000
khoand0000 / snippets.md
Last active December 14, 2015 12:03
snippets

Remove item from json object

var a = {x: ''};
delete a.x;
delete a['x']; // same

Loop json object

@khoand0000
khoand0000 / layout-css.md
Last active December 23, 2015 08:32
2-columns or 3-columns layout css
  • Columns are calculated by percent (not pixel): using float and width: x% for each columns. It suits for columns
<div class="container">
  <div class="left">left</div>
  <div class="right">right</div>
  <div class="clear"></div>
</div>
.left {
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
//$zip->open($zipname, ZipArchive::CREATE); // the mode will append more files to existing file
$zip->open($zipname, ZipArchive::OVERWRITE); // always create new file even file is existing
foreach ($files as $file) {
// $zip->addFile($file); // original code if you want to keep original path of $file when extracting zipped file, means that $zip will create hierarchy folders contain the file
$zip->addFile($file, basename($file)); // changed, if you want to zip all files in single folder
}
$app = new \Slim\Slim();
$app->get('/downloadlog', function () use($app) {
$log = 'mylogfile.log';
$res = $app->response();
$res['Content-Description'] = 'File Transfer';
$res['Content-Type'] = 'application/octet-stream';
$res['Content-Disposition'] ='attachment; filename=' . basename($log);
$res['Content-Transfer-Encoding'] = 'binary';
$res['Expires'] = '0';
  • WP supports only function get a post is get_post() function, but it just get information of post when you know post_id
$post = get_post($post_id);
  • If you want to get post without post_id, must to use get_posts() function
$args = array(
  'posts_per_page' => 1, // required
 // ... your conditions, ex: 'post_type' =&gt; 'post', 'post_author' =&gt; 1