Sunday, December 29, 2013

if primary id is exist update record otherwise insert record in mysql ( ON DUPLICATE KEY)

Same Query we can do the insert and update process in mysql

For Example:

Consider following query  if key id is  '1' existing table  it will perform update query like second query. otherwise it will insert as new record.

INSERT INTO product (id,name,price) VALUES (1,'jeeva','$100')
  ON DUPLICATE KEY UPDATE name='jeeva',price='$100';

UPDATE table SET name='jeeva',price='$100' WHERE id=1;

Friday, May 3, 2013

Suffix String with file name using php


public function filename_suffix($path,$suffix='')
{
$parts = pathinfo($path);
return $parts['dirname'].'/'.$parts['filename'].$suffix.'.'.$parts['extension'];
}

$f = pathinfo('http://cdn.shopify.com/s/files/1/0229/6827/products/tata_compact.jpg?100');
filename_suffix($f,$suffix='_thumb');

Tuesday, April 23, 2013

How to use comma seperate Ids values in JOIN Quereis



Example:

    Table: Customers

     Id  Ordersids
     10  2,3

     Table: Orders
     id price
     2   10
     3   11

     Query:

     select b.id, b.price from Customers a left join Orders b on find_in_set(b.id,a.orders) group by b.id


Getting json data using php curl


        get_json.php:
~~~~~~~~~~~~~

$arr = "your array result";
        echo json_encode($arr);


json_result.php:
        ~~~~~~~~~~~~~~~~

$url  = 'get_json.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept: application/json', "Content-type: application/json"));
curl_setopt($ch, CURLOPT_URL, $url );

$return = curl_exec($ch);

curl_close ($ch);
$result = json_decode($return,true);

Tuesday, March 26, 2013

Seconds to days, hours, minutes in PHP


function sectotime($sec)
{
$d = str_pad(floor($sec/86400),2,0,STR_PAD_LEFT);
$h = str_pad(floor(($sec%86400)/3600),2,STR_PAD_LEFT);
$m =  str_pad(floor(($sec%86400)%3600/60),2,0,STR_PAD_LEFT);
return $d.':'.$h.':'.$m;
}

sectotime(90000);

Thursday, February 14, 2013

How compare the to decimal point values using javascript


// check the amount greater...

if(Math.max(parseFloat(amount).toFixed(2),parseFloat(bal).toFixed(2))==parseFloat(amount).toFixed(2))
{
alert("amount is greater"+amount);
}

Sunday, February 3, 2013

Tooltip show for Image Thumnail:

 
#large {
    background: none repeat scroll 0 0 #FEFEFE;
    border: 4px solid #45B6EA;
    border-radius: 4px 4px 4px 4px;
    color: #FFFFFF;
    display: none;
    padding: 5px;
    position: absolute;
}

<div id="box">
        <h3>Hover over the link</h3>
            
        <p id="large"></p>
<a title="/logo.png" class="jobimage" style="cursor:pointer;">image</a>
</div>  

$(document).ready(function ()
{
    $(".jobimage").hover(function(e){
            $("#large").css("top",(e.pageY+5)+"px")
                             .css("left",(e.pageX+5)+"px")                    
                             .html("<img src="+ $(this).attr("title") +" alt='Large Image' />")
                             .fadeIn("slow");
        }, function(){
            $("#large").fadeOut("fast");
        });    
    
});

Friday, February 1, 2013

Create Web User in Plesk panel using PHP


Following class file will help you:
class Webuser
{
var $host = 'Hostname';
var $login = 'userid';
var $password = 'password';

public function sendRequest($packet='')
{
$host = $this->host;
  $login = $this->login;
  $password = $this->password;

$curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php");
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_POST,           true);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($curl, CURLOPT_HTTPHEADER,
              array("HTTP_AUTH_LOGIN: {$login}",
                    "HTTP_AUTH_PASSWD: {$password}",
                    "HTTP_PRETTY_PRINT: TRUE",
                    "Content-Type: text/xml")
      );

  curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
      $result = curl_exec($curl);
      if (curl_errno($curl)) {
              $errmsg  = curl_error($curl);
              $errcode = curl_errno($curl);
              curl_close($curl);
             
      }
      curl_close($curl);
$xml = new SimpleXMLElement($result);
return $xml;
}


public function createUser($domainid='',$username='',$password='')
{
$xmldoc = new DomDocument('1.0', 'UTF-8');
      $xmldoc->formatOutput = true;
      $packet = $xmldoc->createElement('packet');
      $packet->setAttribute('version', '1.6.0.2');
      $xmldoc->appendChild($packet);

$webuser = $xmldoc->createElement('webuser');
  $packet->appendChild($webuser);

$add = $xmldoc->createElement('add');
  $webuser->appendChild($add);

  $site_id = $xmldoc->createElement('domain-id',$domainid);
  $add->appendChild($site_id);

  $login = $xmldoc->createElement('login',$username);
  $add->appendChild($login);
 
  $password = $xmldoc->createElement('password',$password);
  $add->appendChild($password);
   
$xmloutput = $xmldoc->saveXML();
return $this->sendRequest($xmloutput);
 
}

public function deleteUser($username='')
{
$xmldoc = new DomDocument('1.0', 'UTF-8');
        $xmldoc->formatOutput = true;
        $packet = $xmldoc->createElement('packet');
        $packet->setAttribute('version', '1.6.0.2');
        $xmldoc->appendChild($packet);

$webuser = $xmldoc->createElement('webuser');
   $packet->appendChild($webuser);

$del = $xmldoc->createElement('del');
  $webuser->appendChild($del);  
     
  $filter = $xmldoc->createElement('filter');
  $del->appendChild($filter);
 
  $login = $xmldoc->createElement('login',$username);
  $filter->appendChild($login);  
     
  $xmloutput = $xmldoc->saveXML();
return $this->sendRequest($xmloutput);

}

public function getError(SimpleXMLElement $result)
{
$err = '';
if(isset($result->status) && (string)$result->status == 'error'){ $err = $result->errtext; }
return $err;
}
}

$siteid= ''
$username = '';
$password = '';

$obj = new Webuser();
$response = $obj->createUser($siteId,$userName,$password);
if($response->webuser->add->result->status=='ok')
{
echo json_encode(array('status'=>1,'msg'=>'success'));
}
else
{
$msg= $response->webuser->add->result->errtext;
echo json_encode(array('status'=>0,'msg'=>$msg));
}

Tuesday, January 29, 2013

Animate div with browser scroll


precurscrol = 0;
$(window).scroll(function () {
    //You've scrolled this much:

totalscroll = $(document).height() - $(window).height();
        curscrol = $(window).scrollTop();
if(curscrol<(totalscroll-115))
{
if(precurscrol>curscrol){ curscrol = curscrol+100;  }
if(curscrol>70 )
{
$("#leftbar").css({'margin-top':curscrol+'px'});
curscrol = curscrol-20;
//$("#leftbar").animate({'margin-top':curscrol+'px'},'fast',function () {})
}
precurscrol = curscrol;
//console.log(precurscrol);

}

});

Monday, January 28, 2013

php ajax pagination

<script type="text/javascript">
function getUser(pno)
{
data = "pno="+pno;

$.ajax({
url: base_url+'/AjaxUser.php',
sync: true,
type: 'POST',
data: data,
success: function(resp) {

$("#listhistory").html(resp);
}
});
}
</script>
<div id="listhistory">
<?php
//Ajax Pagination
$w = '';
// getting page number
$pageno = (isset($_POST['pno']))?$_POST['pno']:'';
// for the filter option
$status = isset($_SESSION['status'])?$_SESSION['status']:'';
$status = (isset($_POST['Status']))?$_POST['Status']:$status;

if(!empty($status))
{
$_SESSION['paystatus'] = $status;
$w .= " and Status='$status'";
}
// page number calculation
if(empty($pageno)) $pageno = 1;
$perpage = 5;
$start = ($pageno-1)*$perpage;
// getting total no rows
$query = "select * from users where 1 $w";
$rett = mysql_query($query,$con);
$totrows = $mysql_num_rows($rett);
$query = "select * from users where 1 $w limit $start, $perpage";
$ret = $mydb->query($query);
$rs = $mydb->result_array($ret);
$i=$start+1;
if($mydb->num_rows($ret)>0)
{
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="tabgray" >
<tr><th>Sno</th><th>Username</th><th>email</th><th>Status</th></tr>
<? foreach($rs as $r) { ?>
<tr><td><?php echo $i; ?></td><td><?php echo $r['username']; ?></td><td><?php echo $r['email'] ?></td><td><?php echo $r['status']; ?></td></tr>
<?
$i++; }}
?>
</table>
<?php
// Pagination Links
$nopages = floor($totrows/$perpage)+(($totrows%$perpage)?1:0);
$lfrom = ($pageno>3)?$pageno-3:1;
$lto = ($pageno+5<$nopages)?$pageno+5:$nopages;
?>
<div>
<div class="pagination" style="float:left;">
<?php if($lfrom!=1) { ?><span class="btn" onclick="getUser(1);">1</span><strong>...</strong><?php } ?>
<?php for($li=$lfrom;$li<$lto;$li++){
$cls = ($pageno==$li)?'btnsel':'btn';
?>
<span class="<?php echo $cls; ?>" onclick="getUser(<?php echo $li; ?>);"><?php echo $li; ?></span>
<?php } ?>
<?php if($nopages>3){ ?><strong>...</strong><span class="btn" onclick="getUser(<?php echo $nopages; ?>);"><?php echo $nopages; ?></span> <?php } ?>
</div>

</div>
</div>
<script type="text/javascript">
getUser(1);
</script>

Sunday, January 27, 2013

how get no of between two dates


       $from = strtotime($from);
  $to = strtotime($to);
$r = $to-$from;
floor($r/86400);

how get no of sundays between to dates


$from = "from date";
$to = "to date";
$sunday = 0;
  $from = strtotime($from);
  $to = strtotime($to);
for($i=$from;$i<=$to;$i+=86400)
  {
  $d = date('w',$i);
if($d=='0') $sunday += 1;
  }

Thursday, January 24, 2013

Jquery Basics things


Jquery Basics:

1)Getting Values of following elements:

a) All form elements
b) Div, span etc

2)Setting values to the following elements:

a) All form elements
b) Div, span etc

3) Check whether the following elements have values or not:

a) All form elements
b) Div, span etc

3) Check whether the following elements displyed or not:

a) All form elements
b) Div, span etc

4) Jquery methods
 a) ajax
 b) post
          c) serialize

5) get and set attribute of elements in jquery.
6) Add or Remove class to elements in jquery.
7) How to apply css to elements in jquery.

Show Hide the corresponding div

<span class="question" id="1">what is your name?</span>
<div id="ans1">
test</div>
<span class="question" id="2">what is your name?</span>
<div id="ans2">
test</div>
<span class="question" id="3">what is your name?</span>
<div id="ans3">
test</div>
$(document).ready(function()

{
 $(".question").click(function()
 {
  id= $(this)attr('id');
  $("#ans"+id).slideToggle();
 }

 )
 }

);

Bind Some Event to Element and trigger that function when click the element

Scenario: Suppose if we used same click event function in various web pages. if you want do some logic some page button for that need to re...