月別アーカイブ: 2013年2月

Smarty インストール

ここではSmartyのインストールを解説していきます。

smarty

ダウンロードはこちら
http://www.smarty.net/download

設置

Smarty/libsフォルダをドキュメントルート直下に設置します。
cache , templates , templates_c フォルダをプロジェクト内に作成します。
(例ではSmartyフォルダにlibsの中身を置いています)

パスを通す

<?PHP
  
    // 読み込み
    require_once('Smarty/Smarty.class.php');
    // オブジェクト作成
    $smarty = new Smarty();
    // ディレクトリ設定
    $smarty->template_dir = 'smarty/templates';
    $smarty->compile_dir = 'smarty/templates_c';
    $smarty->cache_dir = 'smarty/cache';
  
    // 表示データをアサイン
    $smarty->assign('hoge', 'Hello world');
  
    // テンプレート表示
    $smarty->display('index.tpl');

Smarty.class.phpを読み込み、オブジェクトを生成後、cache , templates , templates_c フォルダのパスを通します。

アサインした変数の利用法

<!DOCTYPE html>
<html lang="ja">
<head>
   
<meta charset="UTF-8">
<meta content="86400" http-equiv="Expires"/>
<!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>Smarty</title>
</head>
   
<body>
{$hoge}
</body>
   
</html>

ファイルの拡張子は.tplです。
{$hoge}でアサインした変数が使用できHello worldと出力されます。

※注意
cache , templates_cにはキャッシュが書き込まれるので、書き込み権限が必要になります。
私はレンタルサーバーではパーミッションを777にしています。

jQuery 文字にタイピングアニメーショーンをつける jaticker

ここでは文字にタイピングアニメーショーンをつける jquery.jaticker.jsを紹介していきます。

デモはこちら
https://office-goto.info/demo/jaticker/

jatickerのダウンロード
http://www.otchy.net/javascript/jaticker/
otchy.net様

head内で読み込み

<link rel="stylesheet" type="text/css" href="jquery.jaticker_1.0.0.css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery.jaticker_1.0.0.js"></script>

jquery.jaticker_1.0.0.css、jquery.jaticker_1.0.0.jsを読み込んでください。

script

$(function(){
    $('#jaticker').jaticker();
});

ターゲット指定してください。

html

<div id="jaticker">
    <ruby>緑の<rt>みどりの</rt></ruby><ruby>カバンに<rt>かばんに</rt></ruby><ruby>500万<rt>500まん</rt></ruby><ruby>入れて<rt>いれて</rt></ruby>
    <ruby>白の紙で<rt>しろのかみで</rt></ruby><ruby>黄色のカバン<rt>きいろのかばん</rt></ruby><ruby>言うて書いて<rt>いうてかいて</rt></ruby>
    <ruby>赤のカバン<rt>あかのかばん</rt></ruby><ruby>言いながら<rt>いいながら</rt></ruby><ruby>置いてくれたら<rt>おいてくれたら</rt></ruby>
    <ruby>俺黒のカバン<rt>おれくろのかばん</rt></ruby><ruby>言いながら<rt>いいながら</rt></ruby><ruby>取りに行くわ<rt>とりにいくわ</rt></ruby>
</div>

ルビを振ると変換している様なアニメーションがつきます。

カスタマイズ

jquery.jaticker_1.0.0.js (77~85行目)

opt: {
    'inputSpeed': 60,       // アニメーション速度
    'convertSpeed': 120,    // 変換アニメーション速度
    'autoStart': true,      // 自動的に開始するか
    'cursorStr': '|',       // カーソルの文字列
    'cursorInterval': 500,  // カーソルの点滅速度
    'hideCursor': false,    // カーソルを隠すか
    'leaveCursor': false    // アニメーション完了後カーソルを消すか
}

お好みにアニメーションをカスタマイズできます。

CakePHP2.x View

ここではCakePHP2.x Viewの簡単な解説をしていきます。

cakephp

Layoutsサンプルソース

/app/View/Layouts/siteFrame.ctp

<!DOCTYPE html>
<html lang="ja">
<head>
  
<meta charset="UTF-8">
<meta content="86400" http-equiv="Expires"/>
<!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
  
<!-- ▼ページ固有のCSS読み込み -->
<?php echo $this->Html->css($arrCss, 'stylesheet', array('media'=>'screen'));?>
<!-- ▲END -->
  
<!-- ▼ページ固有のJavascript読み込み -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<?php echo $this->Html->script($arrJavascript);?>
<!-- ▲END -->
<title><?php echo $title_for_layout;?></title>
</head>
  
<body>
<?php echo $this->element('header');?>
<?php echo $this->element('menu');?>
<?php echo $content_for_layout;?>
<?php echo $this->element('footer');?>
</body>
  
</html>

$this->Html->css(); (12行目)
/app/webroot/css/からCSSの読み込みを配列で指定してください。

$this->Html->script(); (17行目)
/app/webroot/js/からJavascriptの読み込みを配列で指定

$this->element(); (23~26行目)
/app/View/Elements/から指定してください。

$content_for_layout; (25行目)
コントローラー側$this->render();で指定したViewファイルが読み込まれます。

私はよく、このサンプルソースをベースに付け足したりしながら開発を行っています。

PHP Simple HTML DOM Parserでhtml要素の取得

ここではhtml要素をjQueryのように取得してくれるPHP Simple HTML DOM Parserの紹介をしていきます。

PHP Simple HTML DOM Parserをダウンロード
http://simplehtmldom.sourceforge.net/

サンプルソース

<?php
  
    require_once('simple_html_dom.php');
  
    // html読み込み
    $html = str_get_html('<html><body>Hello World</body></html>');
     
    // URLから読み込み
    $html = file_get_html('http://example.com');
     
    // ファイルから読み込み
    $html = file_get_html('sample.html');
     
    // body内0番目のaタグ内を取得
    $value = $html->find('body a', 0)->innertext;
     
    // body内0番目のaタグごと取得
    $value = $html->find('body a', 0)->outertext;
     
    // body内0番目のaタグhref属性を取得
    $value = $html->find('body a', 0)->href;
     
    // 全てのaタグを配列で取得
    $data = $html->find('body a');
    $value = $data[0]->innertext;
     
    // idやclassなどでも指定
    $data = $html->find('#foo');
    $data = $html->find('.bar');
     
    // メモリリークを回避
    $html->clear();

解説

$html = str_get_html(); (6行目)
htmlを読み込みます。

$html = file_get_html(‘http://example.com’); (9行目)
URLからも読み込めます。

$html = file_get_html(‘sample.html’); (12行目)
ファイルからも読み込めます。

$value = $html->find(‘body a’, 0)->innertext; (15行目)
findでjQueryのようにタグ内の要素を取得することが出来ます。

$value = $html->find(‘body a’, 0)->outertext; (18行目)
outertextとすることで、タグを含めた取得も出来ます。

$value = $html->find(‘body a’, 0)->href; (21行目)
hrefと指定することで、aタグ、href要素を指定することも出来ます。

$data = $html->find(‘body a’); (24行目)
->find(‘body a’);と指定することでaタグの要素を全て配列で取得することができます。

$data = $html->find(‘#foo’); (28、29行目)
idやclassなどのターゲットで指定することも出来ます。

$html->clear(); (32行目)
htmlを生成しすぎるとメモリリークに陥ってしまうので、コールしてください。

画像読み込みのサンプルソース

<?php
  
    require_once('simple_html_dom.php');
     
    // URLから読み込み
    $html = file_get_html('http://example.com');
     
    $value = $html->find('body img', 0)->src;
    $data = file_get_contents($value);
    file_put_contents('dl.jpg', $data);

少し応用をすれば画像ファイルも取り込むことができます。

CakePHP2.x Model

ここではCakePHP2.x Modelの簡単な解説をしていきます。

cakephp

サンプルソースのsql文は仕事で使ったものをそのまま使用しているだけなので、
ご了承お願いします。

サンプルソース

<?php
  
    class Article extends AppModel {
        public $useTable = 'dtb_article';
        public $primaryKey = 'id';
  
        /**
         * $this->query
         * @param type $limit
         */
        public function getQuery($limit) {
            $sql = "SELECT id, name "
                 . "  FROM dtb_article AS ar "
                 . "  LEFT JOIN dtb_news AS ne ON ar.id = ne.id "
                 . " WHERE ar.delflg = 0 "
                 . " GROUP BY ar.id "
                 . " ORDER BY MAX(regi_date) DESC "
                 . " LIMIT {$limit}";
            return $this->query($sql);
        }
         
        /**
         * 1レコード
         * @param type $id
         */
        public function getRow($id) {
            $sql = "SELECT * "
                 . "  FROM dtb_article "
                 . " WHERE id = {$id} ";
            list($result) = $this->query($sql);
            return $result['dtb_article'];
        }
         
        /**
         * 1列
         */
        public function getName() {
            $sql = "SELECT name "
                 . "  FROM dtb_article ";
            $data = $this->query($sql);
            $result = Set::extract('/dtb_article/name', $data);
            return $result;
        }
         
        /**
         * インデックス=>値
         */
        public function getList() {
            $options = array(
                "fields" => array(
                    "id", 
                    "name",
                )
            );
            $this->find("list", $options);
        }
    }

解説

public $useTable = ‘dtb_article’; (4行目)
class名が規約に沿わない場合テーブル名の指定してください。

public $primaryKey = ‘id’; (5行目)
カラム名primaryKeyがidではない場合のみ指定してください。
idなら省略可です。

$this->query (11~20行目)
sql文でデータを取得したいときに使用してください。

Set::extract(‘/dtb_article/name’, $data); (37~43行目)
配列のソートです。
Set::extract(/テーブル名/カラム名,query結果)で指定するとnameの1次元配列で返してくれます。

$this->find(“list”, $options); (48~56行目)
id => nameの配列で返します。
$this->find(“list”)とすると
テーブルのカラム名id => nameがデフォルトで選択されます。

とりあえずデータの取得でよく使う方法をまとめてみました。