how to submit value after selecting radio button in cakephp? - cakephp

my view code is here...what is code for controller, plz can anyone explain.
<td><?php $attributes = array();
$options = array($question['Question']['opt1']);
echo $this->Form->radio($i, $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt2']);
echo $this->Form->radio($i, $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt3']);
echo $this->Form->radio($i, $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt4']);
echo $this->Form->radio($i, $options, $attributes);?> </td>
</tr>
<?php endforeach; ?>
<td><?php echo $this->Form->end(__('Submit'),array('controller'=>'question')); ?></td>

As simple as that. In your questions controller let's say your form is submitted to '/questions/yourActionMethodName'
Your code should look something like:
public function yourActionMethodName(){
if($this->request->is('post') && !empty($this->request->data)){
$this->Question->create(); //to save a new record
$this->Question->save($this->request->data);
}
}
This should create a new New record setting the database column value of field 'opt1' to the value selected by the user. I am not sure what's the case you are trying to solve. This should give you an idea of how things saved to database. Tell me more on the case and I could edit the code to match your needs.

you need put the model and name of this
<td><?php $attributes = array();
$options = array($question['Question']['opt1']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt2']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt3']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt4']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
</tr>
<?php endforeach; ?>
<td><?php echo $this->Form->end(__('Submit')); ?></td>
and after in your controller if you need save a lot fields you need use use saveAll or saveMany dep
public function Name(){
if($this->request->is('post') && !empty($this->request->data)){
$this->Question->create();
$this->Question->save($this->request->data);
}
}

well you have to load your model in your controller in order to create a new record in your DB
your view look like this:
<?php echo $this->Form->create('Question');?>
<td><?php $attributes = array();
$options = array($question['Question']['opt1']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt2']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt3']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
<td><?php $attributes = array();
$options = array($question['Question']['opt4']);
echo $this->Form->radio('Model.'.$i.'.name', $options, $attributes);?> </td>
</tr>
<?php endforeach; ?>
<td><?php echo $this->Form->end(__('Submit')); ?></td>
now in your controller you have to load your model before creating a new record
public function NameOfYourFunction(){
$this->loadModel('Question');
if ($this->request->is('post')) {
if ($this->Question->save($this->request->data)) {
$this->Session->setFlash(__('Information save succesfully.'));
return $this->redirect(array('action' => 'index'));
}
}
}
i hope i helped you

Related

cakephp, link to retrieve status variable

Total n00b in CakePhp
Im trying to link to the view retrieved data from variable $status, defined in the controller, given the simple condition "done" or "pending".
Given the model:
<?php
class Task extends AppModel {
var $name = 'Task';
var $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => 'Title of a task cannot be empty'
)
);
}?>
The Task Controller:
function index($status=null) {
if($status == 'done')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '1')));
else if($status == 'pending')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '0')));
else
$tasks = $this->set('Tasks', $this->Task->find('all'));
$this->set('tasks', $tasks);
$this->set('status', $status);
}
And finally The View:
<h2>Tasks</h2>
<?php if(empty($Tasks)): ?>
There are no tasks in this list
<?php else: ?>
<table>
<tr>
<th>Title</th>
<th>Status</th>
<th>Created</th>
<th>Modified</th>
<th>Actions</th>
</tr>
<?php foreach ($Tasks as $Task): ?>
<tr>
<td>
<?php echo $Task['Task']['title'] ?>
</td>
<td>
<?php
if($Task['Task']['done']) echo "Done";
else echo "Pending";
?>
</td>
<td>
<?php echo $Task['Task']['created'] ?>
</td>
<td>
<?php echo $Task['Task']['modified'] ?>
</td>
<td>
<?php echo $this->Html->link('Edit', array('action'=>'edit', $Task['Task']['id'])); ?>
<?php echo $this->Html->link('Delete', array('action'=>'delete', $Task['Task']['id']), null, 'Are you sure you want to delete this task?'); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index','done')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index', 'pending')); ?><br />
Clicking the "Done Task" returns an empty list (There are no tasks in this list). Can anyone see what is wrong on the link? Thanks in advance!
You are mixing up the casing in the variable name $Tasks. In your example you are assigning the array to $tasks, but then in your view, your are trying to check a variable names $Tasks, which will turn up empty because it has never been properly set.
Change this line:
$this->set('tasks', $tasks);
To:
$this->set('Tasks', $tasks);
As mentioned by McWayWeb above to case of the variable is incorrect.
As you are setting lower case vars to use in your view i would make the change there. Change all counts of Task in the view to task.
I would also consider setting all viewVars at the same time using the below
$this->set(compact('tasks','status));

Edit table-cells in a view

Yii Version 1.1.15
I have a model which contains a serialized array (table) in an attribut. Showing this array as a table in a view works fine.
But I want to update the table-cells and then again save the array serialized in the model. And this does not work. The output stops there (*) without errors.
Here you can see what I did:
$model->table contains a serialized array. The unserialized array looks like this:
array(
array('dog', 'cat', 'cow'),
array('meat', 'milk', 'gras'),
)
I unserialize the table in the controller:
controller: contactController.php
$model = Contact::model()->findByPk((int) $id);
$table = unserialize($model->input)
$this->render('contact_form', array(
'table' => $table));
}
And now I want to show and edit the array $table in a form as a html-table:
view: contact_form.php
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'contact-form',
));
?>
<table border="2">
<?php foreach ($table as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td>
<!-- (*) output stops here, without errors -->
<?= $form->textArea($value, '', array('rows' => 3, 'cols' => 20)); ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
<div class="row buttons">
<?php echo CHtml::submitButton("save"); ?>
</div>
<?php $this->endWidget(); ?>
The output stops before "... textArea ...".
If I only show $table in a view (without a form) it works like a charme:
<table border="2">
<?php foreach ($table as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?= CHtml::encode($value) ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
Hope this helps to help me :-) How can I get this nice idea working?
Actually you are missing the arguments of CactiveForm.textArea(.......)
http://www.yiiframework.com/doc/api/1.1/CActiveForm#textArea-detail
textArea($value, '', array('rows' => 3, 'cols' => 20)); ?>
Instead of it your code should be
textArea($model, $value, array('rows' => 3, 'cols' => 20)); ?>

Cake PHP display field

I have 4 tables named customers, calls, employees and companies. Customers belong to a company and an employee. Calls belong to a customer and an employee. At the moment in my call index file it shows the relevant customer name and employee name. I want to display, for a call the relevant customer name, the customers company name they belong to and the employee name. I don't know how to display the relevant company name based on the customer. Can someone help? This is my code:
call/ index.ctp
<?php
$usertype=$this->SESSION->read('User.usertype');
?>
<div class="calls index">
<h2><?php echo __('Call Details'); ?> </h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Call Time'); ?></th>
<th><?php echo $this->Paginator->sort('Comments'); ?></th>
<th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
<th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php foreach ($calls as $call): ?>
<tr>
<td><?php echo h($call['Call']['id']); ?> </td>
<td><?php echo h($call['Call']['call_date']); ?> </td>
<td><?php echo h($call['Call']['call_time']); ?> </td>
<td><?php echo h($call['Call']['comments']); ?> </td>
<td><?php echo h($call['Call']['next_call_date']); ?> </td>
<td>
<?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
</td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
</td>
</tr>
callsController:
class CallsController extends AppController {
/**
* Components
*
* #var array
*/
public $components = array('Paginator');
/**
* index method
*
* #return void
*/
public function index() {
$this->Call->recursive = 0;
$this->set('calls', $this->Paginator->paginate());
}
/**
* view method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function view($id = null) {
if (!$this->Call->exists($id)) {
throw new NotFoundException(__('Invalid call'));
}
$options = array('conditions' => array('Call.' . $this->Call->primaryKey => $id));
$this->set('call', $this->Call->find('first', $options));
}
/**
* add method
*
* #return void
*/
public function add() {
if ($this->request->is('post')) {
$this->Call->create();
if ($this->Call->save($this->request->data)) {
$this->Session->setFlash(__('The call has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The call could not be saved. Please, try again.'));
}
}
$customers= $this->Call->Customers->find('list',array('order'=>'customer_name ASC','fields'=>array('id','customer_name')));
$employees= $this->Call->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));
$this->set(compact('customers', 'employees'));
}
/**
* edit method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function edit($id = null) {
if (!$this->Call->exists($id)) {
throw new NotFoundException(__('Invalid call'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Call->save($this->request->data)) {
$this->Session->setFlash(__('The call has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The call could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Call.' . $this->Call->primaryKey => $id));
$this->request->data = $this->Call->find('first', $options);
}
$customers= $this->Call->Customers->find('list',array('order'=>'customer_name ASC','fields'=>array('id','customer_name')));
$employees= $this->Call->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));
$this->set(compact('customers', 'employees'));
}
/**
* delete method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function delete($id = null) {
$this->Call->id = $id;
if (!$this->Call->exists()) {
throw new NotFoundException(__('Invalid call'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Call->delete()) {
$this->Session->setFlash(__('The call has been deleted.'));
} else {
$this->Session->setFlash(__('The call could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}}
the array looks like this:
Array
(
[Call] => Array
(
[id] => 7
[call_date] => 2013-04-27
[call_time] => 08:31:00
[comments] => Require installation
[next_call_date] => 2014-04-27
[customers_id] => 2
[employees_id] => 3
)
[Customers] => Array
(
[id] => 2
[customer_name] => Snith Jams
[customer_address] => 192 Waverley Road
[customer_suburb] => Caulfield East
[customer_state] => VIC
[customer_postcode] => 3145
[customer_dob] => 2014-04-09
[customer_anniversary] => 2014-04-10
[customer_phone1] => 0492832921
[customer_phone2] => 0392817894
[customer_phone3] => 0482938281
[customer_fax] =>
[customer_email] => tsmith#hotmail.com
[customer_gender] => M
[customer_type] => Silver
[customer_PW] => a1c680c2bfcca40816dd81eff3980cc9828c9088
[customer_username] => samman
[companies_id] => 3
[employees_id] => 11
)
[Employees] => Array
(
[id] => 3
[employee_name] => Jones
[date_hired] => 2013-02-04
[employee_phone_number] => 0449997582
[employee_email] => indianajones#gmail.com
[employee_address] => 22 Queens street Melbourne CBD
[employee_dob] => 1966-01-31
[access_level] => staff
[employee_username] => jones12
[employee_pw] => 603fce7dcbec3c9cba24e87d058a3341e37779b8
)
)
callsController:
public $components = array('Paginator');
public function index() {
$this->Call->recursive = 2;
$this->set('calls', $this->Paginator->paginate());
}
calls/index.ctp:
<div class="calls index">
<h2><?php echo __('Call Details'); ?> </h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Call Time'); ?></th>
<th><?php echo $this->Paginator->sort('Comments'); ?></th>
<th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
<th><?php echo $this->Paginator->sort('Company Name'); ?></th>
<th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php foreach ($calls as $call): ?>
<tr>
<td><?php echo h($call['Call']['id']); ?> </td>
<td><?php echo h($call['Call']['call_date']); ?> </td>
<td><?php echo h($call['Call']['call_time']); ?> </td>
<td><?php echo h($call['Call']['comments']); ?> </td>
<td><?php echo h($call['Call']['next_call_date']); ?> </td>
<td>
<?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Customers']['Companies']['company_name']); ?>
</td>
<td>
<?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
</td>
<?php echo pr($call); ?>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
just set recursive to 2
$this->Call->recursive = 2;
(or $this->Calls->recursive = 2 it depends if your model is Call or Calls)
and you can access to Company information like this
$call['Customer']['Company']['name']
Because it seems you are not following cake naming conventions (you are using plural for Models' names) maybe you'll need to use
$call['Customers']['Companies']['name']
I suggest you to follow the conventions, anyway if you print your array doing pr($call); you'll see how the array is structured and how to access your data

Implementing pagination in cakephp

I wish to implement pagination in my search results in the view page. Currently in the controller, I limit it to 10 results to view. How do I implement pagination?
In my controller,
var $name = 'Searches';
var $components = array('Auth');
var $uses = array('User','Passion');
$users = $this->User->find('all',array('limit'=>10,'conditions'=>$final_conditions,'fields'=>array('User.*')));
In my view page,
<?php foreach ($search_fields as $user): ?>
<tr>
<?php //debug($search_fields);?>
<td><?php echo $user['User']['firstName']; ?></td>
<td><?php echo $user['User']['lastName']; ?></td>
<td><?php echo $user['User']['email']; ?></td>
<td><?php echo $user['User']['displayName']; ?></td>
<td><?php echo $user['User']['gender']; ?></td>
</tr>
<?php endforeach; ?>
Controller
$this->paginate = array(
'conditions' => $final_conditions,
'limit' => 10
);
$users = $this->paginate('User');
In the view you just use the same as on http://book.cakephp.org/view/166/Pagination-in-Views

Returning extra fields with generatetreelist

I don't know if I'm going the right way with the tree behavior but I'm trying to build a comment system for a blog. I would like to have an indentation of 5 level depth.
The generatetreelist method looks like it would be the fastest way to accomplish this but it doesn't look like you can add fields to the query. Am I right ? Is there a way to modify the method ?
Thanks
This is how I do it for categories. You could modify accordingly.
In controller:
$categories = $this->Category->generatetreelist(null,null,null,"|-- ");
$categories_array = array();
foreach($categories as $k => $v)
{
$categories_array[$k] = $this->Category->find('first', array('conditions' => array('Category.id' => $k)));
$categories_array[$k]["Category"]["path"] = $v;
}
$this->set(compact('categories','categories_array'));
In view:
<table>
<thead>
<tr>
<th><?php __('Id');?></th>
<th><?php __('Name');?></th>
<th><?php __('Status');?></th>
<th><?php __('Action');?></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($categories_array AS $categoryId => $category):
$class = 'even';
if ($i++ % 2 == 0) {
$class = 'odd';
}?>
<tr class="<?php echo $class;?>">
<td><?php echo $category['Category']['id']; ?></td>
<td><?php echo $category["Category"]["path"];?></td>
<?php if ($category['Category']['status'] == '1'){
$published = 'Active';
}else {
$published = 'Inactive';
} ?>
<td><?php echo $published;?></td>
<td>
<?php echo $html->link($html->image("icons/16_icon_view.png"), array('action' => 'view', $category['Category']['id']), array('title'=>'View','escape' => false));?>
<?php echo $html->link($html->image("icons/16_icon_edit.png"), array('action' => 'edit', $category['Category']['id']), array('title'=>'Edit','escape' => false));?>
<?php echo $html->link($html->image("icons/16_icon_delete.png"), array('action' => 'delete', $category['Category']['id']), array('class'=>'delete_trigger','rel'=>'#error','title'=>'Delete','escape' => false));?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
If it was me, I would just do this in the result set. As you get a numeric array in the first dimension of the results you could use that when outputting the data to indent or add a class to your comments as you needed.

Resources