Skip to content

Instantly share code, notes, and snippets.

View EricYue2012's full-sized avatar

Eric Yue EricYue2012

  • Sydney
View GitHub Profile
@EricYue2012
EricYue2012 / lib.js
Last active August 6, 2021 01:49
javascript
// 1. 按名字取得网址中的query string
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, " "));
}
// example
// current URL: http: //www.test.com/index.php?name=John
@EricYue2012
EricYue2012 / functions.php
Last active August 6, 2021 01:47
WordPress
<?php
// 1. 显示当前模板文件的路径
// 1. show template file for page
// - put in theme functions.php
function ey_which_template_is_loaded() {
if ( is_super_admin() ) {
global $template;
print_r( $template );
}
@EricYue2012
EricYue2012 / php_output_excel
Created December 6, 2013 00:40
PHP output excel file
http://stackoverflow.com/questions/8082523/export-records-in-excel-file
1. Do query in php to get the rows you want to output
2. You can use SELECT * FROM table WHERE id IN (1,2,...)
3. Use mysql_fetch_array() or mysql_fetch_assoc() to get the rows one at a time
4. Use fputcsv() to output them to a file ending in csv - this will properly escape your data
Excel will be able to read the file.
Override the defaults for fputcsv to use tabs for delimiters and Excel will have an even easier time reading the file. If you use commas (the default) you may need to pick commas as the delimiter on Excel import.
@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" );
?>
@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 / 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 / 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 / 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_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 / 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;