配列 : グループ化されたデータのこと
[text] <script>
// 配列 – グループ化されたデータ
var sales0 = 100;
var sales1 = 200;
var sales2 = 150;
var sales = [100, 200, 150];
sales[1] = 180;
console.log(sales[1]); // 添字
</script>[/text]
ローカル変数 : ローカル変数が外部からアクセスはできない
[text] <script>
// 関数
// ローカル変数
function getPrice(x) {
var rate = 0.82; // ローカル変数
return (x * rate);
}
console.log(getPrice(83));
console.log(rate);
</script>[/text]
関数の基本的な書き方。
[text]<script>
// 関数(複数の処理をまとめられる)
// aとbは「引数」
function sum(a, b) {
return (a + b);
}
var result = sum(50, 33);
alert(result);
</script>[/text]
for文の書き方。
[text]<script>
// ループ(for)
for (var i = 0; i < 10; i++) {
console.log(i);
}
</script>[/text]
while文の書き方。
[text]
<script>
// ループ(while)
var i = 0;
while (i < 10) {
console.log(i);
i++; // i = i + 1;
}
</script>
[/text]
switch文の書き方。
[text]
<script>
// 条件分岐(switch)
var direction = "right";
var x, y;
switch (direction) {
case "right":
x = x + 10;
break;
case "left":
x = x – 10;
break;
case "up":
y = y – 10;
break;
case "down":
y += 10; // y = y + 10
break;
default:
// 例外処理
break;
}
</script>[/text]
if… else if… else…文の書き方
[text]<script>
// 条件分岐(if)
var score = 20;
if (score >= 70) {
alert("合格!");
} else if (score >= 30) {
alert("不合格!");
} else {
alert("不合格だしお話になりません!");
}
</script>[/text]
条件分岐(if)
[text]<script>
// 条件分岐(if)
var score = 60;
// 比較演算子
// >= <= < >
// == !=
if (score >= 70) {
alert("合格!");
}
</script>[/text]
プログラム用語
[text]変数 :データにつけるラベル[/text]
演算子
[text][/text]代入演算子 : = [text][/text]
[text][/text]文字列に関する演算子 : ”hollo” + “world”[text][/text]
[text][/text]
数値関する演算子 : + – * /
% は 余り
var x = 1;
x++ //x = x + 1;
x– //x = x – 1;
x +=5 // x = x + 5;
[text][/text]
オーバーライド
オブジェクト指向プログラミングにおいてオーバーライド (override)とは、スーパークラスで定義されたメソッドをサブクラスで定義しなおし、動作を上書きすることである。
http://ja.wikipedia.org/wiki/%E3%82%AA%E3%83%BC%E3%83%90%E3%83%BC%E3%83%A9%E3%82%A4%E3%83%89
コンストラクタ
オブジェクト指向のプログラムにおいてオブジェクトを生成するための手続き
hasOneの一括保存
http://cakephp.jp/modules/newbb/viewtopic.php?topic_id=796&forum=6
[php]
public function add() {
if ($this->request->is(‘post’)) {
$this->User->create();
if (!empty($this->data)) {
$user = $this->User->save($this->data);
//if ($this->User->save($this->request->data)) {
// User が保存されたら、User_detail データに User の情報を追加し
// 保存します。
if (!empty($user)) {
// 新しく生成した User の ID は、
// $this->User->id にセットされます。
$this->User->UserDetail->data[‘UserDetail’][‘user_id’] = $this->User->id;
// User は User_detail と hasOne のアソシエーションで関連付いているので
// User モデルを通して Profile モデルにアクセスできます:
$this->User->UserDetail->save($this->data);
}
$this->Session->setFlash(
__(‘保存しました’, __(‘user’)),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-success’
)
);
$this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(
__(‘保存失敗’, __(‘user’)),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-error’
)
);
}
}
$lectures = $this->User->Lecture->find(‘list’);
$prefectures = $this->User->Prefecture->find(‘list’);
$userDetails = $this->User->UserDetail->find(‘list’);
$numbers = $this->User->Number->find(‘list’);
$this->set(compact(‘lectures’, ‘prefectures’, ‘userDetails’,’numbers’));
debug($userDetails);
}
[/php]
[php]
public $hasOne = array(
‘UserDetail’ => array(
‘className’ => ‘UserDetail’,
‘foreignKey’ => ‘user_id’,
‘conditions’ => ”,
‘fields’ => ”,
‘order’ => ”
)
);[/php]
CakePHP 2.xでJavascriptヘルパーを使用する方法
idを取るリンク
Controller
[php]
public function view($id = null) {
$this->Post->id = $id; //idを取る
$this->set(‘post’, $this->Post->read()); //あるIDのデータをフィールド情報+データで受け取る
}
[/php]
View
[php]
<ul>
<?php foreach ($posts as $post) : ?>
<li>
<?php
// debug($post);
// echo h($post[‘Post’][‘title’]);
//
echo $this->Html->link($post[‘Post’][‘title’],’/posts/view/’.$post[‘Post’][‘id’]); //idにリンクをはる
?>
</li>
<?php endforeach; ?>
</ul>[/php]
変数
[php]
public function index() {
$params = array( //params変数の中身を書く
‘order’ => ‘modified desc’,
‘limit’ => 2
);
$this->set(‘posts’, $this->Post->find(‘all’, $params)); //params変数を呼び出し
$this->set(‘title_for_layout’, ‘記事一覧’);
}
[/php]
routes.php
/app/Config/routes.php
[php] Router::connect(‘/’, array(‘controller’ => ‘posts’, ‘action’ => ‘index’));
//どのコントローラのページをルートに表示させるか。postsページのindexページを表示させる
[/php]
foreach
Controller
[php]
<?php
class PostsController extends AppController {
public $helpers = array(‘Html’, ‘Form’); //ヘルパー使いますよ
public function index() {
$this->set(‘posts’, $this->Post->find(‘all’)); // posts変数をviewで使うよ Postのデータを全部取得するよ
}
}
[/php]
Viwe
[php]
<ul>
<?php foreach ($posts as $post) : ?> // foreachでぐるぐると取得するよ
<li>
<?php
// debug($post);
echo h($post[‘Post’][‘title’]);
?>
</li>
<?php endforeach; ?> // foreachでぐるぐると取得の締め
</ul>
[/php]
Bootstrapテンプレート
jQueryのSelect入替
http://web.mt-systems.jp/tech/cakephp/jqueryajax2
http://d.hatena.ne.jp/deeeki/20090313/getjsonselect
http://www.okushin.co.jp/kodanuki_note/2011/03/post-8.html
http://www.openspc2.org/reibun/javascript/form_selectmenu/015/
http://blog.xao.jp/blog/cakephp/one-of-the-simple-way-for-using-ajax-in-cakephp/
findメソッド :CakePHP2.1
http://w.builwing.info/2012/05/16/cakephp2-1%E3%81%AEfind%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89/
[php]
find(‘all’, array(
‘conditions’ => array(‘Article.id’ => 1), ////検索条件の配列
‘fields’ => array(‘"User2"."id"’), ////フィールド名の配列
‘order’ => ‘Article.created’ => ‘desc’, //並び順を文字列または配列で指定
‘recursive’ => 2,
‘group’ => ‘type’
));
[/php]