忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/04/27 06:08 |
PHP1-85:クラスっぽい投稿フォーム作るその2

前回形は作ってしまったので、後はこりこり書いていくだけです。
投稿内容は後でどうとでも修正できるのでとりあえず氏名、年齢、発言という簡単なフォームで、テンプレ以外はまあこんな感じでしょう、ということで途中まで作ったの図。

/form/lib/index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
 
//=========================================================================
//準備
    
    //根本のフォルダ
        $base_dir=dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR ;
        define('BASE_DIR',$base_dir);
    
    //必要なファイルをインクルード
        require_once('../../smarty/libs/Smarty.class.php');
 
//=========================================================================
//クラスclass HtmlForm{
    
    //-------------------------------------------------------------------------
    //インスタンス変数等
        
        //Smartyテンプレ、キャッシュ置き場
            private $smarty_template_dir='../templates/';
            private $smarty_compile_dir='../templates_c/';
        
        //エラーメッセージ入れ
            protected $error_message_array=array(
                 'error_name_null'=>'氏名が入力されていません'
                ,'error_age_null'=>'年齢が入力されていません'
                ,'error_age_int'=>'年齢が正しくありません'
            );
        
    //-------------------------------------------------------------------------
    //コンストラクタ
    public function __construct(){
        
        //Smarty
            $this->smarty=new Smarty();
            $this->smarty->template_dir = $this->smarty_template_dir;
            $this->smarty->compile_dir  = $this->smarty_compile_dir;
        
        //実行メソッドを呼ぶ
            $this->init();
            exit();
    }
    
    //-------------------------------------------------------------------------
    //実行
    public function init(){
        
        //引数によって実行するメソッドを分岐
            if(isset($_REQUEST['form_input'])){
                $this->FormInput();
            }elseif(isset($_REQUEST['form_complete'])){
                $this->FormComplete();
            }else{
                $this->FormIndex();
            }
    }
    
    //-------------------------------------------------------------------------
    //表示
    public function view($template_file='index.html'){
        
        //変数アサイン
            //$this->dataに突っ込めば$dataで表示できるようにします
                $this->smarty->assign('data',$this->data);
            //$_REQUESTは常時参照可能
                $this->smarty->assign('request',$_REQUEST);
        
        //表示
            $this->smarty->display($template_file);
            exit();
    }
    
    //-------------------------------------------------------------------------
    //投稿内容一覧画面
    public function FormIndex(){
        
        //表示内容取得
            $this->data['applied_form']=file('data.txt',FILE_IGNORE_NEW_LINES);
        
        //表示
            $this->view('form_index.html');
    }
    
    //-------------------------------------------------------------------------
    //フォーム入力画面
    public function FormInput(){
        
        //表示
            $this->view('form_input.html');
    }
    
    //-------------------------------------------------------------------------
    //フォーム入力完了画面
    public function FormComplete(){
        
        //入力値のバリデート
            $ret=$this->_FormValidate($_REQUEST);
            if(!$ret){
                $this->FormInput();
            }elseif($ret['error']){
                $this->data['error_message']=
                    $this->_FormSetErrorCode($ret['error']);
                $this->FormInput();
            }
            
        //保存
            file_put_contents('data.txt',$ret['data'],FILE_APPEND);
        
        //表示
            $this->view('form_complete.html');
    }
    
    
    //=========================================================================
    
    //-------------------------------------------------------------------------
    //サブルーチン//入力値チェック
    public function _FormValidate($request_array){
        
        //引数
            if(!$request_array){return false;}
        
        //バリデート
        
        //返却
            return $ret;
    }
    
    //-------------------------------------------------------------------------
    //サブルーチン//エラーメッセージを入れておく
    public function _FormSetErrorCode($error_code_array=array()){
        
        //引数
            if(!is_array($error_code_array)){return false;}
        
        //返り値
            $ret=array();
        
        //各エラーコードに対してエラーメッセージを対応させる
            foreach($error_code_array as $error_code_val){
                $ret[]=$this->error_message_array[$error_code_val];
            }
        
        //返却
            return $ret;
    }
    
    
//クラスのおわり
}


フォームデータを保存する部分は非常にやる気がないので超適当です。
まともに作るときはDBに入れた方が面倒もないでしょう。

$error_message_arrayと_FormSetErrorCode()あたりはもうちょっとどうにかならなかったのだろうか、という気がひしひしとしますが気にしない。

というわけで、あとはテンプレを書いて_FormValidate()と合わせれば完成といったところです。


ところで、私はだいたいいつも今回のようにテンプレを後回しにして作成するのですが、どのような順番で作成するのがいいんでしょうかね?

 

PR


2009/08/14 18:38 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<PHP1-86:クラスっぽい投稿フォーム作るその3 | HOME | PHP1-84:クラスっぽい投稿フォーム作る>>
忍者ブログ[PR]