pan
pan
Reputation Top 1%
Panagiotis Vagenas
5 Snippets  (154th place)
Published
0 Channels
Created
4 Channels
Following
Apr 8, 2016
Last Visit
Apr 1, 2013
Registered
168 points  (181st place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Popular Coder
Serious Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by pan

public by pan created Feb 5, 2015  2788  3  6  0

Export products for PrestaShop 1.6 DB

You can change language id in line 44 to meet your needs
SELECT
p.id_product AS 'ID',
p.active AS 'Active (0/1)',
pl.name AS 'Name *',
p.price AS 'Price tax excl.',
p.id_tax_rules_group AS 'Tax rules ID',
p.wholesale_price AS 'Wholesale price',
p.on_sale AS 'On sale (0/1)',
p.reference AS 'Reference #',
p.supplier_reference AS 'Supplier reference #',
sl.name AS 'Supplier',
ml.name AS 'Manufacturer',
p.ean13 AS 'EAN13',
p.upc AS 'UPC',
GROUP_CONCAT( cl.name SEPARATOR ', ') as 'Categories (x,y,z,...)',
p.ecotax AS 'Ecotax',
p.width AS 'Width',
p.height AS 'Height',
p.depth AS 'Depth',
p.weight AS 'Weight',
p.quantity AS 'Quantity',
pl.description_short AS 'Short description',
pl.description AS 'Description',
pl.meta_title AS 'Meta-title',
pl.meta_keywords AS 'Meta-keywords',
pl.meta_description AS 'Meta-description',
pl.link_rewrite AS 'URL rewritten',
pl.available_now AS 'Text when in stock',
pl.available_later AS 'Text when backorder allowed',
p.available_for_order AS 'Available for order',
p.date_add AS 'Product creation date',
p.show_price AS 'Show price',
p.online_only AS 'Available online only',
p.condition AS 'Condition'


FROM ps_product p
INNER JOIN ps_product_lang pl ON p.id_product = pl.id_product
LEFT JOIN ps_supplier sl ON p.id_supplier = sl.id_supplier
LEFT JOIN ps_manufacturer ml ON p.id_manufacturer = ml.id_manufacturer
left join ps_category_product cp on p.id_product = cp.id_product
left join ps_category_lang cl on cp.id_category= cl.id_category

WHERE pl.id_lang = 2 and cl.id_lang = 2

GROUP BY p.id_product;            
;

public by pan created Jan 17, 2015  312625  0  7  4

CSS Regex Selector Extension For jQuery

Using this extension enables regular expresions css selectors in jQuery
/**
 * CSS Regex Selector Extension For jQuery
 * 
 */
jQuery.expr[':'].regex = function(elem, index, match) {
   var matchParams = match[3].split(','),
       validLabels = /^(data|css):/,
       attr = {
           method: matchParams[0].match(validLabels) ?
               matchParams[0].split(':')[0] : 'attr',
           property: matchParams.shift().replace(validLabels,'')
       },
       regexFlags = 'ig',
       regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
   return regex.test(jQuery(elem)[attr.method](attr.property));
}
;

public by pan created Oct 14, 2014  245464  2  5  0

Replace all occurrences of a string in DB

Searches and replaces all of occurrences of a string in all tables and all fields of the given Database
<?php
/**
* @kudos http://stackoverflow.com/a/10182498
*/
 
header("Content-Type: text/plain");

$host = "localhost";
$username = "root";
$password = "";
$database = "mydatabase";
$string_to_replace  = 'old.example.com';
$new_string = 'new.example.com';

// Connect to database server
mysql_connect($host, $username, $password);

// Select database
mysql_select_db($database);

// List all tables in database
$sql = "SHOW TABLES FROM ".$database;
$tables_result = mysql_query($sql);

if (!$tables_result) {
    echo "Database error, could not list tables\nMySQL error: " . mysql_error();
    exit;
}

echo "In these fields '$string_to_replace' have been replaced with '$new_string'\n\n";
while ($table = mysql_fetch_row($tables_result)) {
    echo "Table: {$table[0]}\n";
    $fields_result = mysql_query("SHOW COLUMNS FROM ".$table[0]);
    if (!$fields_result) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    if (mysql_num_rows($fields_result) > 0) {
        while ($field = mysql_fetch_assoc($fields_result)) {
            if (stripos($field['Type'], "VARCHAR") !== false || stripos($field['Type'], "TEXT") !== false) {
                echo "  ".$field['Field']."\n";
                $sql = "UPDATE ".$table[0]." SET ".$field['Field']." = replace(".$field['Field'].", '$string_to_replace', '$new_string')";
                mysql_query($sql);
            }
        }
        echo "\n";
    }
}

mysql_free_result($tables_result);
;

public by pan created Sep 25, 2014  2466  1  6  0

Instantiate Date from a mysql datetime string

Adds the ability to Date to instatiate from a mysql datetime string. String should be in format "Y-M-D H:M:S" or "Y-M-D".
/**
     * Instantiate Date from a mysql datetime string
     * @param string mysql_string eg "Y-M-D H:M:S" or "Y-M-D"
     * @returns {Date|null}
     * @kudos http://stackoverflow.com/a/5058318
     */
    Date.createFromMysql = function(mysql_string)
    {
        if(typeof mysql_string === 'string')
        {
            var t = mysql_string.split(/[- :]/);

            //when t[3], t[4] and t[5] are missing they defaults to zero
            return new Date(t[0], t[1] - 1, t[2], t[3] || 0, t[4] || 0, t[5] || 0);
        }

        return null;
    }
;

public by pan created Sep 25, 2014  2132  2  6  0

Get a time modified Date obj in JS

Modifies the time and returns a new Date obj based on old Date, interval and offset. This is an mySQL DATE_ADD() JS implementation.
/**
     * Add time to a date
     * @param Date date Original date
     * @param string interval Interval: year|quarter|month|week|day|hour|minute|second
     * @param int units How many? (could be negative)
     * @returns {Date}
     * @kudos http://stackoverflow.com/a/1214753
     */
    function dateAdd(date, interval, units) {
        var ret = new Date(date); // original date wont be modified
        switch(interval.toLowerCase()) {
            case 'year'   :  ret.setFullYear(ret.getFullYear() + units);  break;
            case 'quarter':  ret.setMonth(ret.getMonth() + 3*units);  break;
            case 'month'  :  ret.setMonth(ret.getMonth() + units);  break;
            case 'week'   :  ret.setDate(ret.getDate() + 7*units);  break;
            case 'day'    :  ret.setDate(ret.getDate() + units);  break;
            case 'hour'   :  ret.setTime(ret.getTime() + units*3600000);  break;
            case 'minute' :  ret.setTime(ret.getTime() + units*60000);  break;
            case 'second' :  ret.setTime(ret.getTime() + units*1000);  break;
            default       :  ret = undefined;  break;
        }
        return ret;
    }
;