Please wait for 15 seconds...

Thursday 26 July 2012



Sometimes you need to have some data in every HTML page, like user is logged in or not, some header info or anything necessary for user interface.

Suppose you have facebook like, login buttons or Twitter follow us button at many places in your site, then you have to pass APP_KEY to every page or hardcode it in every HTML page. But you can have third way. Just pass it once in MY_Controller and echo wherever you want.

Create My_Controller in application/core. You can also change prefix(MY_) to anything you want.


<?php
class MY_Controller extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $uiData['fb_app_id'] = 1125225022520202;
        $this->load->view('home.htm', $uiData, true);
    }
}

*Here third parameter must be true else everything will be messed up.

Now extend MY_Controller and create custom class as you want into controller folder

class Home extends My_Controller{
        public function index(){
                $this->load->view('home.htm');
        }
}

Now echo Fabebook APP_ID into html page:

<html>
<head>
<title>Main Home</title>

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=<?=$fb_app_id?>";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

</head>
<body>
     <div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
</body>
</html>

and that is it. If you have any question please post your comment below.

Posted by Atul

0 comments:

Post a Comment

Techsirius on Facebook