Wednesday, 1 June 2011

Check all checkboxes through JQuery on a Checkbox event

 //chkAllSub function calls on sub checkboxes click
//chkAll function calls on main checkbox
//.sub_chk is the class of sub checkboxes
//master is the ID of main checkbox



function chkAllSub()
{
  $(".sub_chk").each(function(){
    var chkboxLength = $("input.sub_chk").length;
    if($("input.sub_chk:checked").length >= chkboxLength)
    {
      $("#master").attr('checked',true);
    }
    else
    {
      $("#master").attr('checked',false);
    }
  })
}
function chkAll()
{
  if($("#master").is(':checked',true))
  {
    $(".sub_chk").attr('checked',true);
  }
  else
  {
    $(".sub_chk").attr('checked',false);
  }
}

Monday, 9 May 2011

Check all checkboxes through JQuery

$(document).ready(function(){
$(".checkall").click( function() {
  $("input[type='checkbox']:not([disabled='disabled'])").attr('checked', true);
})

})

Find any part of string in another string & manipulate accordingly

<?php
$string = 'www.google.com';//string
$ss = strpos($string,'http://');//cheking whether the part of string exist in full string or not
if($ss!==false)//if exists
{
  $string = $string;
}
else
{
  $string = 'http://'.$string;
}
echo $string;//final string.
?>

Friday, 6 May 2011

Regular Expression in MySQL

WHERE phone REGEXP '[(]435[)]';

would return only phone numbers with (435), such as "1(435)555-5555".

GroupConcat in MySql directly gives the output in required format as string

Language is column name and CountryLanguage is table name

English
Hindi
Punjabi
Chinese

SELECT GROUP_CONCAT(Language) As Languages FROM CountryLanguage;

Output will be:

English,Hindi,Punjabi,Chinese

SELECT GROUP_CONCAT(Language SEPARATOR '-') As Languages FROM CountryLanguage;
Output will be:


English-Hindi-Punjabi-Chinese

Wednesday, 4 May 2011

Get data through JSON

//Javascript Code

<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#sample").click(function(){

  $.ajax({
      type:'GET',
      url :'testAjax.php',
      success:function(msg)
      {
        var jvar = eval('(' + msg + ')');
        alert(jvar.a);
        alert(jvar.b);
      }
  })
})
})
</script>

//Sample Div

<div id="sample" style="cursor:pointer;>here</div>


//PHP request page

<?php
$arr = array('a'=>5,'b'=>8);
$encodedVal = json_encode($arr);
echo $encodedVal;
?>

Fibonacci series in PHP

<?php

$first = 0;
$second = 1;
$n = 20; // number of times you want to print the value
print $first.'<br>'; // print the first value
for($i=1;$i<$n;$i++)
{
  $final = $first + $second;
  $first = $second;
  $second = $final;
  print $final.'<br>';
}

?>