2013-03-01 40 views
0

我正在使用MVC框架(CodeIgniter)。在我看來,我有兩個數組一個定期報告,以及一個用於管理報告:檢查發佈的值對陣 - PHP

<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是那麼貼值與此模型數據輸出這樣那樣的觀點,但我要救起來的工作和學習了很多東西,做項目。 - 自私 - 對不起。

+1

我的事你這樣做是最難方式,而不是這樣做,你爲什麼不做一個開關箱 – 2013-03-01 13:00:36

+0

aaahhhhhhh ......大壩..實際上聽起來很容易...好吧。當我完成代碼時,我會更新答案。 – RaGe10940 2013-03-01 13:05:11

回答

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'); 
     } 
    } 

我最終使用,如果else語句,我與開關罩的工作,但我看着速度和有很多的使用開關外殼和之間的if else elseif的爭論......我還是堅持了老可靠如果別的。

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]; 
} 

這個方法斷代碼冗餘,這,在同一時間,提供了代碼重用性