0

私はMVCフレームワーク(CodeIgniter)を使用しています。私の見解では、2つのアレイがあります。1つは通常のレポート用で、もう1つは管理レポート用です。

 <div id="queries">

        <center><strong>Report Generator</strong></center>
<br />

<?php 
    $performance = array(
        '' => 'Click Here',
        '1' => 'Student Total Wait',
        '2' => 'Counselor Performance Per Session',
        '3' => 'Average Counselor Performance',
    );

    $admin = array(
        '' => 'Click Here',
        '4' => 'Reasons For Visit',
        '5' => 'Aid Years',
    );

    echo form_open('reports_controller/generate');
    echo "<p><strong>Performance Reports</strong></p>";
    echo form_dropdown('performance', $performance); 
    echo "<p><strong>Administrative Reports</strong></p>";
    echo form_dropdown('admin', $admin);

    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo form_submit('submit', 'Generate Report');
?>

</div>

これら2つのアレイはすべて、ドロップダウンに入力されます。

今、私の問題は、それが実際にコントローラーに投稿されたときに発生します。私のコントローラーには、投稿された値が存在するかどうかを確認するforeachループがあります。存在する場合は、ユーザーが選択したものに基づいてモデル(SQLクエリ)を動的にプルします。

コントローラー:

class Reports_controller extends MY_logincontroller {

function generate() {
    $this -> load -> model('reports_model');
    $reportsfunction = array('1' => 'studenttotalwait()', '2' => 'counselorperformance()', '3' => 'avgperformance()', '4' => 'reasons()', '5' => 'aidyears()', );
    foreach ($reportsfunction as $model_function) {
        $data['returns'] = $this -> reports_model -> $model_function;
        $data['main_content'] = 'reports/generate';
        $this -> load -> view('includes/js/js_template', $data);
    }
}

私の質問は、モデルでクエリされているフィールドセットに対応するビューを実際に動的にロードする方法です。コントローラアレイがどのように機能しているかに自信があります。私はまだコンプを服用していません。科学1なので、私は誤った方法でphpを学んでいます。

これが適切な質問であることを願っています。

編集 :

1が投稿された値であるかどうかをハードコーディングして、このモデルデータを使用してそのようなビューを出力することはできますが、プロジェクトを実行しながら、作業を節約して多くのことを学びたいと思います。-わがまま-ごめんなさい。

4

2 に答える 2

0

これは短いバージョンです。すべての作業を 1 つの関数で行う代わりに、内訳を行います。

function generate()
{
    $this -> load -> model('reports_model');
    $performance = $this -> input -> post('performance');
    $admin = $this -> input -> post('admin');

    if (!empty($performance) || !empty($admin))
    {
        $data = $this->_data($performance);
        $data['main_content'] = 'reports/tables/'.$performance;
        $this -> load -> view('includes/js/js_template', $data);
    } 
    else
    {
        $this -> session -> set_flashdata('reports', 'Please choose a report');
        redirect('staff_controller/reports', 'location');
    }

}

// saves the day
function _data($performance = 0)
{
    $this -> load -> model('reports_model');

    if(!isset($data[$performance]))
    {
        return FALSE;
    };

    $data = array(
        1 => array(
                 'totalwait' => $this -> reports_model -> studenttotalwait()
             ),
        2 => array(
                 'performance' => $this -> reports_model -> counselorperformance()
             ),
        3 => array(
                 'avgperformance' => $this -> reports_model -> avgperformance()
             ),
        4 => array(
                 'reasons' => $this -> reports_model -> reasons()
             ),
        5 => array(
                 'avgperformance' => $this -> reports_model -> aidyears()
             )
    );
    return $data[$performance];
}

この方法は、コードの冗長性を解消すると同時に、コードの再利用性を提供します。

于 2013-03-02T08:17:15.567 に答える
0
function generate() {
        $this -> load -> model('reports_model');
        $performance = $this -> input -> post('performance');
        $admin = $this -> input -> post('admin');
        if (!empty($performance) || !empty($admin)) {

            if ($this -> input -> post('performance') == '1') {
                $data['totalwait'] = $this -> reports_model -> studenttotalwait();
                $data['main_content'] = 'reports/tables/1';
                $this -> load -> view('includes/js/js_template', $data);
            }

            if ($this -> input -> post('performance') == '2') {
                $data['performance'] = $this -> reports_model -> counselorperformance();
                $data['main_content'] = 'reports/tables/2';
                $this -> load -> view('includes/js/js_template', $data);
            }

            if ($this -> input -> post('performance') == '3') {
                $data['avgperformance'] = $this -> reports_model -> avgperformance();
                $data['main_content'] = 'reports/tables/3';
                $this -> load -> view('includes/js/js_template', $data);
            }

            if ($this -> input -> post('admin') == '4') {
                $data['reasons'] = $this -> reports_model -> reasons();
                $data['main_content'] = 'reports/tables/4';
                $this -> load -> view('includes/js/js_template', $data);
            }

            if ($this -> input -> post('admin') == '5') {
                $data['avgperformance'] = $this -> reports_model -> aidyears();
                $data['main_content'] = 'reports/tables/5';
                $this -> load -> view('includes/js/js_template', $data);
            }

        } else {
            $this -> session -> set_flashdata('reports', 'Please choose a report');
            redirect('staff_controller/reports', 'location');
        }
    }

if else ステートメントを使用することになり、Switch Case を使用しましたが、速度を調べたところ、switch case の使用と if else elseif の間には多くの議論があります... 古い信頼できる if else に固執しました。

于 2013-03-02T00:42:51.203 に答える