Monday, December 3, 2012

How to create login and logout buttons with sessions?

The piece of code simply create login and logout buttons with sessions in PHP.
<?php
$log=$this->session->userdata("email");
    
if(!$log)
{
       echo anchor('Login path', 'Login', 'title="login"');
}
else
{
         echo anchor('Logout path', 'Logout', 'title="logout"');
}
?>

How to create pagenation in Codeigniter?

 The follownig code clearly explains you how to cretae page nation in codeigniter.

Controller:
 Step1: declare two parameters to the function, where pagenation is required.
             public function function_name($start=1,$page='pagename')
 Step2: Pass the session variables to the view.
           $val['login']=$this->session->userdata('logged_in');
           $val['admin']=$this->session->userdata('user');
Step3: Intialize and check the start value.
             if(!is_numeric($start))
            {
                 $page = $start;
                 $start = 0;
             }
             else if($start == 1)
            {
                 $start = 0;
             }
Step4: Load the header
           $this->load->view('path to header');
Step5: Load the pagination library.
           $this->load->library('pagination'); 
Step6: Configured the pagination.
            $val["start"] = $start;
            $val["page_key"] = $page;
            $limit=20;
           $val['entry'] =$this->model->get($table,$limit,'sno',$start);
           $val["menu_tag"] = "function_name/1";
           $config['base_url'] = 'path';
            $config['total_rows'] = $this->model->total_rows($table);
           $config['per_page'] = 20;
          $this->pagination->initialize($config);
          $val["pagination"] = $this->pagination->create_links();
Step7: Load the view   
            $this->load->view('admin/pages/contact_info',$val);
Step8: we need to crete one more function in our controller for call current page
            public function page($currentpage)
           {
                  echo $currentpage;
                  echo $this->pagination->create_links();
             }
View:
Step9: The following two lines code added to your view where you want to put pagination.
              <?php $pagination = str_replace('">',"/$page_key\">",$pagination);?>
              <?php echo $pagination; ?>
Model:
Step10. write the following functions in database for fetch the data from database.
            function total_rows($table)
           {
               $query=$this->db->get($table);
               return  $query->num_rows();
            }
           function get($table,$limit,$order_by = "sno",$start = 0)
           {
              $query = $this->db->order_by($order_by, "asc");
               $query = $this->db->limit($limit);
                $query = $this->db->get($table);
                 return $query->result_array();
            }

 This is all about pagination.
Keep smiling......




            

How to give authenication to controllers in codeigniter?

Simply add the following bit code to constructor of your controller, which is needed authenication. 

CODE:

$session_id = $this->session->userdata('logged_in');
$this->session->userdata('email');
if($session_id == FALSE) {
redirect('Login Path');

How to cretae captcha in php code?

The following piece of PHP code simply create captcha.

 <?php
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$res = "";
for ($i = 0; $i < 7; $i++)
{
       $res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo  $res;
?>


Try this code and taste the PHP.

create facebook share to custom webpages?


There are mailly two steps to create share button in our site. They are
http://www.facebook.com/sharer.php?u=<url to share>
&t=<title of content>link or image</a>
  1. url to share – the site that you want to share to Facebook
  2. title of content – the name of what you are sharing
If I wanted to share my Facebook fan page with a custom image, I would use the following code.
<a href="http://www.facebook.com/sharer.php?u=http://facebook.com/
Anti.Social.Development&t=(Anti) Social Development" target="_blank">
<img src="http://www.kimwoodbridge.com/asdface.jpg" /></a>

You also check the following url:
http://www.kimwoodbridge.com/how-to-create-your-one-facebook-share-url/