Passing a string and using it to call a function

Posted by

I have had a situation where I need to pass a string and then call a jquery function based on the sting, here is the code

HTML

<button id='click'>Click</button>

jQuery

(function($){
    $.fn.myfunc = function()
    {
        alert('hello');
    }
})(jQuery);

$('#click').on('click', function(){
    var functionName = "$.fn.myfunc";
    var fn = new Function("return " + functionName + "();");
    fn();
});

http://jsfiddle.net/david_dataweb/xjf24xm2/


Sort by a field in a multi-dimensioned array

Posted by

usort is a great method to sort a multi-dimensioned array. You supply two variables which is the array and the function to call to sort the results. The example below is within a class, that is why I am using self. In the example I am sorting a date that has come from a MySQL query

private function getSQLResults()
{

     ....
     ....     
     usort($rowData, 'self::sortNextContact');
     ....

}
            

private static function sortNextContact($a, $b)
{
        $aval = strtotime($a['raw_next_contact_due_date']);
        $bval = strtotime($b['raw_next_contact_due_date']);
        
        if ($aval == $bval) {
            return 0;
        }
       
        return $aval < $bval ? -1 : 1;
 }

Comparing two date and/or time in jQuery

Posted by

Here is some code that will enable you to compare two dates and/or time. It will need to be adapted to your own situation and my American friends need to change it as month and date are different in the UK.

Format of date: dd/mm/yyyy
Format of time: 00:00

HTML

<button id='compare_dates'>Compare dates</button>

 

jQuery

(function($){
  
    $.fn.compare_date_time = function(option){
        var settings = $.extend({
            start_date: null,
            start_time: null,
            end_date: null,
            end_time: null,
            message: "End date is before the Start date",
            alert: false
        },option);
        
        var formatStartDateTime = $.fn.splitDateTime(settings.start_date, settings.start_time);
        var formatEndDateTime = $.fn.splitDateTime(settings.end_date, settings.end_time);
      
        if(formatStartDateTime.getTime() > formatEndDateTime.getTime())
        {
            if(settings.alert)
            {
                alert(settings.message);
            }
            return false;
        }
        return true;
    };
  
    $.fn.splitDateTime = function(date,time)
    {
        var dateSplit = date.split('/');
        var day = (typeof dateSplit[0] !== 'undefined') ? parseInt(dateSplit[0]) : 0;
        var month = (typeof dateSplit[1] !== 'undefined') ? parseInt(dateSplit[1]) : 0;
        var year = (typeof dateSplit[2] !== 'undefined') ? parseInt(dateSplit[2]) : 0;
      
        var hour = 0;
        var minute = 0;
      
        if(time && time !== '0')
        {         
            var timeSplit = time.split(':');
            var hour = (typeof timeSplit[0] !== 'undefined') ? parseInt(timeSplit[0]) : 0;
            var minute = (typeof timeSplit[1] !== 'undefined') ? parseInt(timeSplit[1]) : 0;
                   
        }
      
        var dateObj = new Date(year, month - 1, day, hour, minute);
        return dateObj;
    };
  
})(jQuery);

$('#compare_dates').on('click', function(){
$.fn.compare_date_time({start_date: '7/05/2014', start_time: '09:30', end_date: '7/05/2014', end_time: '09:20', alert: true});

$.fn.compare_date_time({start_date: '7/05/2014', end_date: '6/05/2014', alert: true});
});

 

Test on jsfiddle

 


Constructing an address

Posted by

Sometimes I need to put together an address but I do not want blank fields from the database table. This is one of the ways I do this.

$address = ((!empty($rows['address1'])) ? $rows['address1'] . "<br />" : "") .
           ((!empty($rows['address2'])) ? $rows['address2'] . "<br />" : "") .
           ((!empty($rows['address3'])) ? $rows['address3'] . "<br />" : "") .
           ((!empty($rows['address4'])) ? $rows['address4'] . "<br />" : "") .
           ((!empty($rows['postcode'])) ? $rows['postcode']  : "");

Copy a MySQL table

Posted by

If you need to copy a table or rows within the table with SQL use the following. It does not copy indexes and keys

create table table_name_copy as select * from table_name

Text areas and emailing

Posted by

I had a slight problem. I had an area in our CRM where you could add a note and email it. The note was composed in a <textarea>. When it was sent to mail function in PHP all the new line characters where changed into strings like ‘Dear Sir\r\n’. Not what the person receiving wants. What I found was that all these characters had been escaped. To solve it I used the str_replace PHP function. Hope it helps someone.


$message = str_replace("\\r\\n", "\r\n", $message);
mail($email, $subject, $message);

* Please note the above is a simple example



Sorting results from a sql union

Posted by

When you construct a union, you can only sort the results of each sql statement. To sort all sql tables within the union you can do the following.

select *
from (
    select client AS company_name from clients 
    union all
    select 2 as supplier AS company_name from suppliers
) temp_table_name
order by company_name

Open google maps using a postcode and jQuery

Posted by

I work on a CRM system used by IT engineers. They needed to click on a postcode and opens up the location in google maps. Below is an example of the code I used.
Some comments.

  • I check for an empty postcode.
  • Google uses a plus sign between the parts of the postcode.
  • I use a timestamp so the engineer can open up more than one window. If not used any link opened will open in the same window

HTML


<input type='text' id='postcode' value='SW1A 0AA' />
<button id='google_maps_button'>Open google maps</button>

jQuery


$('#google_maps_button').on('click', function(){
      var timestamp = new Date().getTime();
      if($('#postcode').val() !== "")
      {
          var postcode = $('#postcode').val().replace(' ', '+');
          window.open('http://google.com/maps?q=' + postcode, "GOOGLEMAP" + timestamp,"width=1200, height=800");     
      }
   });

See example at http://jsfiddle.net/david_dataweb/97dhK/