Tuesday, 21 October 2014

URL validation using regular expression for javascript & PHP

Pattern for javascript & PHP /^((http|https):\/\/)?(www[.])?([a-zA-Z0-9]|-)+([.][a-zA-Z0-9(-|\/|=|?)?]+)+$/Validation function in Javascriptfunction checkForValidURL(value) {    var urlregex = new RegExp("^((http|https):\/\/)?(www[.])?([a-zA-Z0-9]|-)+([.][a-zA-Z0-9(-|\/|=|?)?]+)+$");    if (urlregex.test(value)) {        return (true);    }    return (false);}...

Some JavaScript Date Formatting Tips

1. Date formatting from date string    strDate =  "2012-08-02T07:00:00+0000"; // For Eg    function formatDate(strDate){        var monthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");        var date = new Date(strDate);        return monthNames[date.getMonth()]+ ' '+ date.getDate();    } 2. Date formatting from time stampfunction...

MYSQL: HAVING, GROUP BY in SQL UPDATE query

             I have to update the rows of a table in which a column value have unique count, ie the value of that column comes only once. For eg: as per the table shown below:                  I want to update the value of b=1, for every rows having the count of value of column a is unique. Here the value a=3 comes...

MYSQL: JOIN in UPDATE query

       As in the SELECT query we can also use JOIN in UPDATE query. But there is difference in using join in SELECT query and in UPDATE query. For eg:- (in SELECT query)    SELECT * FROM  table1 AS T1    JOIN table2 AS T2 ON T2.t1 = T1.id    WHERE T2.a = 3; As per this we will write the JOIN statement just before the WHERE statement in UPDATE query, but it is wrong. The JOIN statement statement must be near to the first table. That means the SET statement must be after...

display time ago using php

<?php function time_ago_fun($d1){ //Default TimeZone date_default_timezone_set('UTC');     $today = time();                      $createdday= strtotime($d1); //mysql timestamp of when post was created                    $datediff = abs($today - $createdday);                    $difftext="";                    $years...

Monday, 20 October 2014

Decimal / Floating point number validation using javascript / jQuery (regular expression)

To validate any decimal number     function validateDecimal(value)    {         var RE = /^\d*\.?\d*$/;         if(RE.test(value)){            return true;         }else{            return false;         }     } Eg: 54     -> true      1.235  -> true     ...

Regular Expression (Regex)

Regular Expression, or regex or regexp in short, is extremely and amazingly powerful in searching and manipulating text strings. One line of regex can easily replace several dozen lines of programming codes. Regex is supported in almost all the scripting languages (such as Perl, Python, PHP, and JavaScript); as well as general purpose programming languages such as Java; and even word processors such as Word for searching texts. Getting started with regex may not be easy due to its geeky syntax, but it is certainly worth the investment...

Friday, 17 October 2014

PHP RecursiveDirectoryIterator

I  want to do a RecursiveDirectoryIterator on a set of folders in a directory, say ./temp and then list the files in each folder according to the name of the folder. For example I have the folders A and B. In A, I have a list of files say, 1.txt, 2.php, 2.pdf, 3.doc, 3.pdf. In B, I have 1.pdf, 1.jpg, 2.png. I want my results to be like this: A => List of files in A B => List of files in B How can this be done? Use a combination of RecursiveDirectoryIterator and RecursiveIteratorIterator to...

MYSQL import data from csv using LOAD DATA INFILE

I am importing some data of 20000 rows from a CSV file into Mysql. Columns in the CSV are in a different order than MySQL table's columns. How to automatically assign columns corresponding to Mysql table columns? You need to understand LOAD DATA INFILE syntax. LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (col1, col2, col3, col4, col5...) You probably need to set the FIELDS TERMINATED BY ',' or whatever the delimiter happens to be....

MySQL TRIM() function

In the past life was easy in MySQL. Both CHAR and VARCHAR types meant the same, only being difference in the sense of fixed or dynamic row length used. Trailing spaces were removed in both cases. With MySQL 5.0 however things changed so now VARCHAR keeps trailing spaces while CHAR columns do not any more. Well in reality CHAR columns are padded to full length with spaces but it is invisible as those trailing spaces are removed upon retrieval. This is something you need to watch both upgrading to MySQL 5.0 as well as designing your applications...

Thursday, 16 October 2014

Rename a table column in MySQL

Syntax:ALTER TABLE xyz CHANGE manufacurerid manufacturerid datatype(length) Try this also: There is an syntax problem because the right syntax to alter command is ALTER TABLE tablename CHNAGE OldColumnName NewColunmName DATATYPE ; Other information: Add the NOT NULL constraint to a column Alter table table_name change column_name column_name datatype(length) definition ie, Alter table wall change tocken_message tocken_message varchar(40) NOT NULL DEFAUL...

Difference between delete from table_a and truncate table_a in MySQL

Delete:delete rows and space allocated by mysqldata can be roll back againyou can use it with WHERE clauseTruncate:It is similar to delete. But the difference is you can't roll back data again and you cann't use WHERE clause with it. --------------------------------------------------------------------------------------------- Truncate: Works by deallocating all the data pages in the table. Will delete all data - you cannot restrict it with a WHERE clause. Deletions are not logged. Triggers are not fired. Cannot be used if any foreign keys reference...

mySQL Query Remove Null Records in Column

I have a large mySQL database and I want to remove every record that is empty, not null in a certain column. What is the best way to do write a SQL query for this?delete from table_name where column=...

How to update table records with another table

I have a problem with updating records in my table. I am doing research all day but it is just beyond me. Basics: I have two tables TABLE1  TABLE2  I need to update nr_g from TABLE1 with id from TABLE2 but only where 'kraj' 'region' 'nazwa_hotelu' from TABLE1 is equal 'country' 'region' 'hotelName' from TABLE2 Solution: UPDATE merlinx u, merlinx_new s SET u.nr_g = s.id WHERE u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND...