Tuesday, June 2, 2015

Codeigniter Template

Codeigniter Template

Folder Structure
application/
     /controllers/
          /test_template.php
     /libraries/
          /Template.php
     /views/
          /template/
               /another_page_one.php
               /another_page_three.php
               /another_page_two.php
               /default_content.php
               /main_tmp.php
system/

./application/controllers/test_template.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test_template extends CI_Controller {

     public function __construct() {
          parent::__construct();
          $this->load->library('template');
     }

     public function index() {
          $this->template->set('title', 'My Title');
          $this->template->load('template/main_tmp', 'template/default_content');
     }

     public function another_page_one() {
          $this->template->set('title', 'Another Page One');
          $this->template->load('template/main_tmp', 'template/another_page_one');
     }

     public function another_page_two() {
          $this->template->set('title', 'Another Page Two');
          $this->template->load('template/main_tmp', 'template/another_page_two');
     }

     public function another_page_three() {
          $this->template->set('title', 'Another Page Three');
          $this->template->load('template/main_tmp', 'template/another_page_three');
     }

}

/* End of file template.php */
/* Location: ./application/controllers/template.php */

./application/libraries/Template.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Template {

     var $template_data = array();
     private $data = array();

     public function __construct() {
          $this->CI =& get_instance();
     }

     public function set($name, $value) {
          $this->template_data[$name] = $value;
     }

     public function load($template = '', $view = '', $view_data = array(), $return = FALSE) {
          $this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
          return $this->CI->load->view($template, $this->template_data, $return);
     }

}

/* End of file Template.php */
/* Location: ./application/libraries/Template.php */

./application/views/template/another_page_one.php
<h3>Another Page One</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

./application/views/template/another_page_three.php
<h3>Another Page Three</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

./application/views/template/another_page_two.php
<h3>Another Page Two</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

./application/views/template/default_content.php
<h3>Default Content</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

./application/views/template/main_tmp.php
<ul>
     <li><?=anchor('test_template', 'Home');?></li>
     <li><?=anchor('test_template/another_page_one', 'Another page 1');?></li>
     <li><?=anchor('test_template/another_page_two', 'Another page 2');?></li>
     <li><?=anchor('test_template/another_page_three', 'Another page 3');?></li>
</ul>
<?=$contents;?>

Categories: ,

PHP: Image Upload



Folder Structure
upload/
connect.php
index.php

tb_image.sql
CREATE TABLE IF NOT EXISTS `tb_image` (
     `ID_image` int(11) NOT NULL AUTO_INCREMENT,
     `file_name` varchar(100) NOT NULL,
     `file_size` varchar(100) NOT NULL,
     `url_path` varchar(100) NOT NULL,
     PRIMARY KEY (`ID_image`)
);

connect.php
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_php_series';

$db_conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db, $db_conn);

index.php
<?php
/* insert image form */
include('connect.php');

if (isset($_POST['upload'])) {
     $file_name = $_FILES['file']['name'];
     $file_size = $_FILES['file']['size'];
     $upload_path = 'upload/';
     $final_path = $upload_path.$file_name;

     if (move_uploaded_file($_FILES['file']['tmp_name'], $final_path)) {
          mysql_query("INSERT INTO `tb_image`(`file_name`, `file_size`, `url_path`) VALUES ('".$file_name."','".$file_size."','".$final_path."')");
          header("location:index.php");
     } else {
          echo 'general_system_error';
     }
}
?>

<form method="post" action="" enctype="multipart/form-data">
     <input type="file" name="file" />
     <input type="submit" name="upload" value="Upload" />
</form>
/* List view image after insert */
<table width="100%">
     <tr>
          <th></th>
          <th align="left">File Name</th>
          <th align="left">File Size</th>
          <th align="left">Full Path</th>
     </tr>
     <?php
     $query = mysql_query("SELECT * FROM `tb_image` order by ID_image desc");
     while ($data = mysql_fetch_array($query)) {
          echo '<tr>';
          echo '<td><img src="'.$data['url_path'].'" width="50" height="50" /></td>';
          echo '<td>'.$data['file_name'].'</td>';
          echo '<td>'.$data['file_size'].'</td>';
          echo '<td>'.$data['url_path'].'</td>';
          echo '</tr>';
     }
     ?>
</table>

Categories: ,

Codeigniter: Multiple Upload Image



Download assets here

tb_image.sql
CREATE TABLE IF NOT EXISTS `tb_image` (
     `ID_image` int(11) NOT NULL AUTO_INCREMENT,
     `file_name` varchar(50) NOT NULL,
     `file_type` varchar(50) NOT NULL,
     `file_path` varchar(50) NOT NULL,
     `full_path` varchar(100) NOT NULL,
     `raw_name` varchar(50) NOT NULL,
     `orig_name` varchar(50) NOT NULL,
     `client_name` varchar(50) NOT NULL,
     `file_ext` varchar(50) NOT NULL,
     `file_size` varchar(10) NOT NULL,
     `image_width` varchar(10) NOT NULL,
     `image_height` varchar(10) NOT NULL,
     `image_type` varchar(10) NOT NULL,
     `image_size_str` varchar(50) NOT NULL,
     PRIMARY KEY (`ID_image`)
);

Folder Structure
application/
css/
img/
js/
upload/
.htaccess
index.php

.htaccess
RewriteEngine On
RewriteBase /ci_series/

RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

RewriteRule ^\.html$ /home/read/?$ [L]

# Remove trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enfore NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

./application/config/config.php
$config['index_page'] = '';
./application/config/autoload.php
$autoload['libraries'] = array('database', 'form_validation');
$autoload['helper'] = array('url', 'html');

./application/controllers/multiple.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Multiple extends CI_Controller {

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

     public function index() {
          $config = array(
               'upload_path' => './upload/',
               'upload_url' => site_url().'upload/',
               'allowed_types' => 'jpg|gif|png',
               'overwrite' => false
          );
          $this->load->library('upload', $config);

          if ($this->upload->do_upload('file')) {
               $data = $this->upload->data();

               $insert_to_database = array(
                    'file_name' => $data['file_name'],
                    'file_type' => $data['file_type'],
                    'file_path' => $data['file_path'],
                    'full_path' => $data['full_path'],
                    'raw_name' => $data['raw_name'],
                    'orig_name' => $data['orig_name'],
                    'client_name' => $data['client_name'],
                    'file_ext' => $data['file_ext'],
                    'file_size' => $data['file_size'],
                    'image_width' => $data['image_width']
,                     'image_height' => $data['image_height'],
                    'image_type' => $data['image_type'],
                    'image_size_str' => $data['image_size_str']
               );
               $this->db->insert('tb_image', $insert_to_database);
               redirect('multiple');
          }

          $data['result'] = $this->db->get('tb_image')->result();

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

}

/* End of file multiple.php */
/* Location: ./application/controllers/multiple.php */

./application/views/multiple_upload.php
// between head tag insert this
<link rel="stylesheet" type="text/css" href="<?=site_url('css/dropzone.css');?>" />
<script type="text/javascript" src="<?=site_url('js/dropzone.js');?>"></script>
<form action="<?=site_url('multiple');?>" class="dropzone"></form> <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
     <tr>
          <td></td>
          <th align="left">File Name</th>
          <th align="left">File Type</th>
          <th align="left">Full Path</th>
          <th align="left">File Size</th>
     </tr>
     <?php
     foreach ($result as $data) {
          echo '<tr>';
          echo '<td>'.img(array('src' => 'upload/'.$data->file_name, 'width' => '80', 'height' => '50')).'</td>';
          echo '<td>'.$data->file_name.'</td>';
          echo '<td>'.$data->file_type.'</td>';
          echo '<td>'.$data->full_path.'</td>';
          echo '<td>'.$data->file_size.'</td>';
          echo '</tr>';
     }
     ?>
</table>

Categories: ,

Monday, June 1, 2015

PHP: simple CRUD (Create, Read, Update and Delete)



Folder Structure
add_item.php
connect.php
detail_item.php
edit_item.php
function.php
index.php

add_item.php
<?php
include('connect.php');
include('function.php');

if (isset($_POST['insert'])) {
     $title = $_POST['title'];
     $content = $_POST['content'];
     $date = date('Y-m-d H:i:s');
     $slug = strtolower(url_title($title));
     mysql_query("insert into tb_crud(title, content, date, slug) values('".$title."', '".$content."', '".$date."', '".$slug."')");
     header("location:index.php");
}
?>

<form method="post" action="">
     <table width="100%">
          <tr>
               <td>Title</td>
               <td><input type="text" name="title" size="60" /></td>
          </tr>
          <tr>
               <td valign="top">Content</td>
               <td><textarea type="text" style="width:500px;height:300px" name="content"></textarea></td>
          </tr>
          <tr>
               <td> </td>
               <td><input type="submit" value="Submit" name="insert" /></td>
          </tr>
     </table>
</form>

connect.php
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_php_series';

$db_conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db, $db_conn);

detail_item.php
<table width="100%" border="1">
     <tr>
          <th>Title</th>
          <th>Content</th>
          <th>Date</th>
     </tr>
     <?php
     include('connect.php');
     if (isset($_GET['slug'])) {
          $detail = mysql_fetch_array(mysql_query("SELECT * FROM `tb_crud` WHERE `slug` = '".$_GET['slug']."'"));
     }
     echo '<tr>';
     echo '<td>'.$detail['title'].'</td>';
     echo '<td>'.$detail['content'].'</td>';
     echo '<td>'.$detail['date'].'</td>';
     echo '</tr>';
     ?>
</table>

edit_item.php
<?php
include('connect.php');
include('function.php');

if (isset($_GET['edit'])) {
     $update = mysql_fetch_array(mysql_query("select * from tb_crud where ID_crud = '".$_GET['edit']."'"));
}

if (isset($_POST['update'])) {
     $title = $_POST['title'];
     $content = $_POST['content'];
     $date = date('Y-m-d H:i:s');
     $slug = strtolower(url_title($title));
     mysql_query("UPDATE `tb_crud` SET `title`='".$title."',`content`='".$content ."',`date`='".$date."',`slug`='".$slug."' WHERE ID_crud = '".$_GET['edit']."'");
     header("location:index.php");
}
?>

<form method="post" action="">
     <table width="100%">
          <tr>
               <td>Title</td>
               <td><input type="text" name="title" size="60" value="<?php echo $update['title'];?>" /></td>
          </tr>
          <tr>
               <td valign="top">Content</td>
               <td><textarea type="text" style="width:500px;height:300px" name="content"><?php echo $update['content'];?></textarea></td>
          </tr>
          <tr>
               <td> </td>
               <td><input type="submit" value="Submit" name="update" /></td>
          </tr>
     </table>
</form>

function.php
<?php
function url_title($str, $separator = '-', $lowercase = FALSE) {
     if ($separator == 'dash') {
          $separator = '-';
     } else if ($separator == 'underscore') {
          $separator = '_';
     }
     $q_separator = preg_quote($separator);

     $trans = array(
          '&.+?;' => '',
          '[^a-z0-9 _-]' => '',
          '\s+' => $separator,
          '('.$q_separator.')+' => $separator
     );

     $str = strip_tags($str);

     foreach ($trans as $key => $val) {
          $str = preg_replace("#".$key."#i", $val, $str);
     }

     if ($lowercase === TRUE) {
          $str = strtolower($str);
     }
     return trim($str, $separator);
}
?>

index.php
<table width="100%" border="1">
     <tr>
          <th>Title</th>
          <th>Content</th>
          <th>Date</th>
          <th>Slug</th>
          <th>Options</th>
     </tr>

     <?php
     include('connect.php');

     $query = mysql_query("select * from tb_crud order by date desc");
     while ($data = mysql_fetch_array($query)) {
          echo '<tr>';
          echo '<td><a href="detail_item.php?slug='.$data['slug'].'">'.$data['title'].'</a></td>';
          echo '<td>'.$data['content'].'</td>';
          echo '<td>'.$data['date'].'</td>';
          echo '<td>'.$data['slug'].'</td>';
          echo '<td><a href="edit_item.php?edit='.$data['ID_crud'].'">Edit</a> | <a href="index.php?delete='.$data['ID_crud'].'">Delete</a></td>';
          echo '</tr>';
     }

     // delete script
     if (isset($_GET['delete'])) {
          mysql_query("DELETE FROM `tb_crud` WHERE `ID_crud` = '".$_GET['delete']."'");
          header("location:index.php");
     }
     ?>
</table>
<br/>
<a href="add_item.php">Add Item</a>

Categories: ,

Saturday, May 30, 2015

Codeigniter: Encryption Dynamic URL



./application/config/autoload.php
$autoload['libraries'] = array('my_encryption');
./application/controllers/encryption.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Encryption extends CI_Controller {

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

     public function index() {
          $data['encode'] = $this->my_encryption->encode('my_dynamic_url');
          $data['decode'] = $this->my_encryption->decode('HoDTZBLPrchP3hG3XCmKNhSxQNOk4n8qOlKqAkgLdwM');

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

}

/* End of file encryption.php */
/* Location: ./application/controllers/encryption.php */

./application/libraries/MY_Encryption.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Encryption {

     var $skey = "KQYsG4Hi201ajyEzOSGzr4MVfw"; // you can change it

     public function safe_b64encode($string) {
          $data = base64_encode($string);
          $data = str_replace(array('+', '/', '='),array('-', '_', ''),$data);
          return $data;
     }

     public function safe_b64decode($string) {
          $data = str_replace(array('-', '_'),array('+', '/'),$string);
          $mod4 = strlen($data) % 4;
          if ($mod4) {
               $data .= substr('====', $mod4);
          }
          return base64_decode($data);
     }

     public function encode($value) {
          if (!$value) {
               return false;
          }
          $text = $value;
          $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
          $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
          $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);
          return trim($this->safe_b64encode($crypttext));
     }

     public function decode($value) {
          if (!$value) {
               return false;
          }
          $crypttext = $this->safe_b64decode($value);
          $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
          $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
          $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);
          return trim($decrypttext);
     }

}

/* End of file MY_Encryption.php */
/* Location: ./application/libraries/MY_Encryption.php */

./application/views/encryption.php
<?php echo $encode;?>
<br/>
<?php echo $decode;?>

Categories: ,

Codeigniter: Sending email with gmail smtp



./application/controllers/sending_email.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Sending_email extends CI_Controller {

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

     public function index() {
          $config = Array(
               'protocol' => 'smtp',
               'smtp_host' => 'ssl://smtp.googlemail.com',
               'smtp_port' => 465,
               'smtp_user' => 'example@gmail.com', // change it to yours
               'smtp_pass' => 'my_password', // change it to yours
               'mailtype' => 'html',
               'charset' => 'iso-8859-1',
               'wordwrap' => TRUE
          );
          $this->load->library('email', $config);

          $message = 'Enter your email message here';
          $this->email->set_newline("\r\n");
          $this->email->from('example.from@gmail.com'); // change it to yours
          $this->email->to('example.to@gmail.com'); // change it to email destination
          $this->email->subject('Subject');
          $this->email->message($message);
          $this->email->send();

          $data['result'] = $this->email->print_debugger();

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

}

/* End of file sending_email.php */
/* Location: ./application/controllers/sending_email.php */

./application/views/sending_email.php
<?php echo $result;?>

Categories: ,

Codeigniter: Upload/Save Image to Database and Watermark



Folder Structure
./application/
./system/
./upload/
./index.php

tb_image.sql
CREATE TABLE IF NOT EXISTS `tb_image` (
     `ID_image` int(11) NOT NULL AUTO_INCREMENT,
     `file_name` varchar(50) NOT NULL,
     `file_type` varchar(50) NOT NULL,
     `file_path` varchar(50) NOT NULL,
     `full_path` varchar(100) NOT NULL,
     `raw_name` varchar(50) NOT NULL,
     `orig_name` varchar(50) NOT NULL,
     `client_name` varchar(50) NOT NULL,
     `file_ext` varchar(50) NOT NULL,
     `file_size` varchar(10) NOT NULL,
     `image_width` varchar(10) NOT NULL,
     `image_height` varchar(10) NOT NULL,
     `image_type` varchar(10) NOT NULL,
     `image_size_str` varchar(50) NOT NULL,
     PRIMARY KEY (`ID_image`)
)

Categories: ,

luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com

Copyright © Click | Powered by Blogger