[objective-c]NSStringでクラス名を指定する
NSStringでクラス名を指定する
Class targetClass=NSClassFromString(@"SettingViewController"); UIViewController*currentCV=[[targetClass alloc]init];
NSStringでクラス名を指定する
Class targetClass=NSClassFromString(@"SettingViewController"); UIViewController*currentCV=[[targetClass alloc]init];
AppModel.php
public function isHalfLetter($data) { $str = current($data); return preg_match('/^[\x21-\x7E]*$/', $str); }
各モデル
var $validate=array( 'target'=>array( 'rule1'=>array( 'rule' => 'isHalfLetter', 'message' => '全角文字が含まれてるっす' ), ) );
DB設定を記述する。
Config/database.php
public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'ktyr', 'password' => 'ktyrps', 'database' => 'ktyrdb', 'prefix' => '', 'encoding' => 'utf8', ); public $testdb = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'ktyr', 'password' => 'ktyrps', 'database' => 'ktyrtestdb', 'prefix' => '', 'encoding' => 'utf8', );
DBを切替える。
各Model内で切替える場合。
/* testdbに切替える */ $this->setDataSource('testdb'); /* defaultに切替える */ $this->setDataSource('default');
各Model内
/* トランザクションスタート */ $dataSource=$this->getDataSource(); $result=$dataSource->begin(); /* コミット */ $result=$dataSource->commit(); /* ロールバック */ $result=$dataSource->rollback();
AppModel.phpに下記のようにするのもいいかもね。
public function begin(){ return $this->getDataSource()->begin(); } public function commit(){ return $this->getDataSource()->commit(); } public function rollback(){ return $this->getDataSource()->rollback(); }
同定義のテーブルA、テーブルBにおいて
テーブルAにあり、テーブルBに無いデータのidを取得する。
SELECT id FROM テーブルA WHERE NOT EXISTS (SELECT id FROM テーブルB WHERE テーブルB.id = テーブルA.id) ORDER BY id
配列演算
PHP
$a = array('a' => 'b','c'=>'d','e'=>'f' ); $b = array('g' => 'h','i'=>'j','k'=>'l','a'=>'あ' ); $c = $a+$b; foreach ($c as $key => $value) { echo $key.' : '.$value.'<br/>'; }
出力結果
PHP
$a = array('a' => 'b','c'=>'d','e'=>'f' ); $b = array('g' => 'h','i'=>'j','k'=>'l','a'=>'あ' ); $c=array_merge($a,$b); foreach ($c as $key => $value) { echo $key.' : '.$value.'<br/>'; }
出力結果
PHP
$a = array('a','b','c'); $b = array('d','e','f','g'); $c = $a+$b; foreach ($c as $key => $value) { echo $key.' : '.$value.'<br/>'; }
出力結果
PHP
$a = array('a','b','c'); $b = array('d','e','f','g'); $c=array_merge($a,$b); foreach ($c as $key => $value) { echo $key.' : '.$value.'<br/>'; }
出力結果
下記はエラーになる。
$c = $a-$b; $c = $a*$b; $c = $a/$b;
指定フォルダのファイル一覧を取得
<ul> <?php $tmp=getFiles('./image/'); foreach ($tmp as $key => $value) { echo '<li>'.$value.'</li>'; } function getFiles($targetDir){ $tmp=array(); $pDir=opendir($targetDir); while( false!==( $fileName = readdir($pDir) ) ){ if($fileName !== '.' && $fileName !== '..' && !is_dir($targetDir.$fileName)){ $tmp[]=$fileName; } } closedir( $pDir ); return $tmp; } ?> </ul>
checkboxでvalueを0に出来ない
この方法だと出来ない。
<?php $options = array('hiddenField' => false,'value'=>'0'); echo $this->form->checkbox('samples.check1.', $options); echo '1-A<br/>'; $options = array('hiddenField' => false,'value'=>'1'); echo $this->form->checkbox('samples.check1.', $options); echo '1-B'; ?>
出力結果
<input type="checkbox" name="data[samples][check1][]" value="1" id="samplesCheck1"/>1-A<br/> <input type="checkbox" name="data[samples][check1][]" value="1" id="samplesCheck1"/>1-B
理由はFormHelper.phpのcheckboxメソッドに下記の処理があるため
if (empty($options['value'])) { $options['value'] = 1; }
しかし、下記のやり方だと出来る。
ただし、<div>と<label>が勝手に付いてしまう。
<?php $label=array('2-A','2-B','2-C','2-D'); echo $this->Form->input('check2', array('type' => 'select', 'multiple' => 'checkbox', 'options' => $label, 'div'=>false, 'label'=>false, 'hiddenField'=>false, 'before'=>'', 'after'=>'', 'between'=>'', 'value' => array(0, 2) ) ); ?>
出力結果
<div class="checkbox"><input type="checkbox" name="data[samples][check2][]" checked="checked" value="0" id="SamplesCheck20" /><label for="SamplesCheck20" class="selected">2-A</label></div> <div class="checkbox"><input type="checkbox" name="data[samples][check2][]" value="1" id="SamplesCheck21" /><label for="SamplesCheck21">2-B</label></div> <div class="checkbox"><input type="checkbox" name="data[samples][check2][]" checked="checked" value="2" id="SamplesCheck22" /><label for="SamplesCheck22" class="selected">2-C</label></div> <div class="checkbox"><input type="checkbox" name="data[samples][check2][]" value="3" id="SamplesCheck23" /><label for="SamplesCheck23">2-D</label></div>
selectメソッドでも出来る。
<?php $label=array('2-A','2-B','2-C','2-D'); echo $this->Form->select('check3', $label, array('multiple' => 'checkbox', 'div'=>false, 'label'=>false, 'hiddenField' => false, 'class'=>'testClass' ) ); ?>
inputでファイル選択後即時アップ。
[Twitter]tweet with mediaでも実装していました。jQuery.uploadを使います。
Sample ※Firefox,IE8,9で動作確認済
index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript"> <meta http-equiv="Content-Style-Type" content="text/css"> <title>ファイル選択後即時アップ</title> <meta name="description" content="****"> <meta name="keywords" content="****"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> <link rel="apple-touch-icon" href="apple-touch-icon.png"> <link rel="stylesheet" href="style.css" type="text/css"> <script type="text/javascript" src="jquery-1.9.1.min.js"></script> <script type="text/javascript" src="jquery.upload-1.0.2.min.js"></script> <script type="text/javascript" src="script.js"></script> </head> <body> <form method="post" action="" target="f1" enctype="multipart/form-data"> <input id="filebox" type="file" name="image" size="0" accept="image/*"/> </form> <ul id="preview"></ul> </body> </html>
script.js
$(function(){ set_thumaction(); }); function remove_thum(){ $('#preview').empty(); } function set_thumaction(){ $('#filebox').change(function() { var preview = $('#preview'); $(this).upload('upload.php',$("form").serialize(),function(html){ preview.append(html); },'html'); }); } function form_tweetmode(){ $('#filebox').remove(); $('form').attr('enctype','application/x-www-form-urlencoded'); } function restore_form(){ $('#fileboxwrap').html('<input id="filebox" type="file" name="image" size="0">'); $('form').attr('enctype','multipart/form-data'); set_thumaction(); }
upload.php
<?php if(isset($_FILES["image"])){ deleteFilesOfDir('./tmp'); $html='<li>'; $v=$_FILES["image"]; $ext=end(explode('.', $v["name"])); $filename='./tmp/'.time().'.'.$ext; $result = @move_uploaded_file( $v["tmp_name"], $filename); if($result){ $html .= '<img src="'.$filename.'" width="300"/><br/>'; }else{ $html .= 'エラー<br/>'; } foreach($_FILES["image"] as $key => $v){ $html .= $key.' : '.$v.'<br/>'; } $html .= '</li>'; echo $html; } function deleteFilesOfDir($dirPath){ if (!isset($dirPath)) { return; } $res_dir = opendir($dirPath); while( $file_name = readdir( $res_dir ) ){ if (is_dir($dirPath.'/'.$file_name)) { continue; } unlink($dirPath.'/'.$file_name); } closedir($res_dir); } ?>
jQuery.upload
http://lagoscript.org/jquery/upload