This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Saturday, March 5, 2011

Building a Simple Blogger with the Code Igniter PHP Framework

In case you haven't heard about it, Code Igniter is a powerful, comprehensive framework written in PHP 4 that provides developers with a robust set of classes and helpers that can be used for building different web applications by means of the Model-View-Controller pattern, which separates application logic from visual presentation.

Thus, in this series of articles I'm going to explain how to build a basic blogger with Code Igniter. I will explain it in a step-by-step fashion to allow you to get an excellent grounding in its main features, as well as learn how to implement the MVC approach in a real-world situation.

This is a sample blog with Code Igniter you can download it here.

Wednesday, February 23, 2011

create and configure htaccess for codeigniter

  1. Create .htaccess file on index.php.
    RewriteEngine On
    RewriteBase /sitename
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    
    php_flag display_errors on
  2. Enable rewrite_module on apache server.
  3. Enable include_module on apache server.
  4. Enable cgi_module on apache server.
  5. Enable short open tag on php server.
  6. Configure config.php become.
    $config['index_page'] = "";

Friday, January 28, 2011

Make Auto Readmore on Blogspot

  1. Edit HTML and find </head>.
  2. Copy-paste and put this script bellow in above code script </head>, and continuous "Make Auto Read More Functions".
  3. <script type='text/javascript'>
    var thumbnail_mode = "float" ;
    
    summary_noimg = 250;
    summary_img = 250;
    img_thumb_height = 120;
    img_thumb_width = 120;
    </script>
    <script type='text/javascript'>
    
    //<![CDATA[
    
    function removeHtmlTag(strx,chop){
    if(strx.indexOf("<")!=-1)
    {
    var s = strx.split("<");
    for(var i=0;i<s.length;i++){
    if(s[i].indexOf(">")!=-1){
    s[i] = s[i].substring(s[i].indexOf(">")+1,s[i].length);
    }
    }
    strx = s.join("");
    }
    chop = (chop < strx.length-1) ? chop : strx.length-2;
    while(strx.charAt(chop-1)!=' ' && strx.indexOf(' ',chop)!=-1) chop++;
    strx = strx.substring(0,chop-1);
    return strx+'...';
    }
    function createSummaryAndThumb(pID){
    var div = document.getElementById(pID);
    var imgtag = "";
    var img = div.getElementsByTagName("img");
    var summ = summary_noimg;
    if(img.length>=1) {
    imgtag = '<span style="float:left; padding:0px 10px 5px 0px;"><img src="'+img[0].src+'" width="'+img_thumb_width+'px" height="'+img_thumb_height+'px"/></span>';
    summ = summary_img;
    }
    var summary = imgtag + '<div>' + removeHtmlTag(div.innerHTML,summ) + '</div>';
    div.innerHTML = summary;
    }
    //]]>
    </script>
  4. On EDIT HTML, check "Expand widget template" and found this script code bellow
  5. <data:post.body/>
  6. If you have been found <data:post.body>, copy-paste this script code bellow:
  7. <b:if cond='data:blog.pageType != "item"'>
    <div expr:id='"summary" + data:post.id'><data:post.body/></div>
    <script type='text/javascript'>createSummaryAndThumb("summary<data:post.id/>");</script>
    <span class='rmlink' style='float:left'><a expr:href='data:post.url' expr:title='data:post.title'>Read More »»»</a></span>
    </b:if>
    <b:if cond='data:blog.pageType == &quot;item&quot;'><data:post.body/></b:if>
    
  8. Save your template and see the result.

Create Authentication Login On Code Igniter With Extends Controller

  1. Create MY_Controller.php on /system/apllication/libraries/.
  2. <?php
    class MY_Controller extends Controller {
    
        function  My_Controller() {
            parent::Controller();
            $this->is_logged_in();
        }
    
        private function is_logged_in() {
            $this->ci =& get_instance();
            $this->ci->load->library('session');
            if(! $this->ci->session->userdata('login')) {
                redirect('login');
            }
        }
    
    }
    ?>
  3. Login Controller login.php
  4. <?
    
    /**
     * Login Class
     *
     *
     */
    class Login extends Controller {
     /**
      * Constructor
      */
     function login()
     {
      parent::Controller();
      $this->load->model('Login_model', '', TRUE);
     }
    
     /**
      * Check User Status, if login status it will appear in main page,
      * If Not it'll redirect in login page
      */
     function index()
     {
      if ($this->session->userdata('login') == TRUE)
      {
       redirect('home');
      }
      else
      {
       $this->load->view('login/login_view');
      }
     }
    
     /**
      * Login
      */
     function process_login()
     {
    
      $this->form_validation->set_rules('username', 'Username', 'required');
      $this->form_validation->set_rules('password', 'Password', 'required');
    
      if ($this->form_validation->run() == TRUE)
      {
       $username = $this->input->post('username');
       $password = $this->input->post('password');
    
       if ($this->Login_model->check_user($username, $password) == TRUE)
       {
        $row = $this->Login_model->data_user($username);
        $data = array('jbname' => $username, 'login' => TRUE, 'jbnama' => $row['nama'], 'jbid' => $row['id_user']);
        $this->session->set_userdata($data);
        redirect('home/');
       }
       else
       {
        $this->session->set_flashdata('message', 'Sorry, username and password is wrong');
        redirect('login/index');
       }
      }
      else
      {
       $this->load->view('login/login_view');
      }
     }
    
     /**
      * logout
      */
     function process_logout()
     {
      $this->session->sess_destroy();
      redirect('login', 'refresh');
     }
    }
    
  5. Make Model Login_model.php
  6. <?php
    /**
     * Login_model Class
     *
     *
     */
    class Login_model extends Model {
     /**
      * Constructor
      */
     function Login_model()
     {
      parent::Model();
     }
    
     // Inisialisation user table name
     var $table = 'user';
    
     /**
      * Check user table
      */
     function check_user($username, $password)
     {
      $password = md5($password);
      $query = $this->db->get_where($this->table, array('username' => $username, 'pass' => $password, 'aktif' => 1,
      'nm_module' => 'akd'), 1, 0);
    
      if ($query->num_rows() > 0)
      {
       return TRUE;
      }
      else
      {
       return FALSE;
      }
     }
    
     function data_user($username){
      $sql = "select id_user, nama from user where username = '$username' and nm_module = 'akd' ";
      $query = $this->db->query($sql);
      $row = $query->row_array();
      return $row;
     }
    
    }
    // END Login_model Class
    
    /* End of file login_model.php */
    /* Location: ./system/application/model/login_model.php */
    

Simple General Helper on Code Igniter

  1. Create general_helper.php on /sytem/application/helpers.
  2. <?php
    
    /**
     * change date format from dd/mm/yyyy become yyyy-mm-dd
     * @param <type> $date
     * @return string
     */
    function encode_date($date) {
        list($day, $month, $year) = explode('/', $date);
        $new_date = $year . "-" . $month . "-" . $day;
        return $new_date;
    }
    
    /**
     * change date format from yyyy-mm-dd become dd/mm/yyyy
     * @param <type> $date
     * @return string
     */
    function decode_date($date) {
        list($year, $month, $day) = explode('-', $date);
        $new_date = $day . "/" . $month . "/" . $year;
        return $new_date;
    }
    
    /**
     * create money value in Rupiah
     * @param <type> $uang
     * @return <type>
     */
    function format_rupiah($uang) {
        return "Rp. ". number_format($uang, 0, ",", ".");
    }
    
    function format_dana($uang) {
        return number_format($uang, 0, ".", "");
    }
            
    function indonesian_month($month) {
        switch ($month) {
            case "01" : $imonth = "Januari"; break;
            case "02" : $imonth = "Februari"; break;
            case "03" : $imonth = "Maret"; break;
            case "04" : $imonth = "April"; break;
            case "05" : $imonth = "Mei"; break;
            case "06" : $imonth = "Juni"; break;
            case "07" : $imonth = "Juli"; break;
            case "08" : $imonth = "Agustus"; break;
            case "09" : $imonth = "September"; break;
            case "10" : $imonth = "Oktober"; break;
            case "11" : $imonth = "November"; break;
            case "12" : $imonth = "Desember"; break;
            default  : $imonth = "-"; break;
        }
        return $imonth;
    }
    
    /**
     * change date format to indonesia
     * @param <type> $month
     * @return string
     */
    function indonesian_date($date) {
        list($year, $month, $day) = explode('-', $date);
        $new_date = $day ." ". indonesian_month($month) . " ". $year;
        return $new_date;
    }
    
    function indonesian_yearmonth($yearmonth) {
        list($month,$year) = explode(' ', $yearmonth);
        $new_yearmonth =  indonesian_month($month) . " ". $year;
        return $new_yearmonth;
    }
    
    function bulan_hari($day) {
        $tahun = floor($day/365);
        $bulan=floor(($day - ($tahun * 365))/30);
        $hari=$day - $bulan * 30;
        if($day=="30"){return $bulan." Bulan ";}
        elseif($day >= "30"){return $bulan." Bulan, ".$hari." Hari";}
        else{return $hari." Hari";}
    
    }
    ?>
  3. Controller script
  4. <?php
    
    /**
     * @property CI_DB_active_record $db
     */
    class tryhelper extends MY_Controller {
    
        function cobapdf() {
            parent::MY_Controller();
      $this->load->helper('general_helper');
        }
    
        function index() {
            echo indonesia_month('01');
        }
    }
    ?>
    

Exsport to PDF with dompdf in Code Igniter

  1. cDownload dompf from here and exstract the file.
  2. Then put to the directory ./system/application/plugins/.
  3. After that then create new file on ./system/application/plugins/dompdf_pi.php like this
  4. <?php
    
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    /**
     * Try increasing memory available, mostly for PDF generation
     */
    ini_set("memory_limit", "32M");
    
    function pdf_create($html, $filename, $stream=TRUE, $orientation="portrait") {
        require_once(BASEPATH . "application/plugins/dompdf/dompdf_config.inc.php");
    
        $dompdf = new DOMPDF();
        $dompdf->set_paper("a4", $orientation);
        $dompdf->load_html($html);
        $dompdf->render();
        if ($stream) { //open only
            $dompdf->stream($filename . ".pdf");
        } else { // save to file only, your going to load the file helper for this one
            write_file("pdf/$filename.pdf", $dompdf->output());
        }
    }
    
    ?>
  5. Create controler mypdf.php
  6. <?php
    
    /**
     * @property CI_DB_active_record $db
     */
    class mypdf extends MY_Controller {
    
        function mypdf() {
            parent::MY_Controller();
      $this->load->plugins('dompdf_pi');
        }
    
        function index() {
         $html="Hello Word";
      $filename="Mypdf";
            pdf_create($html, $filename, $stream=TRUE, $orientation='portrait');
        }
    
     }
    ?>

Count day, month and year from two date

$tgl1 = "2009-10-01";  // 1 October 2009
$tgl2 = "2009-11-10";  // 10 October 2009

// break dates to get the date, month and year
// from the first date

$pecah1 = explode("-", $tgl1);
$date1 = $pecah1[2];
$month1 = $pecah1[1];
$year1 = $pecah1[0];

// break dates to get the date, month and year
// from the second date

$pecah2 = explode("-", $tgl2);
$date2 = $pecah2[2];
$month2 = $pecah2[1];
$year2 =  $pecah2[0];

// count JDN from each of the dates

$jd1 = GregorianToJD($month1, $date1, $year1);
$jd2 = GregorianToJD($month2, $date2, $year2);

// compute difference the second day date

$selisih = $jd2 - $jd1;
$month=floor(($selisih - ($tahun * 365))/30);
$day=$selisih - $bulan * 30;

echo "difference the both of the day date is ".$month." month ".$day." day";
This Source is originaly from www.ms-room.com

Simple Script Exsport Excel With PHP

First example
<?
header("Content-Type: application/vnd.ms-excel");
echo "<table border=1>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>";
?>

Implementation
$header = "Header1" . "\t";
$header .= "Header2" . "\t";

//Reading the data thro' POST
$data= $_POST['header1']. "\t";
$data .= $_POST['header2'] . "\t";

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=xyz.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";