Thursday 26 July 2012

Thursday 19 July 2012

facebook login logout with codeigniter

Facebook login with our websites using codeigniter

It should present your controller: initially you should include the two files like facebook.php and base_facebook.php you can get this two libraries from following website..

and

Controller:

controller name: home.php 


<?php

class Home extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
     // $this->load->library('facebook');
        $fb_config = array(
            'appId'  => '265991286842551',
            'secret' => '****************************'
        );

        $this->load->library('facebook', $fb_config);

        $user = $this->facebook->getUser();

        if ($user) {
            try {
                $data['user_profile'] = $this->facebook->api('/me');
                
            } catch (FacebookApiException $e) {
                $user = null;
            }
            $this->load->model('home_model');
        $this->home_model->insert();
        }

        if ($user) {
            $data['logout_url'] = $this->facebook->getLogoutUrl();
        } else {
            $data['login_url'] = $this->facebook->getLoginUrl();
        }
        

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

}


model:

home_model.php

<?php
class Home_model extends CI_Model {
function insert(){
$user_profile = $this->facebook->api('/me');
$fbid=$user_profile['id'];
 
$this->db->select('fbid');
$this->db->where('fbid', $fbid);
$query=$this->db->get('user');
$num=$query->num_rows();
if($num){
return false;
}
else{
$insert1=array (
    'first_name' => $user_profile['first_name'],
    'last_name' => $user_profile['last_name'],
'fbemail' => $user_profile['username'],
    'current_location'  => $user_profile['location']['name'],
'fbid' => $user_profile['id']
    );
    $this->db->insert('user' ,$insert1);
    return true;
}
}
}

view :

<html>
<head>
    <title>Facebook Sweetness</title>
</head>
<body>
    <h1>Facebook stuff</h1>

    <?php  if (@$user_profile): ?>
    
   
    <?php  $user_profile['name'];?>
        <pre>
            <?php // echo print_r($user_profile, TRUE) ?>
        </pre>
        
        <a href="<?php echo $logout_url ?>">Logout of this thing</a>
    <?php else: ?>
        <h2>Welcome to this facebook thing, please login below</h2>
        <a href="<?php echo $login_url ?>">Login to this thing</a>
    <?php endif; ?>

</body>

</html>

For logout url:

$logoutUrl = $facebook->getLogoutUrl(array(
  'next' => 'Your Redirect URL after logout', // URL to which to redirect the user after logging out
  ));

For login url:

if($user){
 // Get logout URL
 $logoutUrl = $facebook->getLogoutUrl();
}else{
 // Get login URL
 $loginUrl = $facebook->getLoginUrl(array(
  'scope' => 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
  ));
}

from here we can get userbirthday and location user work history hometown , photos everything...

For getting profile picture:


echo "</br><img src='https://graph.facebook.com/$user/picture?type=large'/>";

For getting email:

$loginUrl = $facebook->getLoginUrl(array(
  'scope' => 'email'
  ));

Friday 13 July 2012

Template parser in codeigniter

Variable Pairs




$this->load->library('parser');

$data = array(
              'blog_title'   => 'My Blog Title',
              'blog_heading' => 'My Blog Heading',
              'blog_entries' => array(
                                      array('title' => 'Title 1', 'body' => 'Body 1'),
                                      array('title' => 'Title 2', 'body' => 'Body 2'),
                                      array('title' => 'Title 3', 'body' => 'Body 3'),
                                      array('title' => 'Title 4', 'body' => 'Body 4'),
                                      array('title' => 'Title 5', 'body' => 'Body 5')
                                      )
            );

$this->parser->parse('blog_template', $data);


http://codeigniter.com/user_guide/libraries/parser.html

   blog_template view

<html>
<head>
<title>{blog_title}</title>
</head>
<body>

<h3>{blog_heading}</h3>

{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
{/blog_entries}
</body>
</html>

Thursday 12 July 2012

codeigniter pagination with search

This is the    simple way to create the search function using pagination in codeigniter. it should be helpfull to those who need the search with pagination..

Controller




function search_category()
{
$post = $this->input->post('search');
if(!$post){
$post=$this->uri->segment(3);
}

        $this->session->set_userdata('ser', $post);
$this->load->model('category_model');
$per_page = 2;
    $total = $this->category_model->search_query_count();
    $data['categoryList'] = $this->category_model->search_query($per_page, $this->uri->segment(4));
  $this->load->library('pagination');
  $base_url = base_url().'category/search_category/'.$post;
    $config['base_url'] = $base_url;
    $config['total_rows'] = $total;
    $config['per_page'] = $per_page;
    $config['uri_segment'] = '4';
$this->pagination->initialize($config);
$data['inactive'] = $this->category_model->inactive_category();
$data['active'] = $this->category_model->active_category();
$data['main_content']='category_list';

$this->load->view('includes/template', $data);
}


Model
function search_query_count()
{
$post = $this->session->userdata('ser');
echo $post;
$query  = $this->db->query('select count(id) as count from category where name LIKE "%'.$post .'%" or description like "%' . $post .'%"');
   foreach($query->result() as $row)
   {
  return $row->count;
   }
}
function search_query($limit = NULL, $offset = NULL)
{
$post = $this->session->userdata('ser');
echo $post;
if(!$offset)
{
$offset = 0;
}
echo $offset;
$this->db->select('*');
$this->db->from('category');
$this->db->like('name', $post);
       $this->db->or_like('description', $post);
       $this->db->order_by("id", "desc");
       $this->db->limit($limit, $offset);
       $query = $this->db->get();
       return $query->result();
}