0

我在表单上有以下一系列复选框的示例:

<?php

echo $this->Form->input(
    'reason', array(
        'label'=>false,
        'type' => 'select', 
        'multiple' => 'checkbox',
        'options' => array(
            'Contact me' => 'Contact me',
            'Brochure request' => 'Brochure request',
            'Demonstration request' => 'Demonstration request',
            'Training' => 'Training',
            'Support' => 'Support',
            'Other' => 'Other'
        )
    ));

?>

然后将其设置为控制器中的变量,例如:

$this->set('reason', $this->data['Contact']['reason']);

然后在电子邮件模板中我尝试过这样做:

<?php var_dump( implode( ', ', $reason ) ); ?>

结果如下:string(28) "Contact me, Brochure request"如何删除这些引号和 string(28) 前缀?

这样做的最佳方法是什么?谢谢

4

2 回答 2

2

使用字符串助手/文本助手。它具有内置toList功能。

// controller
App::uses('String', 'Utility');
echo String::toList($this->request->data['Contact']['reason']);

// or
// controller
$this->set('reason', $this->data['Contact']['reason']);
// view
echo $this->Text->toList($reason);
// outputs 1, 2, 3 and 4

假设reason是一个格式如下的数组:

array(
    (int) 0 => '1',
    (int) 1 => '2',
    (int) 2 => '3',
    (int) 3 => '4'
)

或者只是做一个简单的foreach($reason as $k => $v) { echo $v; }

于 2012-09-18T14:53:23.347 回答
0

尝试在视图中访问您的原因:

控制器

$this->set('contact', $this->request->data);

看法

$contact['Contact']['reason'];
于 2012-09-18T14:53:10.980 回答