2016-04-14 119 views
0

view image 其實當我選擇商家ID和今年我想顯示的結果,但是如何使用選擇按鈕可以在任何一個可以幫助我解決這個問題顯示結果3.0

這是我的模型

function overviews($theYear = '', $theMonth = '') 
{ 

$this->db->select("DATEPART(Year, TRANS_TransactionDate) [TheYear], DATEPART(Month, TRANS_TransactionDate) [TheMonth], DATENAME(Month, TRANS_TransactionDate) [TheMonthName], SUM(TRANS_Amount) [TotalAmount]", false); 
$this->db->from('BTBL_Transactions'); 

if ($theYear != '') { 
    $this->db->where("DATEPART(Year, TRANS_TransactionDate) = '" . $theYear . "'", NULL, FALSE); 
} 

if ($theMonth != '') { 
    $this->db->where("DATEPART(Month, TRANS_TransactionDate) = '" . $theMonth . "'", NULL, FALSE); 
} 

$this->db->group_by("DATEPART(Year, TRANS_TransactionDate), DATEPART(Month, TRANS_TransactionDate), DATENAME(Month, TRANS_TransactionDate)"); 
$this->db->order_by("1", "asc"); 
$this->db->order_by("2", "asc"); 
$this->db->order_by("3", "asc"); 

$query = $this->db->get(); 

return $query->result(); 
} 

public function merchant_type_dropdown(){ 
$this->db->distinct(); 
    $this->db->select('TRANS_MerchantId'); 
    $this->db->from('BTBL_Transactions'); 
    $query= $this->db->get(); 
    // echo $this->db->last_query();die(); 
    if($query->num_rows()>0){ 
         $result=$query->result(); 
         return $result; 

    } 
    else{ 
     return false; 
    } 

} 

public function Year_dropdown(){ 
$this->db->distinct(); 
    $this->db->select('DATEPART(Year, TRANS_TransactionDate) [TheYear]'); 
    $this->db->from('BTBL_Transactions'); 
    $query= $this->db->get(); 
    // echo $this->db->last_query();die(); 
    if($query->num_rows()>0){ 
         $result=$query->result(); 
         return $result; 

    } 
    else{ 
     return false; 
    } 

} 

,這是我的控制器

public function overviews() 
{ 
$this->load->model('livemerchant_model'); 
$name=$this->session->userdata('name'); 
$data['monthlyTotals'] = $this->livemerchant_model- >overviews($theyear=''); 
$this->load->view('overviews', $data); 
} 

這是我的看法文件

<label class="tp-label">Select a year</label> 
                   <select> 
                   <?php $year=$this->livemerchant_model->Year_dropdown(); 

          foreach ($year as $row){ 
          ?> 
          <option value="<?php echo $row->TheYear ?>"><?php echo $row->TheYear ?></option> 
         <!--- <option value="tier_two">MPOS</option> 
          <option value="tier_three">E-Commerce</option> 
          <option value="tier_four">Virtual Machine</option>---> 
          <?php }?> 
                   </select> 

商家ID

                <?php $merchanttype=$this->livemerchant_model->merchant_type_dropdown(); 

          foreach ($merchanttype as $merchant){ 
          ?> 
          <option value="<?php echo $merchant->TRANS_MerchantId ?>"><?php echo $merchant->TRANS_MerchantId ?></option> 
         <!--- <option value="tier_two">MPOS</option> 
          <option value="tier_three">E-Commerce</option> 
          <option value="tier_four">Virtual Machine</option>---> 
          <?php }?> 
+0

將商家ID和年份傳遞給控制器​​函數,並使用它來使用where子句過濾模型函數中的結果行。要傳遞這些值,您可以使用表單提交或JavaScript。 –

回答

0

我不能完全肯定我理解正確,這一點,但這裏是我覺得你想要做。首先,您需要調整模型函數以接受另一個可選參數。

function overviews($theMerchant = '', $theYear = '', $theMonth = '') 
{ 
    $this->db->select("DATEPART(Year, TRANS_TransactionDate) [TheYear], DATEPART(Month, TRANS_TransactionDate) [TheMonth], DATENAME(Month, TRANS_TransactionDate) [TheMonthName], SUM(TRANS_Amount) [TotalAmount]", false); 
    $this->db->from('BTBL_Transactions'); 

    if ($theYear != '') { 
     $this->db->where("DATEPART(Year, TRANS_TransactionDate) = '" . $theYear . "'", NULL, FALSE); 
    } 

    if ($theMonth != '') { 
     $this->db->where("DATEPART(Month, TRANS_TransactionDate) = '" . $theMonth . "'", NULL, FALSE); 
    } 

    if ($theMerchant != '') { 
     $this->db->where("TRANS_MerchantId", $theMerchant); 
    } 

    $this->db->group_by("DATEPART(Year, TRANS_TransactionDate), DATEPART(Month, TRANS_TransactionDate), DATENAME(Month, TRANS_TransactionDate)"); 
    $this->db->order_by("1", "asc"); 
    $this->db->order_by("2", "asc"); 
    $this->db->order_by("3", "asc"); 

    $query = $this->db->get(); 

    return $query->result(); 
} 

一旦你這樣做,你將不得不放棄選擇框在您的視圖名稱屬性(理想的ID屬性)。例子:

<select name="transactionYear" id="transactionYear" aria-required="true" required="required"> 

和...

<select name="transactionMerchant" id="transactionMerchant" aria-required="true" required="required"> 

一旦你這樣做,你可以調整你的控制器像下面。在此,我也向你表明它很可能不如你的數據傳遞到視圖,而不是讓視野內填充的變量:

public function overviews() 
{ 
    $this->load->model('livemerchant_model'); 

    $name=$this->session->userdata('name'); 

    if ($this->input->post('transactionMerchant')) { 
     $data['monthlyTotals'] = $this->livemerchant_model->overviews($this->input->post('transactionMerchant'), $this->input->post('transactionYear')); 
    } else { 
     $data['monthlyTotals'] = $this->livemerchant_model->overviews(); 
    } 

    $data['merchanttype'] = $this->livemerchant_model->merchant_type_dropdown(); 
    $data['year'] = $this->livemerchant_model->Year_dropdown(); 

    $this->load->view('overviews', $data); 
} 

這應該給你想要的東西。希望這可以幫助。

+0

感謝@cfnerd爲你的迴應,基本上我是codeigniter的新手,但是當我選擇id後沒有結果,因爲我的瀏覽頁面在tabbar – ohmygood