0

我正在使用 MVC 框架(CodeIgniter)。在我看来,我有两个数组,一个用于常规报告,一个用于管理报告:

 <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>

所有这两个数组都填充了下拉列表。

现在,当它实际发布到控制器时,我的问题就来了。在我的控制器中,我有一个 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);
    }
}

我现在的问题是我如何实际动态加载与模型中正在查询的字段集相对应的视图?我对我的控制器阵列的工作方式充满信心。我还没有参加 Comp。科学 1 所以我一直在以错误的方式学习 php。

我希望这是一个适当的问题。

编辑 :

我可以硬编码以查看 1 是否是发布的值,然后使用此模型数据输出这样那样的视图,但我想节省工作并在做项目时学到很多东西。- 自私 - 对不起。

4

2 回答 2

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 回答
0

这是一个较短的版本。而是在一个函数中完成所有工作,做一个细分:

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 回答