Skip to content

Instantly share code, notes, and snippets.

View EricYue2012's full-sized avatar

Eric Yue EricYue2012

  • Sydney
View GitHub Profile
@EricYue2012
EricYue2012 / gist:7366890
Created November 8, 2013 06:07
PHP : Age Calculator based on Birth Date
test
@EricYue2012
EricYue2012 / 0_reuse_code.js
Created November 14, 2013 03:41
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@EricYue2012
EricYue2012 / php_remove_query_string_key
Created November 14, 2013 03:44
Remove specified key from url question string
function remove_url_key($key){
$base_uri = $_SERVER['QUERY_STRING'];
$_new_uri = array();
if(!isset($_GET[$key]))
{
$q_string = $base_uri;
}else
{
foreach($_GET as $uri=>$val)
{
@EricYue2012
EricYue2012 / php_age_calculator
Created November 14, 2013 03:49
Calculate age based on birthday
// version 1:
// param: $birth_date (input string: YYYY-MM-DD)
function birthday ($birth_date){
list($year,$month,$day) = explode("-",$birth_date);
$today = date('d-m-Y');
$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);
$age = $a_today[2] - $year;
@EricYue2012
EricYue2012 / javascript_clicked_redirect
Created November 14, 2013 04:13
Update URL query string and redirect
//HTML
<select id="year_selector" onchange="redirectTo(this)">
<option value="">Select Year</option>
<option value="2014">2014</option><option value="2013">2013</option>
</select>
//Javascript
@EricYue2012
EricYue2012 / javascript_get_url_query_value
Created November 14, 2013 04:26
Get URL query string value
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
@EricYue2012
EricYue2012 / javascript_add_or_update_url_query_parameter_value
Created November 14, 2013 04:29
Add or Update URL query string value
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?|&])" + key + "=.*?(&|$)", "i");
separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
@EricYue2012
EricYue2012 / php_solution_order_by new_field
Created November 14, 2013 06:04
Re-order data when click a column header (new field)
1. click html header -> generate new url with query string including new orderby field name
2. php parse query string, handle filed name
//html
<tr><td align="left" width="10%" class="table_toprow"><div class="orderby" id="job_serial_number"><b>Serial Number</b></div></td>
<td align="left" width="10%" class="table_toprow"><div id="invoice_status"><b>Invoice Status</b></div></td>
<td align="left" width="20%" class="table_toprow"><div class="orderby" id="job_name"><b>Name</b></div></td>
<td align="left" width="10%" class="table_toprow"><div class="orderby" id="job_brand"><b>Brand</b></div></td>
@EricYue2012
EricYue2012 / php_force_brower_download
Created November 22, 2013 05:12
PHP: force browser download file
header("Pragma: public");
header("Expires: 0");
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-disposition: attachment; filename=' . '1006_3d-HK-Flag.png');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize(WWW_ROOT.'/feedback_files/1006_3d-HK-Flag.png'));
@EricYue2012
EricYue2012 / cakephp_output_excel_file
Created December 6, 2013 00:23
How to generate excel file in cakephp
//excel layout
<?php
header ("Expires: Mon, 28 Oct 2008 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=\"report_".date('dmy_His').".xls" );
header ("Content-Description: Generated Report" );
?>