Skip to content

Instantly share code, notes, and snippets.

View rahuldadhich's full-sized avatar

Rahul Dadhich rahuldadhich

View GitHub Profile
@rahuldadhich
rahuldadhich / clear-string.php
Created January 10, 2018 11:44
PHP - Remove all special characters including white space, multiple space
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}
@rahuldadhich
rahuldadhich / mysql-json
Last active January 16, 2018 04:14
MYSQL JSON methods
// compair date from JSON
WHERE updated > JSON_UNQUOTE(json_column->'$.JSON_KEY_NAME')
#####
// SAVE JSON value in UPDATE query
// when don't know either JSON key have set value or not?
UPDATE table SET table_col = JSON_SET(COALESCE(table_col, '{}'), '$.JSON_KEY_NAME', 'json_values')
#####
@rahuldadhich
rahuldadhich / constant-check.php
Last active July 1, 2021 04:42
Check if constant is defined or not - Codeigniter
if(defined('CONSTANT_NAME')) {
// do yes
}
@rahuldadhich
rahuldadhich / constant.php
Created October 9, 2017 11:17
Global array in codeigniter
// Codeigniter
// In application/config/constant.php
$usr_data = array(1 => "a", 2 => "b", 3 => "c");
// In any view
global $usr_data;
echo $usr_data[2];
@rahuldadhich
rahuldadhich / redirect-www
Created September 24, 2017 12:40
htaccess redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourwebsite.com [NC]
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]
@rahuldadhich
rahuldadhich / Template_Loader.php
Created September 23, 2017 09:42
Creating Header and Footer Templates in Codeigniter
<?php
// Creating Header and Footer Templates in Codeigniter
class Template_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
@rahuldadhich
rahuldadhich / Mylist.php
Created September 12, 2017 16:21
Magento Sale Products
class Mage_Catalog_Block_Product_Mylist extends Mage_Catalog_Block_Product_List
{
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
protected function _getProductCollection()
{
$collection = parent::_getProductCollection();
@rahuldadhich
rahuldadhich / array_sort.php
Created August 8, 2017 07:05
Array sort by key
$data_array = array(
array(
'ItemRef' => 20,
'ClassRef' => 'test1',
'Hours' => 20
),
array(
'ItemRef' => 21,
'ClassRef' => 'test2',
'Hours' => 10