2016-03-04 43 views
1

我正在開發codeigniter站點。這裏是我的控制器功能:在codeigniter站點的cookie中設置數組

public function hoteldetails($id) 
{ 
    $this->load->helper('cookie'); 
    $hotel_details_view = array(); 


    $hotel_details_view = get_cookie('hotel_details_view'); 
    array_push($hotel_details_view, $id); 

    $this->input->set_cookie($hotel_details_view); 
    print_r($hotel_details_view); 
    $this->load->view('hotel_details',$data); 

} 

現在我想在功能的時間來設置cookie的多個$ ID叫

我得到的錯誤:

A PHP Error was encountered

Severity: Warning

Message: array_push() expects parameter 1 to be array, boolean given

Filename: controllers/hotel.php

Line Number: 191

我需要幫助:)

回答

1

UPDATE:試試這個:

public function hoteldetails($id) 
{ 
    $this->load->helper('cookie'); 

    $hotel_details_view = array(); 
    $cookieValue = get_cookie('hotel_details_view');  
    $hotel_details_view[] = $cookieValue; 
    $hotel_details_view[] = $id; 

    $this->input->set_cookie($hotel_details_view); 
    print_r($hotel_details_view); 
    $this->load->view('hotel_details',$data); 
} 

$hotel_details_view將使用字符串值覆蓋數組,該值將從get_cookie()獲得。

+0

我曾經嘗試這樣做,這不是打印陣列:( –

+0

@SharmisthaDas:答案更新 – devpro

0
<?php 
public function hoteldetails($id) 
{ 
    $this->load->helper('cookie'); 
    $hotel_details_view = array(); 
    $hotel_details_view = get_cookie('hotel_details_view'); 
    $hotel_details_view[] = $id; 
    $this->input->set_cookie($hotel_details_view); 
    print_r($hotel_details_view); 
    $this->load->view('hotel_details',$data); 

} 
?> 
+0

這只是在陣列中添加一個ID –

+0

推入陣'的print_r($ hotel_details_view)之前打印您的Cookie;'。 –