簡體   English   中英

未設置Codeigniter會話變量

[英]Codeigniter Session variable not set

我正在嘗試使用$ this-> session-> set_userdata($ booking_data)設置會話變量,但不會設置會話變量。 我的控制器是:

<?php

class Car extends CI_Controller {

    function __construct()
    {       
        parent::__construct();

        $this->load->model(array('Tariff_model', 'Category_model', 'Brand_model', 'Car_model', 'Booking_model'));
        $this->load->helper('formatting_helper');
    }

    function index($car_id = 0)
    {
        //price calculation
        $delivery_charge = $data['locations'][$this->input->post('start_location')]['delivery_charge'];
        $collection_charge = $data['locations'][$this->input->post('return_location')]['collection_charge'];
        $tax_vat = config_item('vat')/100;

        $booking_price = ($car->price_3*$total_days) + $delivery_charge + $collection_charge;
        $booking_price_vat = $booking_price*$tax_vat;                   
        $booking_total_price = $booking_price+$booking_price_vat;

        $booking_data = array(
            'car_id' => $car_id,
            'car_slug' => $car->slug,
            'vehicle' => $car->title,
            'start_location' => $this->input->post('start_location'),
            'start_date' => $this->input->post('start_date'),
            'start_time_hour' => $this->input->post('start_time_hour'),
            'start_time_min' => $this->input->post('start_time_min'),
            'return_location' => $this->input->post('return_location'),
            'end_date' => $this->input->post('end_date'),
            'end_time_hour' => $this->input->post('end_time_hour'),
            'end_time_min' => $this->input->post('end_time_min'),
            'discount' => '',
            'start_location_name' => $data['locations'][$this->input->post('start_location')]['name'],
            'return_location_name' => $data['locations'][$this->input->post('return_location')]['name'],
            'start_date_time' => $this->input->post('start_date').' at '.$this->input->post('start_time_hour').$this->input->post('start_time_min'),
            'return_date_time' => $this->input->post('end_date').' at '.$this->input->post('end_time_hour').$this->input->post('end_time_min'),
            'total_days' => $total_days,
            'total_price' => format_currency($booking_total_price)
        );

        $this->Booking_model->set_booking_data($booking_data);

    }
}

?>

和Booking_model.php:

<?php
Class Booking_model extends CI_Model
{

  function __construct()
  {
    parent::__construct();
  }

  function reset_booking_data()
  {
    $this->session->unset_userdata('booking');
  }

  function set_booking_data($data)
  {
    $booking_data = array();
    $booking_data['booking'] = array();
    $booking_data['booking'] = $data;

    // put our booking in the cart
    $this->session->set_userdata($booking_data);
  }

  function save_booking_data($data)
  {
    if(isset($data['id']))
    {
      $this->db->where('id', $data['id']);
      $this->db->update('orders', $data);
      return $data['id'];
    }
    else
    {
      $this->db->insert('orders', $data);
      return $this->db->insert_id();
    }
  }

  function get_booking_data()
  {
    $booking = $this->session->userdata('booking');

    return $booking;
  }

  function is_booking_data()
  {    
    $booking = $this->session->userdata('booking');

    if(!$booking)
    {
      return false;
    }
    else
    {
      return true;
    }
  }

  function orders($data=array(), $return_count=false)
  {
    if(empty($data))
    {
      //if nothing is provided return the whole shabang
      $this->get_all_orders();
    }
    else
    {
      //grab the limit
      if(!empty($data['rows']))
      {
        $this->db->limit($data['rows']);
      }

      //grab the offset
      if(!empty($data['page']))
      {
        $this->db->offset($data['page']);
      }

      //do we order by something other than category_id?
      if(!empty($data['order_by']))
      {
        //if we have an order_by then we must have a direction otherwise KABOOM
        $this->db->order_by($data['order_by'], $data['sort_order']);
      }

      if($return_count)
      {
        return $this->db->count_all_results('orders');
      }
      else
      {
        return $this->db->get('orders')->result();
      }

    }
  }

  function get_all_orders()
  {
    //sort by alphabetically by default
    $this->db->order_by('id', 'DESC');
    $result = $this->db->get('orders');

    return $result->result();
  }  
}

?>

我的$booking_data var_dump輸出為:

array(1) {
    ["booking"]=> array(18) {
        ["car_id"]=> string(1) "7"
        ["car_slug"]=> string(19) "audi-r8-v10-spyder1"
        ["vehicle"]=> string(16) "Aston Martin DB9"
        ["start_location"]=> string(20) "premiere-velocity-hq"
        ["start_date"]=> string(8) "23/09/15"
        ["start_time_hour"]=> string(2) "10"
        ["start_time_min"]=> string(2) "00"
        ["return_location"]=> string(18) "birmingham-airport"
        ["end_date"]=> string(8) "24/09/15"
        ["end_time_hour"]=> string(2) "10"
        ["end_time_min"]=> string(2) "00"
        ["discount"]=> string(0) ""
        ["start_location_name"]=> string(20) "Premiere Velocity HQ"
        ["return_location_name"]=> string(18) "Birmingham Airport"
        ["start_date_time"]=> string(16) "23/09/15 at 1000"
        ["return_date_time"]=> string(16) "24/09/15 at 1000"
        ["total_days"]=> int(1)
        ["total_price"]=> string(8) "£744.00"
    }
}

您需要初始化會話庫以啟動CI會話。 在控制器中使用它。

$this->load->library('session');

或者,您可以將其添加到自動加載配置中,以便始終加載會話。 確保同時在配置中設置會話密鑰,否則CI會話將無法工作。 如果記憶對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM