Friday, February 5, 2016

Different language message error from Model Cakephp


That is just a small trick how how we can make our website to be multiple language. For the concept is not different from other
framework, by the way you have understand about folder structure of Cakephp.

In this case you will see the Local folder which its contain the two folder inside.

I have created two folder of language "en and jp".

1- Create language folder. ex. ('en and jp')



2- Create file with extension ".po"
3- There are two key word in the file which you have to understand :
    msgid : it is the key or id which represent the message as a global
    msgstr: it is the message of the key a head
4- Use in model. ex.
public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notBlank'),
                'message' => 'This field is required!'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notBlank'),
                'message' => 'This field is required!',
            ),
            'min_length' => array(
                'rule' => array('minLength', '5'),
                'message' => 'Password must over 5',
            )
        ),
);

5- check in the file .po syntax
    a- Example in en folder :

msgid "This field is required!"
msgstr "This field is required!"

msgid "Please enter the same value again!"
msgstr "Please enter the same value again!"

msgid "Password must over 5!"
msgstr "Password must over 5!"

msgid "Email already exist!"
msgstr "Email already exist!"

msgid "Please enter valid phone number!"
msgstr "Please enter valid phone number!"

msgid "This field must be number!"
msgstr "This field must be number!"

msgid "Invalid email format!"
msgstr "Invalid email format!"

   b- Example in jp folder :
msgid "This field is required!"
msgstr "必須項目です。"

msgid "Please enter the same value again!"
msgstr "必須項目です。"

msgid "Password must over 5!"
msgstr "パスワードは5文字以上を入力ください。"

msgid "Email already exist!"
msgstr "入力されたEメールは既に存在しています。"

msgid "Please enter valid phone number!"
msgstr "電話の形式ではありません。"

msgid "This field must be number!"
msgstr "数値ではありません。"

msgid "Invalid email format!"
msgstr "Eメールの形式ではありません。"

6- Please configure the language in AppController :

Configure::write('Config.language', 'jp or en');

Ok, that is. Let's enjoy.

Friday, January 22, 2016

Go to position with jQuery



This is just a small trick on how to go to the page section with Jquery by referenced from url. So the concept is not difficult first you need to do is to add the # which will be connected with your link (eg. www.url.com/page#id). After that you can use Jquery window.location.hash to get the "#id" from url. In this code I have used animate function to show the transition while the page is scrolled to the section.



ID


Text Description

$(function() {
        var ha = window.location.hash;
        if(ha) {
            var divLoc = $(ha).offset();
            $('html, body').animate({scrollTop: divLoc.top}, "slow");
        }
    })
 }
}



Tuesday, November 17, 2015

Call and use helper in controller Cakephp

Somebody said that there would have many ways to call Helper to use in view , but inspire I would show you how to implement it in Controller.php

class YourContrller extends Objects{
  pubic function yourfunction()
  {
  App::import('Helper', 'String'); 
  $string = new StringHelper(new View());
 
  $string->yourFunctionHelper();
 }
}


StringHelper.php

class StringHelper extends AppHelper{
  
   public function yourfunction()
   {
     .....
   }
} 

Saturday, November 14, 2015

Check and Remove Flash Session with Cakephp


Controller.php
 public function contact()
    {
        if($this->Session->check('Message.message_send_success')) {
            $this->Session->delete('Message.message_send_success');
        }
        if ($this->request->is('post')) {
            $author = $this->viewVars['get_author'];

            if (!$author) {
                return $this->Session->setFlash(
                        'Both fields are required!',
                        'default',
                        array('class' => 'redTxt'),
                        'message_send_error');
            } else {
                $sender_name = ucfirst($author[0]['User']['username']);
                $email       = $author[0]['User']['email'];
                $user_id     = $author[0]['User']['id'];
                $this->loadModel('User');
                    $title = $this->request->data['User']['title'];
                    $body  = $this->request->data['User']['body'];
                    if (!$title && !$body) {
                        return $this->Session->setFlash(
                            'Both fields are required!',
                            'default',
                            array('class' => 'redTxt'),
                            'message_send_error');
                    }
                    $Email = new CakeEmail();
                    $Email->from(
                        array($email => $title))
                        ->to($email)
                        ->subject($title)
                        ->emailFormat('html')
                        ->send('From '.h($sender_name).' '.h($body));
                    $this->Session->setFlash(
                        'The message has been send!',
                        'default',
                        array('class' => 'alert alert-success'),
                        'message_send_success');
                }
        }
        $this->set('title', 'Contact Us');
    }
}
view.php
Session->check('Message.message_send_success')): ?> 

THANK FOR YOUR CONTACT US!

your message has been send to us successfully.
Go back to home

Monday, November 9, 2015

Get properties from beforefilter in Cakephp to other function



That is just a trick which most of the developers are still wondering that is there a ways to get some properties from beforefilter in Cakephp to use in other function ? so here is the ways to do.

AppController.php
public function beforeFilter()
{
$user_id = ($this->Auth->user('id') != '' ? $this->Auth->user('id'): $author[0]['User']['id']);
$this->User->recursive = -1;
$get_author = $this->User->find('all',
     array(
        'conditions' => array('User.id' => $user_id)
     ));
$this->set('get_author', $get_author);
}
FavoritesController.php
public function get_vaforite(){
   pr( $this->viewVars['get_author']); 
}
Result :
Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 563c9401-445c-4d94-9151-5edb0a79370f
                    [username] => samphors bb
                    [password] => 3076ca74262cf933d1bbb4ee97474255f9aa8d9a
                    [facebook] => 
                    [email] => samphors.info@gmail.com
                    [address] => March dela deja
                    [mobile] => 1234567890
                    [birthdate] => 2015-11-10
                    [country] => Cambodia
                    [partner_country] => Italy
                    [height] => 6
                    [foot_size] => 3
                    [partner] => Dora vanndya
                    [partner_birthdate] => 2015-11-18
                    [partner_height] => 2
                    [partner_foot_size] => 5
                    [role] => 
                    [token] => 
                    [created] => 2015-11-06 20:50:25
                    [modified] => 2015-11-07 00:20:07
                )

        )

)

Monday, August 31, 2015

Replace Multiple String with PHP


Replace Multiple String In PHP
If you are working with PHP, then you would meet some problem with how to replace multiple string with a line of code. So there I will show you how to replace multiple string to make your url to be friendly.
In this case you have to know a PHP function which is call "str_replace", the below code will explain you how it work.

function replace_multiple_string($search=array(),$replace="",$string="")
{
  if(count($search) < 1)
  {
    return false;
  }
  else
  {
    return str_replace($search, $replace, $string);
  }
}

Explanation:
In the function above you would see the str_replace function which I've use to remove and replace the underscore instead of $replaces array.

 - str_replace : Replace all occurrences of the search string with the replacement string.

Usage : 

$string= "You are on_tip(and)php+website";
$search= array("_", "(", "+", ")"," ","+");

var_dump(replace_multiple_string($search,"-",$string));

Demo :

string(29) "You-are-on-tip-php-website" 

Saturday, August 29, 2015

Get Mac Address With PHP


get mac address with php

This post is to show you about Getting Mac Address or PC Address using PHP. This script will help you to display the Address of your computer. Based on this function I hope you will like it. Please take a look at live demo and to see your computer address.

Function (index.php)

function get_mac_address()
{
  ob_start(); 
  system('ipconfig /all'); 
  $mycom=ob_get_contents();
  ob_clean(); 
  $findme = "Physical";
  $pmac = strpos($mycom, $findme);

  return substr($mycom,($pmac+36),17);
}


Explanation

- ob_start() : Turn on output buffering
- system('ipconfig /all') : Get the ipconfig details using system commond.
- ob_get_contents() : Return the contents of the output buffer
- ob_clean() : clear or Erase output buffering.

Call the function (index.php)/ Demo

<?php get_mac_address(); ?>


I hope you will share if you think it is important.