通过在codeigniter 3.0中选择选项来显示结果

时间:2016-04-14 11:58:18

标签: sql-server codeigniter

view image 实际上,当我选择商家ID和年份我想显示结果,但如何使用该选择按钮可以帮助我解决问题

这是我的模特

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

1 个答案:

答案 0 :(得分:0)

我并不完全确定我理解这一点,但这就是我想要做的想法。首先,您需要调整Model函数以接受另一个可选参数。

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

这应该可以满足您的需求。希望这会有所帮助。