2017-11-22 8 views
0

私はユーザーがウェブサイトに商品を追加できるようにウェブサイトを作っています。今私は製品の詳細ページに取り組んでいます。すべてがうまく動作し、製品のすべての詳細がエコーされています。しかし今、私はボタンを作ろうとしています。そのボタンをクリックすると、 'cadeaupagina_ontvanger'という名前の新しいビューページにリンクする必要があります。また、製品をコピーしてそのビューページに貼り付ける必要があります。 CodeIgniterにこの関数を書く方法がわかりません 誰かが私を助けてくれますか?商品をCodeIgniterの別のビューページにコピー

これは、詳細ページからボタンです:

<a href="<?php echo base_url() . 'Product/cadeaupagina_ontvangen/'.$product['user_id'];?>"> <button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a> 

コントローラの機能の詳細:

public function details($product_id) 
    { 
    //load the Product_model 
    $this->load->model('Product_model'); 
    //call function getdata in de Product_model 
    $data['userdetail_list'] = $this->Product_model->getdata(); 
    $this->session->set_userdata("product_id", $product_id); 

    //get product details 
    $data['product'] = $this->Product_model->get_product_details($product_id); 
    $data['products'] = $this->Product_model->selectProducts(); 

    //laad view 
    $data['main_content'] = 'details'; 
    $this->load->view('details',$data); 
    } 

コントローラ機能cadeaupagina_ontvangen:

public function cadeaupagina_ontvangen($product_id) { 
    $this->load->model('Product_model'); 
    //call function getdata in de Product_model 
    $data['userdetail_list'] = $this->Product_model->getdata(); 

    // get product details 
    // if you want to pass data by url last param than use this 
    if(!empty($product_id)){ 
    $data['product'] = $this->Product_model->get_product_details($product_id); 
    } 


    //if you want to pass data in session than use this 
    //get session in current controller 
    $product_id = $this->session->userdata("product_id"); 
    if(!empty($product_id)){ 
     $data['product'] = $this->Product_model->get_product_details($product_id); 
    } 


    $data['products'] = $this->Product_model->selectProducts(); 

    //laad view 
    $data['main_content'] = 'ontvangen_cadeaus'; 
    $this->load->view('cadeaupagina_ontvanger',$data); 
    } 

cadeaupagina_ontvangerは私がしたいビューのページです同じ製品を貼り付ける。 Product_modelで

モデル関数:

public function getdata() 
    { 
     $this->db->select('*'); 
     $this->db->from('users'); 
     $this->db->join('products','products.user_id = users.user_id'); 
     $query = $this->db->get(); 
     if($query->num_rows()>0) 
     { 
      return $query->result_array(); 
     } 
    } 

    public function selectProducts() 
    { 
     $query= $this->db->query("SELECT * FROM products"); 
     $result = $query->result_array(); 
     return $result; 
    } 



    /** Met deze functie kan je de product details ophalen via de $product_id **/ 
    public function get_product_details($product_id) 
    { 
     $arrReturn = array(); 
     $this->db->select('*'); 
     $this->db->from('products'); 
     $this->db->where('product_id', $product_id); 
     $query = $this->db->get(); 
     $result = $query->result_array(); 
     if (!empty($result)) { 
      $arrReturn = $result[0]; 
     } 
     return $arrReturn; 
    } 
+0

を追加このコントローラの以前のコントローラメソッド。 –

+0

私の質問を編集し、以前のコントローラの機能を表示する – Learningprogrammingphp44

+0

詳細のコントローラ機能をチェックしました – Learningprogrammingphp44

答えて

2

あなたは2種類で、これを行うことができます。

1.コントローラのURLとプロダクトIDを送信します。最後に商品IDを追加したURLにこのURLが含まれていることを確認してください。

<a href ='<?php echo base_url("Product/cadeaupagina_ontvangen/$product_id"); ?>'><button type="button" class="btn btn-default">Ik wil dit cadeau!</button></a> 

2.それとも、現在のコントローラに今、この

$this->session->set_userdata("product_id", $whatever_product_id); 

のような以前のコントローラでは、同様のセッションでプロダクトIDを追加することができます。

public function cadeaupagina_ontvangen($product_id='') { 
    $this->load->model('Product_model'); 
    //call function getdata in de Product_model 
    $data['userdetail_list'] = $this->Product_model->getdata(); 

    //get product details 
    //if you want to pass data by url last param than use this 
    if(!empty($product_id)){ 
     $data['product'] = $this->Product_model->get_product_details($product_id); 
    } 


    //if you want to pass data in session than use this 
    //get session in current controller 
    $pid = $this->session->userdata("product_id"); 
    if(!empty($pid)){ 
     $data['product'] = $this->Product_model->get_product_details($pid); 
    } 




    //laad view 
    $data['main_content'] = 'ontvangen_cadeaus'; 
    $this->load->view('cadeaupagina_ontvanger',$data); 
} 
+0

こんにちは、詳細ページからボタンをクリックするとどうなりますか?ontvangen_cadeausビューに入ると、すべての製品がエコーされます。私は私の質問を編集し、すべてのコードを表示し、あなたが私のためにそれを修正することを願って – Learningprogrammingphp44

+0

私はそれがすべての製品をエコーするようにしたくありません。私は詳細ページから同じ製品をエコーし​​、同じログインユーザのontvangen cadeauのページにそれをコピー&ペーストしたいと思います。したがって、次回同じユーザーがontvangen_cadeausビューページに行くと、その製品がそこに存在するはずです。だから、私は、製品テーブルからのuser_idとuserテーブルからのuser_idで何かをする必要があると思いますか?また私は私の質問を編集しました。私のコントローラの機能を今すぐ見ることができます。あなたが私を助けることを望む – Learningprogrammingphp44

+0

それで、私は特定の製品をクリックしていると言うことができます。私は詳細ページにリンクします。特定のproduct_idでたとえばProduct/details/96というリンクはproduct_id = 96の製品詳細ページにあります。そのページでは、別のページにリンクしてその同じ製品をその新しいビューページにコピーするボタンが必要です。しかし、今のところログインしているユーザーのみ。彼はontvangen_cadeausビューページ – Learningprogrammingphp44

関連する問題