0

さて、コントローラーにこのコードスニペットがあります。ただし、これはすべてDB駆動型であり、実際にはモデルに含まれている必要があります。ただし、IFステートメントでわかるように、$dataをビューに渡す必要があります。結果に基づく。このコーディングのチャックをモデルのメソッドに貼り付けようとしました(コントローラーを介してモデルメソッドを呼び出します)が、$data[update_prompt]文字列がビューによって呼び出されません...

このコードをモデルに変換するにはどうすればよいですか?$ data値をコントローラーに送り返して、ビューに埋め込みますか?

    // show appropriate upgrade message if user has free account

    $id = $this->session->userdata('user_id'); 

    $this->db->select('subscription'); // select the subscription column
    $this->db->where('id', $id); //find id in table that matches session id
    $query = $this->db->get("subscriptions"); // connect to this database
    $subscribe = $query->result_array(); //returns the result of the above

    if($subscribe[0]['subscription'] == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
    {
        $data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
    }
    else 
    {
        $data['update_prompt'] = '';
    }
4

1 に答える 1

2

次のように、モデルに関数を追加します。

public function myModelFunction($id) {

    //we return row as we are looking up by primary key and are guaranteed only one row
    return $this->db->select('subscription')
                    ->where('id', $id)
                    ->get('subscriptions')
                    ->row();
}

次に、コントローラーで:

public function myControllerFunction() {

    $subscribe = $this->my_model->myModelFunction($this->session->userdata('id'));
    if($subscribe->subscription == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
    {
        $data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
    }
    else 
    {
        $data['update_prompt'] = '';
    }
}
于 2012-11-07T14:59:12.250 に答える