我是cakephp的新手。在此先感谢所有人。我正在使用Cakephp2.8.5
版本。我有一个带有输入字段和提交按钮的表单。当我提交表单时,输入字段应该通过数组并存储在名为$totalData
的变量中,我想将数组变量$totalData
存储在cakephp
中的会话变量中。我在Userscontroller的cartData
函数中编写了代码。请帮我找出如何声明一个数组并将其存储在cakephp中的会话变量中。
我的index.ctp
页面:
<form method="post" action="<?php echo $this->webroot ?>users/cartData?>"">
<table><thead>
<th>Exam Name</th>
<th>Venue Name</th>
<th>Date of Exam</th>
<th>Price / Course</th>
<th>Number of Exams</th>
<th>Add to Cart</th>
</thead>
<tbody>
<tr>
<td>
<select name="course">
<option value="">--Select--</option>
<option value="ITIL Foundation">ITIL Foundation</option>
<option value="PMP">PMP</option>
<option value="Prince 2 Foundation">Prince 2 Foundation</option>
</select>
</td>
<td><input type="text" name="venue" id="venue"></td>
<td><input type="text" name="Userdate" id="Userdate" ></td>
<td><input type="text" name="txtprice" id="Userdate" ></td>
<td>
<select name="num_exams">
<option value="">--Select--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><input type="submit" name="btnSubmit" value="Submit"></td>
</tr></tbody>
</table>
My `UsersController.php` page :
<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{
public function cartData()
{
if($this->request->is('post')|| $this->request->is('put'))
{
$totalData = array(
'exam' => $this->Form->input('course'),
'venue' => $this->Form->input('venue'),
'date' => $this->Form->input('Userdate'),
'price' => $this->Form->input('txtprice'),
'orders' => $this->Form->input('num_exams')
);
// I have a confusion how to store array values in session variable
$_SESSION['total_data'][] = $totalData;
}
}
}
答案 0 :(得分:1)
始终尽最大努力在所有可能的情况下使用框架。一些非常具体的案例是使用助手的问题,但除此之外,总是尝试使用它们。
<?php
$courseOptions = array(
'ITIL Foundation' => 'ITIL Foundation',
'PMP' => 'PMP',
'Prince 2 Foundation' => 'Prince 2 Foundation'
);
$examOptions = array(1,2,3);
?>
<?=$this->Form->create('User', array('action' => 'cartData'))?>
<table>
<thead>
<tr>
<th>Exam Name</th>
<th>Venue Name</th>
<th>Date of Exam</th>
<th>Price / Course</th>
<th>Number of Exams</th>
<th>Add to Cart</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?=$this->Form->input('course', array('options' => $courseOptions)?>
</td>
<td><?=$this->Form->input('venue')?></td>
<td><?=$this->Form->input('userdate')?></td>
<td><?=$this->Form->input('txtprice')?></td>
<td><?=$this->Form->input('num_exams', array('options' => $examOptions)); ?></td>
<td><?=$this->Form->submit('Submit')?></td>
</tr>
</tbody>
</table>
<?=$this->Form->end()?>
所有表单输入助手都会根据创建的表单创建一个数组。在这种情况下,它会为每个名称生成一个数据[用户] [课程],并为ID创建一个UserCourse(以第一个输入为例)。
在控制器端,您可以访问$this->request->data
上具有该结构的数据数组(即$this->request->data['User']['course']
来访问第一个输入值)。
答案 1 :(得分:0)
$this->Form->input()
是FormHelper的一种方法,它旨在用于视图而不是控制器,以便在$this->Form->create()
和$this->Form->end()
旁边生成表单输入。
在您的控制器表单中,使用$this->request->data['Model']['fieldname']
您可以在视图中执行以下操作
<?php
$courseOptions = array(
'ITIL Foundation' => 'ITIL Foundation',
'PMP' => 'PMP',
'Prince 2 Foundation' => 'Prince 2 Foundation'
);
$examOptions = array('1' => '1', '2' => '2', '3' => '3');
echo $this->Form->create('User', array('action' => 'cartData'));
?>
<table>
<thead>
<tr>
<th>Exam Name</th>
<th>Venue Name</th>
<th>Date of Exam</th>
<th>Price / Course</th>
<th>Number of Exams</th>
<th>Add to Cart</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo $this->Form->input('course', array('options' => $courseOptions)); ?>
</td>
<td><?php echo $this->Form->input('venue'); ?></td>
<td><?php echo $this->Form->input('userdate'); ?></td>
<td><?php echo $this->Form->input('txtprice'); ?></td>
<td><?php echo $this->Form->input('num_exams', array('options' => $examOptions)); ?></td>
<td><?php echo $this->Form->submit('Submit'); ?></td>
</tr>
</tbody>
</table>
<?php echo $this->Form->end(); ?>
然后你可以在你的控制器中做这样的事情
<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{
public function cartData()
{
if($this->request->is('post')|| $this->request->is('put'))
{
$totalData = array(
'exam' => $this->request->data['User']['course'],
'venue' => $this->request->data['User']['venue'],
'date' => $this->request->data['User']['userdate'],
'price' => $this->request->data['User']['txtprice'],
'orders' => $this->request->data['User']['num_exams']
);
$this->Session->write('total_data', $totalData);
// Alternatively you can do something like this
$this->Session->write('total_data', $this->request->data['User']);
}
}
}
您可能需要启用会话组件,我强烈建议您在食谱中reading the blog tutorial掌握这些常见组件/帮助以及如何访问帖子数据