วันอังคารที่ 20 พฤศจิกายน พ.ศ. 2555

การ Pagination ด้วย kohana 3.2


1.ติดตั้ง Module Pagination
- ดาน์วโหลด https://github.com/kloopko/kohana-pagination (Pagination for Kohana 3.2
- แตกไฟล์นำไปไว้ที่ http://www.yoursite.com/modules/pagination
2.เข้าไปแก้ไข application/bootstrap.php
Kohana::modules(array(
   'userguide'  => MODPATH.'userguide',
   'database'       => MODPATH.'database',   // Database access
   'pagination'   => MODPATH.'pagination',
  ));
3.ทำการสร้างไฟล์ Config (application/config/pagination.php)
<?php defined('SYSPATH') or die('No direct script access.');
  return array(
 // Application defaults
 'default' => array(
  'current_page'      => array('source' => 'query_string', 'key' => 'page'), // source: "query_string" or "route"
  'total_items'       => 3,
  'items_per_page'    => 1,
  'view'              => 'pagination/basic',
  'auto_hide'         => FALSE,
 ),
);
4. ตัวอย่างการนำไปใช้งานใน Controller
public function action_index() {
  
      // Define our template view and bind to variables
          $this->template->content = View::factory('article/index')
   ->bind('count', $count)
 ->bind('articles', $articles)
   ->bind('page_links', $page_links);
    
      // Get the total count of records in the database
   $count = DB::select(DB::expr('COUNT(*) AS mycount'))->from('articles')->execute()->get('mycount');
    
      // Create an instance of Pagination class and set values
   $pagination = Pagination::factory(array(
    'total_items'    => $count,
    'items_per_page' => 10,
 ));
 $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action() )); 
    
      // Load specific results for current page
   $articles = DB::select()->from('articles')
     ->order_by('id','ASC')
     ->limit($pagination->items_per_page)
     ->offset($pagination->offset)->execute();
              
      // Render the pagination links
   $page_links = $pagination->render();
    }
    
5.ตัวอย่างไฟล์ View

<?php defined('SYSPATH') or die('No direct script access.'); ?>
 <h1>Kohana Blog Homepage</h1>
 <?php echo HTML::anchor("article/new", "New Article"); ?>
<?php foreach ($articles as $article) : ?>
<div class="article">
<div class="title"><?php echo HTML::anchor("article/view/".$article['id'], $article['title']); ?> (<?php echo  DB::select()->from('comments')->where('article_id', '=', $article['id'])->execute()->count();?>)</div>
<pre><?php echo $article['content']; ?></pre>
<?php echo HTML::anchor("article/edit/".$article['id'], "Edit"); ?>
<?php echo HTML::anchor("article/delete/".$article['id'], "Delete"); ?>
</div>
<?php endforeach; ?>
<?php echo $count; ?>
<?php echo $page_links; ?>

วันอาทิตย์ที่ 18 พฤศจิกายน พ.ศ. 2555

การใช้ Validation ในการตรวจสอบการโพตส์ข้อมูล

1. สร้างตารางชื่อ users

CREATE TABLE `users` (

  `id` int(11) unsigned NOT NULL auto_increment,

  `email` varchar(254) NOT NULL,

  `username` varchar(32) NOT NULL default '',

  `password` varchar(64) NOT NULL,

  PRIMARY KEY  (`id`),

  UNIQUE KEY `uniq_username` (`username`),

  UNIQUE KEY `uniq_email` (`email`)

);


2.สร้างไฟล์ application/views/user/register.php

<?php defined('SYSPATH') or die('No direct script access.'); ?>



<?php echo Form::open() ?>

<?php if ($errors): ?>

<p class="message">Some errors were encountered, please check the details you entered.</p>

<ul class="errors">

<?php foreach ($errors as $message): ?>

    <li><?php echo $message ?></li>

<?php endforeach ?>

<?php endif ?>



<dl>

    <dt><?php echo Form::label('username', 'Username') ?></dt>

    <dd><?php echo Form::input('username', $post['username']) ?></dd>



    <dt><?php echo Form::label('password', 'Password') ?></dt>

    <dd><?php echo Form::password('password') ?></dd>

    <dd class="help">Passwords must be at least 6 characters long.</dd>

    <dt><?php echo Form::label('confirm', 'Confirm Password') ?></dt>

    <dd><?php echo Form::password('confirm') ?></dd>



    <dt><?php echo Form::label('email', 'Email') ?></dt>

    <dd><?php echo Form::input('email', $post['email']) ?></dd>

</dl>



<?php echo Form::submit(NULL, 'Sign Up') ?>

<?php echo Form::close() ?>


3.สร้างไฟล์ application/classes/controller/user.php


<?php defined('SYSPATH') or die('No direct script access.');



class Controller_User extends Controller_Template {

 public $template = 'template';

 // nothing here

 public function action_index()

 {

  $this->request->redirect('');

 }



    public function action_register()

    {

        $user = ORM::factory('user');



    if (isset($_POST) && Valid::not_empty($_POST)) {



    $post = Validation::factory($_POST)

    ->rule('username', 'not_empty')

    ->rule('username', 'regex', array(':value', '/^[a-z_.]++$/iD'))

    ->rule('username', array($user, 'unique_username'))



    ->rule('password', 'not_empty')

    ->rule('password', 'min_length', array(':value', 6))

    ->rule('confirm',  'matches', array(':validation', ':field', 'password'))



    ->rule('email', 'not_empty');



   if ($post->check())

   {

    $user->values(array(

     'email'  => $post['email'],

     'username'  => HTML::entities(strip_tags($post['username'])),

     'password' => $post['password'],

    ));

    $user->save();

   }



        // Validation failed, collect the errors

   $errors = $post->errors('user');

 }

        // Display the registration form



    // display

  $this->template->content = View::factory('user/register')

  ->bind('post', $post)

  ->bind('errors', $errors);

    }



}


4.สร้างไฟล์ application/classes/model/user.php

<?php defined('SYSPATH') or die('No direct script access.');

class Model_User extends ORM {



public static function unique_username($username)

{

    // Check if the username already exists in the database

    return ! DB::select(array(DB::expr('COUNT(username)'), 'total'))

        ->from('users')

        ->where('username', '=', $username)

        ->execute()

        ->get('total');

}

}

5.สร้างไฟล์ application/messages/user.php
เป็นการกำหนดข้อความ Error เอง โดยไฟล์นี้ถูกเรียกใช้จาก application/classes/controller/user.php ในโค้ด $errors = $post->errors('user');


<?php defined('SYSPATH') or die('No direct script access.');

return array(

    'username' => array(

        'unique_username' => 'This username already exists in the database'

    )

);

หมายเหตุ ถ้าเปลี่ยน $errors = $post->errors('user/validation'); เราก็ต้องสร้างไฟล์ application/messages/user/validation.php แทนครับ

วันจันทร์ที่ 12 พฤศจิกายน พ.ศ. 2555

วิธีติดตั้ง extension mcrypt บน windows xp


  1. แก้ไขไฟล์ php.ini ใน C:\WINDOWS โดยเปิดการทำงาน extension=php_mcrypt.dll 
  2. ใน copy ไฟล์ C:\AppServ\php5\libmcrypt.dll ไปยัง C:\WINDOWS
  3. จากนั้นให้ restart apache

วันจันทร์ที่ 5 พฤศจิกายน พ.ศ. 2555

สวัสดีเพื่อนๆ ท่านผู้แวะเยี่ยมชม เว็บไซต์ของผม

        ก่อนอื่นผมต้องขอขอบคุณน้องคนหนึ่งที่ทำให้ผมได้รู้จัก Kohana ซึ่งก่อนนี้ผมก็เป็นคนหนึ่งที่ใช้ php ในการพัฒนาเว็บไซต์  กว่าจะใช้ Kohana เป็นก็ใช้เวลาพอสมควร(ใจรักก็ลองไปเรื่อย)
     
        หลังจากนี้เอาไว้ผมมีเวลาผมจะลงบทความไปเรื่อยๆนะครับ ตอนนี้ขอไปเลี้ยงลูกก่อน ^ ^


ระหว่างรอผมอยู่ไปดูที่นี้ก่อนครับ http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/

http://kerkness.ca/kowiki/doku.php