Tuesday, 14 August 2012

checkbox implementation with jquery

jQuery(document).ready(function(){
    jQuery('.food_list').click(function() {
            //alert(jQuery(this).attr('id'));
            var l = jQuery('.food_list').filter(':checked').length;
          
            //alert(b);
            if(l > 5)
            {
                alert('Please select only 5 food types');  
                return false;
            }
          
    });                          
    jQuery('#step4').click(function() {
            //alert(jQuery(this).attr('id'));
            var l = jQuery('.chk_like').filter(':checked').length;
            var d = jQuery('.chk_dislike').filter(':checked').length;
            //alert(b);
            if(l < 1 && d < 1)
            {
                alert('Please select like or dislike restaurants');  
                return false;
            }
    });                          
    jQuery('#step5').click(function() {
            //alert(jQuery(this).attr('id'));
            var b = jQuery('input[type=checkbox]');
            var chk_length = b.filter(':checked').length;
            if(chk_length > 2)
            {
                alert('Please select only 2 restaurants');  
                return false;
            }
            else if(chk_length < 2)
            {
                alert('Please select 2 restaurants');  
                return false;
            }
          
    });                          
    jQuery('.chk_like').click(function() {
            //alert(jQuery(this).attr('id'));  
            var cl = jQuery(this).attr('id').split('like');
            jQuery("#dislike"+cl[1]).is(':checked') ? jQuery(this).attr('checked',false) : true;
            //jQuery(this).is(':checked') ? true : false;
            if(jQuery(this).is(':checked'))
            {
                jQuery("#have_been"+cl[1]).attr('checked',true);
            }
            else
            {
                if(jQuery("#dislike"+cl[1]).is(':checked'))
                    jQuery("#have_been"+cl[1]).attr('checked',true);
                else
                    jQuery("#have_been"+cl[1]).attr('checked',false);
            }
        });
    jQuery('.chk_dislike').click(function() {
            //alert(jQuery(this).attr('id'));
            var cd = jQuery(this).attr('id').split('dislike');
            //alert(d[1]);
            //alert("#like"+d[1]);
            jQuery("#like"+cd[1]).is(':checked') ? jQuery(this).attr('checked',false) : true;
            //jQuery(this).is(':checked') ? true : false;
            if(jQuery(this).is(':checked'))
            {
                jQuery("#have_been"+cd[1]).attr('checked',true);
            }
            else
            {
                if(jQuery("#like"+cd[1]).is(':checked'))
                    jQuery("#have_been"+cd[1]).attr('checked',true);
                else
                    jQuery("#have_been"+cd[1]).attr('checked',false);
            }
        });
                      
    /* setup the form validation */
    jQuery('#mainform').validate({                        
        errorClass: 'invalid',
      
        errorPlacement: function(error, element) {
            if (element.attr('type') == 'checkbox' || element.attr('type') == 'radio') {
                element.closest('ol').after(error);
            } else {
              
                offset = element.offset();
                error.insertBefore(element)
                error.addClass('message');  // add a class to the wrapper
                error.css('position', 'absolute');
                error.css('left', offset.left + element.outerWidth());
                error.css('top', offset.top);
            }  
        }

    });

    /* setup the tooltip */
    jQuery("#mainform a").easyTooltip();

});

How to call widget in our site in wordpress

Step 1 : Create one sidebar in sidebars file.

<?php
register_sidebar(array(
        'name'=>'moreWidgets',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
    ));
?>

Step 2: Goto admin section and list out widget there. one sidebar is created with name moreWidgets. Drag any widget in that sidebar.

Step 3 : put this code in anywhere in your site....Widget functionality work there.....
<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('moreWidgets') ) :
        endif;
?>

Show map on mouse hover

You need to add google map api script here......
<a href="javascript:void(0);" onmouseover='showMap(event,"2766 W 4th Vancouver","Momento Coffee");' onmouseout="removeMap();">View Map</a>
function showMap(e1,address,title)
 {
    var preview = document.createElement('div');
    var previewmap = document.createElement('div');
    previewmap.setAttribute('id','map');
    preview.appendChild(previewmap);
    preview.setAttribute('id','preview');
    document.body.appendChild(preview);
    //alert(document.getElementById('map').innerHTML);
     var xOffset = 50;
     var yOffset = 50;
    // alert(e.pageY);
   
     //alert(title);
    document.getElementById('map').style.display='block';
    document.getElementById('preview').style.display='block';
   
    var map = null;
    var geocoder = null;
    var fromAdd;
    var toAdd;
    var redFlag = "/images/red-flag.png";
    var shadow = "/images/red-flag-shadow.png";
    var noLuck = "/images/gmaps-no-result.gif";
    //var title = "<?php esc_js(the_title()); ?>";
    var contentString = '<div id="mcwrap"><span>'+title+'</span><br />' + address+ '</div>';

    geocoder = new google.maps.Geocoder();
        //directionsDisplay = new google.maps.DirectionsRenderer();
    var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
    var myOptions = {
        zoom: 14,
        center: newyork,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        }
    }
    map = new google.maps.Map(document.getElementById('map'), myOptions);


         geocoder.geocode( { 'address': address }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter();

            var marker = new google.maps.Marker({
                map: map,
                icon: redFlag,
                shadow: shadow,
                //title: title,
                animation: google.maps.Animation.DROP,
                position: results[0].geometry.location
            });

            var infowindow = new google.maps.InfoWindow({
                maxWidth: 270,
                maxHeight: 270,
                content: contentString,
                disableAutoPan: false
            });

            infowindow.open(map, marker);

            google.maps.event.addListener(marker, 'click', function() {
              infowindow.open(map,marker);
            });

          } else {
           
           
          }
        });
    document.getElementById('preview').style.top = (e1.pageY - xOffset)+'px';
    document.getElementById('preview').style.left = (e1.pageX + yOffset)+'px';   
}
function removeMap()
{
    var el = document.getElementById('preview');
    var remElement = (el.parentNode).removeChild(el);   
}

Send Invitation request to facebook friends from our site

<div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    
    <input type="button"
      onclick="sendRequestViaMultiFriendSelector(); return false;"
      value="Send Request to Many Users"
    />
    </p>
    
    <script>
      FB.init({
        appId  : 'YOUR_APP_ID',
        frictionlessRequests: true,
      });

      function sendRequestViaMultiFriendSelector() {
        FB.ui({method: 'apprequests',
          message: 'My Great Request'
        }, requestCallback);
      }
      
      function requestCallback(response) {
        // Handle callback here
      }
    </script>

Thursday, 9 August 2012

How to read csv file

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            if($data[$c] !='')
                echo trim($data[$c]) . "<br />\n";
        }
    }
    fclose($handle);
}
?>

How to download file in php

<?php
// your file to upload
$file = 'text.txt'; // your file destination path
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/txt"); // change content-type according to your file to be download
// tell file size
header('Content-length: '.filesize($file));
// set file name
header('Content-disposition: attachment; filename='.basename($file));
readfile($file);
// Exit script. So that no useless data is output-ed.
exit;
?>

Arrange ul li in tabular format

<?php
$i = 1;
?>
<ul>
<?php
while($i <= 10)
{
    ?><li><?php echo $i;?></li>
    <?php
    if($i%3 == 0)
    {
        ?></ul><ul>
        <?php   
    }
   
    $i++;
}
?>
</ul>