Tuesday, September 24, 2013

social network share code

<script>
function facebook(){
var urlToShare = 'http://ersegment.com';
var link = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(urlToShare);
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');
}
</script>
<script>
function tweeter(){
var urlToShare = 'http://ersegment.com';
var link = 'http://twitter.com/intent/tweet?url='+encodeURIComponent(urlToShare);
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');
}
</script>
<script>
function linkedin(){
var urlToShare = 'http://ersegment.com';
var link = 'http://www.linkedin.com/shareArticle?url='+encodeURIComponent(urlToShare);
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');
}
</script>


<script>
function twitter(){
var urlToShare = 'www.google.com';
var link = 'http://twitter.com/intent/tweet?url='+encodeURIComponent(urlToShare);
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');

}
function facebook(){
var urlToShare = 'www.google.com';
var link = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(urlToShare);
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');

}
function googleplus(){
var urlToShare = 'www.google.com';
var language = 'de';
var link = 'https://plus.google.com/share?url='+encodeURIComponent(urlToShare)+'&hl='+language;
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');
}
function linkedin(){
var urlToShare = 'www.google.com';
var link = 'http://www.linkedin.com/shareArticle?url='+encodeURIComponent(urlToShare);
window.open(link,'sharer','toolbar=0,status=0,width=640,height=440');
}
</script>

<ul>
<li><a href="#" onclick="facebook()" class="facebook" title="Share on Facebook"><img src="images/fb.png" /></a></li>
<li><a href="#" onclick="tweeter()" class="twitter"  title="Share on Twitter"><img src="images/tw.png" /></a></li>
<!--<li><a href="#" class="google"  title="Share on Google+"><img src="images/go.png" /></a></li>-->
<li><a href="https://plus.google.com/share?url={http://ersegment.com}" onclick="javascript:window.open(this.href,
  '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><img
  src="/images/go.png" alt="Share on Google+" class="google"/></a></li>

<li><a href="#" class="bing"  title="Share on Rss"><img src="images/bi.png" /></a></li>
<li><a href="#" onclick="linkedin()" class="in"  title="Share on Linkedin"><img src="images/ing.png" /></a></li>
</ul>

Friday, July 5, 2013

zend form example

<?php
class Application_Form_UserForm extends Zend_Form
{
public function init()
{

$fname = $this->createElement('text','firstName')
->setLabel('First Name*')
->setRequired(true)
->setAttrib('size', '15');

$lname = $this->createElement('text','lastName')
->setLabel('Last Name*')
->setRequired(true)
->setAttrib('size', '15');



$email = $this->createElement('text', 'email', array(
      'label' => 'Email Address*',
      'required' => true,
      'validators' => array(
        array('NotEmpty', true, array('messages' => 'Please Enter Email')),
        array('regex', false, array('pattern' => '/^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/',
        'messages'=>array(Zend_Validate_Regex::NOT_MATCH=>'%value% is not a valid Email')
    )))
       )
  );



$password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password*')
->setRequired(true)
->addFilter(new Zend_Filter_StringTrim())
    ->addValidator(new Zend_Validate_NotEmpty())
                    ->setAttrib('size',15);

        $password2 = new Zend_Form_Element_Password('confirm_password');
        $password2->setLabel('Confirm Password*')
->setRequired(true)
->addFilter(new Zend_Filter_StringTrim())
->addValidator('Identical', false, array('token'=>'password', 'messages'=>'Passwords mismatch!'       )      )

->setAttrib('size',15);

$captcha = new Zend_Form_Element_Captcha('captcha', array(
'label' => 'Security Check',
'required' => true,
'captcha' => array(
'captcha' => 'Image',
'font' => APPLICATION_PATH . '/../public/captcha/AllerDisplay.ttf',
'fontSize' => '24',
'wordLen' => 6,
'height' => '50',
'width' => '150',
'imgDir' => APPLICATION_PATH . '/../public/captcha',
'imgUrl' => Zend_Controller_Front::getInstance()->getBaseUrl() . '/captcha',
)
    ));



  $submit = $this->createElement('submit', 'submit')
->setAttrib('class', 'botton')
->setName('Submit');

$this->addElements(array($fname, $lname, $email, $password, $password2, $captcha,  $submit));
}
}
?>

Wednesday, August 15, 2012

Zend Framework SQL Joins Examples


You may have custom of using advanced queries. It often requires writing complex queries if you are working on enterprise, large scale web application(s).
The use of joins can never be ignored.
Zend Framework developers have done tremendous job by providing simple method for implementing joins.
Lets look some examples of different type of joins.
Before discussing joins lets consider we have two tables, “authors” and “books”.
These are associated with author_id.

1. Inner Join
The simplest query will be
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinInner('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->order('column name ASC/DESC');

2. Left Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinLeft('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

3. Right Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinRight('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

4. Full Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

5. Cross Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

Once you write these queries, you can fetch a single row or multiple rows as

$result = $this->getAdapter()->fetchRow($select);
$result = $this->getAdapter()->fetchAll($select);

The first statement fetch only one row, while the second statement fetch the entire dataset.


6. Insert


$table = new Bugs();

$data = array(
    'created_on'      => '2007-03-22',
    'bug_description' => 'Something wrong',
    'bug_status'      => 'NEW'
);

$table->insert($data);



7. Updating rows in a Table

$table = new Bugs();

$data = array(
    'updated_on'      => '2007-03-23',
    'bug_status'      => 'FIXED'
);

$where = $table->getAdapter()->quoteInto('bug_id = ?', 1234);

$table->update($data, $where);


8.Deleting Rows from a Table

$table = new Bugs();

$where = $table->getAdapter()->quoteInto('bug_id = ?', 1235);

$table->delete($where);


Retrieving specific columns


$table = new Bugs();

$select = $table->select();
$select->from($table, array('bug_id', 'bug_description'))
       ->where('bug_status = ?', 'NEW');

$rows = $table->fetchAll($select);







Retrieving expressions as columns


$table = new Bugs();

$select = $table->select();
$select->from($table,
              array('COUNT(reported_by) as `count`', 'reported_by'))
       ->where('bug_status = ?', 'NEW')
       ->group('reported_by');

$rows = $table->fetchAll($select);



                       (or)


Public function getUserDataById($id) {
$select = $this->_db->select()
->from($this->_name)
->where(‘id = ?’, $id);
$result = $this->getAdapter()->fetchAll($select);
return $result;
}
    or

$table = new My_Db_Table_Picture();
$select = $table->select()
->from($table, array('image'))
->where('gallery_id = '.$gallery_id)
->limit(1);
$rowset = $table->fetchAll($select)->toArray();







Using a lookup table to refine the results of fetchAll()

$table = new Bugs();

// retrieve with from part set, important when joining
$select = $table->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
       ->where('bug_status = ?', 'NEW')
       ->join('accounts', 'accounts.account_name = bugs.reported_by')
       ->where('accounts.account_name = ?', 'Bob');

$rows = $table->fetchAll($select);



Example of finding a single row by an expression


$table = new Bugs();

$select  = $table->select()->where('bug_status = ?', 'NEW')
                           ->order('bug_id');

$row = $table->fetchRow($select);

Tuesday, November 1, 2011

joins

SQL Joins:

SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.

Different SQL JOINs

Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables

SQL INNER JOIN :

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: INNER JOIN is the same as JOIN.


SQL INNER JOIN Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger








The "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Now we want to list all the persons with any orders.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:
LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678

The
INNER JOIN keyword return rows when there is at least one match in both
tables. If there are rows in "Persons" that do not have matches in
"Orders", those rows will NOT be listed.


SQL LEFT JOIN:

The
LEFT JOIN keyword returns all rows from the left table (table_name1),
even if there are no matches in the right table (table_name2).

SQL LEFT JOIN Syntax

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.


SQL LEFT JOIN Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Now we want to list all the persons and their orders - if any, from the tables above.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The result-set will look like this:
LastName FirstName OrderNo
Hansen Ola

Friday, February 4, 2011

ajax insert,update,delete

create action.php

<?php
 require('common/dbconnect.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
  function delete(){
    var xmlhttp;
    if(window.XMLHttpRequest){
      xmlhttp=new XMLHttpRequest();
    }
    else{
      xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
    }
    xmlhttp.open("GET","delete.php",true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
    {
     if(xmlhttp.readyState==4 && xmlhttp.status==200){
      document.getElementById("custdiv").innerHTML=xmlhttp.responseText;
    }
    }
  }
 
</script>
</head>

<body>
 <form action="" method="post">
 
   <div id="custdiv"></div>
   <input type="button" value="add" name="add" onclick="javascript:location.href='add.php'" />
    <table border="1" bordercolor="#3399FF">
    <tr bgcolor="#CCFFCC">
      <th> Sno </th>
      <th> Cust Name </th>
      <th> Cust Father </th>
      <th> Address </th>
      <th> Actions </th>
      <th></th>
     </tr>
    <?php
      $i=1;
      //$res=mysql_query("select custid,custname,custfather,address from customer") or die(mysql_error());
      $res=mysql_query("select * from customer");
        while($row=mysql_fetch_array($res)){
    ?>
     <tr>
      <td> <input type="checkbox" name="sno" value="<?php echo $i?>" /><?php echo $i?></td>
      <td>
     <?php echo $row['custname']; ?>
      </td>
      <td>
     <?php echo $row['custfather']; ?>
      </td>
      <td>
     <?php echo $row['address']; ?>
      </td>
      <td> <a href="update.php?cid=<?php echo $row['custid']; ?>">Update</a></td>
      <td><a href="delete.php?cid=<?php echo $row['custid']; ?>" >Delete</a></td>
     </tr>
     <?php $i++; } ?>
    </table>
 </form>
</body>
</html>

create add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">

 function addcust(){
   //alert("hai"); 
   str1=document.getElementById("cname").value;
   str2=document.getElementById("cfname").value;
   str3=document.getElementById("address").value;
   if(str1==""||str2==""||str3==""){
    alert("please add the fields");
    return false;
   }
  
   var xmlhttp;
    if(window.XMLHttpRequest){
      xmlhttp=new XMLHttpRequest();
    }
    else{
      xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
    }
    url="addcust.php?name="+str1+"&fname="+str2+"&addres="+str3;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
    {
     if(xmlhttp.readyState==4 && xmlhttp.status==200){
      document.getElementById("custdiv").innerHTML=xmlhttp.responseText;
    }
    }
  return false;
 }
</script>
</head>

<body>
   <div id="custdiv">
  <table border="1">
    <tr>
     <td> Customer Name </td>
     <td><input type="text" name="cname" id="cname" value="" /></td>
    </tr>
    <tr>
     <td> Customer Father Name </td>
     <td> <input type="text" name="cfname" id="cfname" value=""/></td>
    </tr>
    <tr>
     <td> Address </td>
     <td> <input type="text" name="address" id="address" value="" /></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="submit" onclick="addcust()" /></td>
    </tr>
    </table>
   </div>
</body>
</html>


addcust.php


<?php
  $name=$_GET["name"];
  $fname=$_GET["fname"];
  $address=$_GET["addres"];
  require("common/dbconnect.php");
  mysql_query("insert into customer(custname,custfather,address) values('$name','$fname','$address')") or die(mysql_error());
        header("Location:actions.php");
?>

update.php


<?php
    require("common/dbconnect.php");
        $cid=$_GET["cid"];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
 function update(){
   var xmlhttp;   
   cid=document.getElementById("cno").value;
   str1=document.getElementById("cname").value;
   str2=document.getElementById("cfname").value;
   str3=document.getElementById("address").value;
   //alert(str1);
   //alert(str2);
   //alert(str3);
   if(str1==""||str2==""||str3==""){
     alert("insert any value not empty");
     return false;
   }
   if(window.XMLHttpRequest){
     xmlhttp = new XMLHttpRequest();
   }
   else{
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   url = "edit.php?name="+str1+"&fname="+str2+"&addr="+str3+"&cno="+cid;
   xmlhttp.open("GET",url,true);
   xmlhttp.send(null);
   xmlhttp.onreadystatechange=function()
   {
     if(xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("cdiv").innerHTML=xmlhttp.responseText;
        //document.getElementById("cdiv").style.display="none";
     }
   }
  return false; 
}
</script>
</head>

<body>

 <?php
      $res=mysql_query("select * from customer where custid=$cid");
    $row=mysql_fetch_array($res);
  ?>
  <div id="cdiv">
  
   <input type="hidden" id="cno" name="cno" value="<?php echo $cid?>" />
     <table border='1'>
    <tr>
      <td>Customer Name</td>
      <td><input type="text" name="cname" id="cname" value="<?php echo $row['custname']?>" /></td>
    </tr>
    <tr>
      <td>Customer Father Name</td>
      <td><input type="text" name="cfname"  id="cfname" value="<?php echo $row['custfather']?>" /></td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="text" name="address"  id="address" value="<?php echo $row['address']?>"/></td>
    </tr>
    <tr>
      <td></td>
      <td><button type="submit" onclick="update()">update</button></td>
    </tr>
   </table>
  </div>
 </body>
</html>



edit.php


<?php
  $cid=$_GET["cno"];
  $name=$_GET["name"];
  $fname=$_GET["fname"];
  $addr=$_GET["addr"];
  require('common/dbconnect.php');
  mysql_query("update customer set custname='$name',custfather='$fname',address='$addr' where custid=$cid")or die("error".mysql_error());
  header("Location:actions.php");
?>

delete.php

<?php
  require("common/dbconnect.php");
  $cno=$_GET["cid"];
  mysql_query("delete from customer where custid=$cno");
  header("Location:actions.php");
?>


dbconnect.php

<?php
  $con=mysql_connect("localhost","root","") or die("fail to connect".mysql_error());
  mysql_select_db("base",$con) or die(mysql_error());
?>

                                                                

insert,delet,update

Regstration.php(Controller )


<?php
  class Registration extends Controller
  {
 
    function Registration()
      {
       parent::Controller();
       $this->load->database();
       $this->load->model('registration_model');
       $this->load->helper('url');
       }
      function index()
       {
      
        $data=array();
        $submit = $this->input->post('submit');
        if($submit == 'Insert')
           {
          
            $name=$this->input->post('name');
            $age =$this->input->post('age');
            $gender=$this->input->post('gender');
            $address=$this->input->post('address');
             $this->registration_model->insert_values($name,$age,$gender,$address);
         
            }
       
             $row=$this->registration_model->getusers();
             $data['query']=$row;
            $this->load->view('registration',$data);
         
        }
      function update($id=NULL)
      {
        $data=array();
   
          $submit=$this->input->post('submit');
           if($submit=='Update')
           {
            
             $name=$this->input->post('name');
             $age=$this->input->post('age');
             $gender=$this->input->post('gender');
             $address=$this->input->post('address');
                    
             $this->registration_model->update_values($name,$age,$gender,$address,$id);
               redirect('registration');
              }
           
             $row=$this->registration_model->getusers_byid($id);
             $data['query']=$row;
           
             $this->load->view('update_reg',$data);
           
     }
           function delete($id= NULL)
           {
             $this->registration_model->delete_values($id);
             $row=$this->registration_model->getusers();
             $data['query']=$row;

            $this->load->view('registration',$data);
           }
   
}
?>

Registration.php(Model)

<?php
class Registration_model extends Model
{
   function Registration_model()
     {
       parent::Model();
     }
      
    function insert_values($name,$age,$gender,$address)
    {
     $data=array(
                  'name'=>$name,
                  'age'=>$age,
                  'gender'=>$gender,
                  'address'=>$address );
     $query=$this->db->insert('user',$data);             

    }  
   
      function getusers()
        {
       $query = $this->db->get('user');
       return $query->result();
       }
       function getusers_byid($id)
        {
          $this->db->where('id',$id);
          $query = $this->db->get('user');
          return $query->row();
       }
      
     function update_values($name,$age,$gender,$address,$id)
     {
   
        $data=array('name'=>$name,
                    'age'=>$age,
                   'gender'=>$gender,
                   'address'=>$address);
            $this->db->where('id',$id);
            $this->db->update('user',$data);
           
   
           
     }
     function delete_values($id)
     {
        $this->db->where('id', $id);
        $this->db->delete('user');
   
     }

}


?>



Registration.php(view)

<html>
<title>
Registration</title>
<body>
<form action="<?php echo base_url()?>registration" method="post">
<table border="1">
<tr><td>Username:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Age:</td><td><input type="text" name="age" /></td></tr>
<tr><td>Gender:</td><td><input type="text" name="gender" /></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Insert" /></td>
</tr>
</table>
</form>

<table border="1">
<tr><td>NAME</td><td>AGE</td><td>GENDER</td><td>ADDRESS</td></tr>
<?php
$i="";
foreach($query as $value){?>
<form action="<?php echo base_url()?>registration" method="post">

<tr>
  <td><?php echo $value->name;?></td>
  <td><?php echo $value->age;?></td>
  <td><?php echo $value->gender;?></td>
  <td><?php echo $value->address;?></td>
  <td><a href="<?php echo base_url()?>registration/update/<?php echo $value->id;?>">Update</a></td>
  <td><a href="<?php echo base_url()?>registration/delete/<?php echo $value->id;?>">Delete</a></td> 
</tr>
  </form>
<?php
}?>

</table>

</body>
</html>















update.php(view)




<html>
<head>
</head>
<title>Update</title>
<body>
<form action="<?php echo base_url() ?>registration/update/<?php echo $query->id; ?>" method="post">

<table border="2" align="center">

<tr>
<td>NAME</td><td><input type="text" name="name" value="<?php echo $query->name; ?>" /></td></tr>
<tr><td>AGE</td><td><input type="text" name="age" value="<?php echo $query->age; ?>" /></td></tr>
<tr><td>GENDER</td><td><input type="text" name="gender" value="<?php echo $query->gender; ?>"/></td></tr>
<tr><td>ADDRESS</td><td><input type="text" name="address" value="<?php echo $query->address; ?>"/></td></tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Update" /></td>

</table>
</form>
</body>
</html>