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
Related
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)); ?>
i have two tables with the following columns, guardians(id,student_no) and student(id,admission_no). student_no is foreign to admision_no. students has a hasMany association with guardians, guardians has a belongsTo association with students.
here are my models
STUDENT
public $hasMany = array(
'Guardian' => array(
'className' => 'Guardian',
'foreignKey' => 'student_no',
'dependent' => true,
)
)
GUARDIAN
public $belongsTo = array(
'Student' => array(
'className' => 'Student',
'foreignKey' => 'student_no',
)
)
Guardian controller
public function view($id = null) {
if (!$this->Guardian->exists($id)) {
throw new NotFoundException(__('Invalid guardian'));
}
$options = array('conditions' => array('Guardian.' . $this->Guardian->primaryKey => $id));
$this->set('guardian', $this->Guardian->find('first', $options));
}
Guardian view.ctp
(truncated to view only the associated student from within the Guardian model)
<h3><?php echo __('Associated Students'); ?></h3>
<?php if (!empty($guardian['Student'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Admission No'); ?></th>
<th><?php echo __('First Name'); ?></th>
<th><?php echo __('Last Name'); ?></th>
<th><?php echo __('Gender'); ?></th>
<th><?php echo __('Date Of Birth'); ?></th>
<th><?php echo __('Join Date'); ?></th>
<th><?php echo __('Form'); ?></th>
<th><?php echo __('Student Class'); ?></th>
<th><?php echo __('Middle Name'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($guardian['Student'] as $student): ?>
<tr>`
<td><?php echo $student['admission_no']; ?></td>
line(115) <td><?php echo $student['first_name']; ?></td>
<td><?php echo $student['last_name']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['date_of_birth']; ?></td>
<td><?php echo $student['join_date']; ?></td>
<td><?php echo $student['form']; ?></td>
<td><?php echo $student['student_class']; ?></td>
<td><?php echo $student['middle_name']; ?></td>
</tr>
<?php endforeach; ?>
</table>
i can view associated parent details from students view with above similar code,
however in guardians view i get error for the associated guardian
ERROR:Warning (2): Illegal string offset 'first_name' [APP/View/Guardians/view.ctp, line 115]
and for three lines below it.what exactly is going wrong
Do some debugging: debug($guardian)
In your belongsTo assocaition, a guardian can only have a single student, so you are iterating over the columns of that one single student, ie consequently the $student variable will be a string, hence the error.
See also Cookbook > Models > Associations > belongsTo
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
I want to paginate in view for a custom array. My controller and view code is given below.
Controller Code:
public function admin_detail(){
$totalByDate = $this->Pbscodemetric->find('all',array('fields'=>array('created')));
$createdDate = Set::extract('/Pbscodemetric/created', $totalByDate);
$uniqueCreatedDate = array_unique($createdDate);
$finalArray = array();
foreach($uniqueCreatedDate as $created){
$downloadsArr = $this->Pbscodemetric->find('all',array('fields'=>array('downloads_iphone','downloads_ipad','downloads_android'),'conditions'=>array('created'=>$created)));
$download_iphone = array_sum(Set::extract('/Pbscodemetric/downloads_iphone',$downloadsArr));
$download_ipad = array_sum(Set::extract('/Pbscodemetric/downloads_ipad',$downloadsArr));
$downloads_android = array_sum(Set::extract('/Pbscodemetric/downloads_android',$downloadsArr));
$finalArray[$created] = array(
'downloads_iphone' => $download_iphone,
'downloads_ipad' => $download_ipad,
'downloads_android' => $downloads_android
);
}
$this->set('finalArray',$finalArray);
}
View Code:
<div class="pbscodemetrics index">
<h2><?php echo __('Pbscodemetrics List Detail'); ?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th>Date</th>
<th>iPhone</th>
<th>iPad</th>
<th>Android</th>
</tr>
<?php
foreach($finalArray as $key => $final){?>
<tr>
<td><?php echo $key;?></td>
<td><?php echo $final['downloads_iphone'];?></td>
<td><?php echo $final['downloads_ipad'];?></td>
<td><?php echo $final['downloads_android'];?></td>
</tr>
<?php }?>
</table>
</div>
<div class="actions">
<?php echo $this->element('nav');?>
</div>
Now I want to set pagination. I know in CakePHP default pagination needs Model name. But How can I do that for the above code?
Can anyone help me please..
You could implement a custom pagination, see Custom Query Pagination in the Cookbook. However, it looks like what you're doing there could be done using grouping and the SQL SUM function, that way you could then simply use the built-in pagination. Example:
$alias = $this->Pbscodemetric->alias;
$this->Pbscodemetric->virtualFields = array
(
'downloads_iphone_sum' => sprintf('SUM(`%s.downloads_iphone`)', $alias),
'downloads_ipad_sum' => sprintf('SUM(`%s.downloads_ipad`)', $alias),
'downloads_android_sum' => sprintf('SUM(`%s.downloads_android`)', $alias)
);
$this->paginate = array
(
'fields' => array
(
'downloads_iphone_sum',
'downloads_ipad_sum',
'downloads_android_sum',
'created'
),
'group' => 'created',
'order' => array
(
'created' => 'desc'
),
'limit' => 10
);
$data = $this->paginate($alias);
This will give you Paginator helper compatible results with summed downloads_iphone, downloads_ipad and downloads_android columns, grouped by the created column. It uses virtual fields in order to retreive results in the default CakePHP style, ie they will be accessible like this:
<?php foreach($results as $result): ?>
<tr>
<td><?php echo $result['Pbscodemetric']['created'];?></td>
<td><?php echo $result['Pbscodemetric']['downloads_iphone_sum'];?></td>
<td><?php echo $result['Pbscodemetric']['downloads_ipad_sum'];?></td>
<td><?php echo $result['Pbscodemetric']['downloads_android_sum'];?></td>
</tr>
<?php endforeach; ?>
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.