检查数组的发布值 - PHP

时间:2013-03-01 12:41:10

标签: php arrays codeigniter

我使用的是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);
    }
}

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

我希望这是一个值得提出的问题。

编辑:

我可以硬编码以查看1是否为已发布的值,然后使用此模型数据输出此类视图,但我希望节省工作量并在执行项目时学到很多东西。 - 自私 - 抱歉。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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];
}

这种方法破坏了代码冗余,同时提供了代码重用性