如何在CakePHP中连接字符串值

时间:2015-12-09 07:58:41

标签: php ajax session cakephp concatenation

我创建了一个使用ajax的简单购物车应用程序,我的问题是我想从会话中传递值并将其连接在一个字符串中,但它不添加字符串它只替换旧值。任何人都有任何想法这样做吗?感谢...

我的CartsController.php

    <?php

App::uses('AppController', 'Controller');

class CartsController extends AppController {

    public $uses = array('Timezone');
    public $helpers = array('Html', 'Js' => array("JQuery"), "Session");
    public $components = array('RequestHandler', 'Session');

    public function beforeFilter() {
        parent::beforeFilter();

        if ($this->request->is('ajax')) {
            $this->layout = 'ajax';
        }
    }


    public function viewcart() 
    {
            $this->loadModel('Cart');
            $data = $this->Cart->find('all');
            $this->set('dbdata',$data);
    }

    public function ajax_get_cart() 
    {
        $this->request->onlyAllow('ajax'); 

        //$content = '<div class="alert alert-warning" role="alert">Something unexpected occured</div>';
        if ($content = "")
        {   
            $tempcontent = "";
        }   

        if ($this->request->is('post')) 
        {       
            $this->Timezone->set($this->request->data);
            $ccode = $this->request->data['Cart']['code'];
            $cname = $this->request->data['Cart']['name'];
            $cprice = $this->request->data['Cart']['price'];

            $cartcode[0] = '1';
            $cartcode[1] = 'cart.code2';
            $cartcode[2] = 'cart.code2';

                $this->Session->write(1, $ccode);
                $this->Session->write(2, $cname);
                $this->Session->write(3, $cprice);

                $content .= $this->Session->read(1) . $this->Session->read(2) . $this->Session->read(3);

        }
        else
        {
            $content = "";
        }

        $this->set(compact('content')); 
        $this->render('ajax_response', 'ajax'); 

    }

    public function view()
    {   
            $this->loadModel('Cart');
            $data = $this->Cart->find('all');
            $this->set('dbdata',$data);

    }
}   
?>

1 个答案:

答案 0 :(得分:1)

您使用数字类型而不是字符串或数组来存储会话数据。

CakeSession::write()函数定义如下:

write( string|array $name , string $value null ) 

尝试使用字符串:

$this->Session->write('ccode', $ccode);
$this->Session->write('cname', $cname);
$this->Session->write('cprice', $cprice);

$content .= $this->Session->read('ccode') . $this->Session->read('cname') . $this->Session->read('cprice');