cakephp pagination with count issue - cakephp

I have written below code in my controller action.
$this->paginate['Tag'] = array(
'fields' => array('COUNT(tag_id) AS numbers', 'tag', 'slug'),
'limit' => 50,
'order' => 'numbers desc',
'recursive' => 2,
'group' => array('tag'),
);
$tags = $this->paginate('Tag', array('not' => array('site_id' => NULL), 'tag !=' => ''));
And written below code in the .ctp file.
<div class="row section">
<div class="col col_16 pagination">
<h3>Tags</h3>
<?php
foreach ($tags as $key => $tag) {
echo '<span>' . $this->Html->link($tag['Tag']['tag'] . ' (' . $tag[0]['numbers'] . ')', '/tags/' . $tag['Tag']['slug']) . '</span>';
}
?>
</div>
<div class="col col_16 pagination">
<?php
echo $this->Paginator->first('First', null, null, array('class' => 'disabled'));
echo $this->Paginator->prev('Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next('Next', null, null, array('class' => 'disabled'));
echo $this->Paginator->last('Last', null, null, array('class' => 'disabled'));
?>
</div>
<div class="col col_16 pagination">
<h5 class="margin0Px">
<?php
echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% out of %count% total, starting on %start%, ending on %end%.', true)
));
?>
</h5>
</div>
</div>
Getting false result in view $this->Paginator.
What is the issue here? I am not getting any idea. Pls help.

Does you controller define the helpers variable? If it doesn't the Paginator helper is included by default. If it does you have to remember to include it yourself.
Documentation: http://book.cakephp.org/1.3/en/view/1096/Using-Helpers

Related

CakePHP Html->link and Form->end

I have this link that I use to for my modal
<?php
echo $this->Html->link('Create Schedule', '#', array('id' => 'createSchedule', 'class' => 'btn btn-success', 'role' => 'button', ));
?>
How do I convert it to $this->Form->end() format?
Thank you!
You can do this also
<?php
echo $this->Form->button("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success", "type" => "submit"));
echo $this->Form->end();
?>
or you could also do this
<?php
echo $this->Form->submit("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success"));
echo $this->Form->end();
?>
You can try this
<?php
$options = array(
'label' => 'Create Schedule',
'id' => 'createSchedule',
'class' => 'btn btn-success'
);
echo $this->Form->end($options);
?>

What is cakephp's 'maximum depth reached', I got this error while patination with message 'address not found'

I am new to cakephp and using 2.4 version with bootstrap css.
I have almost finished my project but getting some errors while creating search with pagination.
While pagination in Cakephp, first pages are working fine but last 2-3 pages shows error as the requested address was not found on this server.
tables are below.
universities, courses, semesters, subjects, units, topics
respectively these models having hasMany relationships from left to right.
e.g. universities hasMany courses, courses hasMany semesters.....
my search actions is as below.
public function searchresults() {
if($this->request->is('post')) {
$unid=$this->data['University']['universities'];
$searched = $this->data['University']['searchtxt'];
$this->Session->write('Searched.stext',$searched);
$this->Session->write('Searched.univ',$unid);
}
$unid=$this->Session->read('Searched.univ');
$searched = $this->Session->read('Searched.stext');
//Create Join to find total numbers of records after search
$this->Topic->bindModel(array(
'belongsTo' => array(
'Unit' => array(
'foreignKey' => false,
'type' => 'RIGHT',
'conditions' => array(
'Topic.unit_id = Unit.id' ,
)
),
'Subject' => array(
'foreignKey' => false,
'type' => 'RIGHT',
'conditions' => array(
'Unit.subject_id = Subject.id',
)
),
'Semester' => array(
'foreignKey'=> false,
'type'=>'RIGHT',
'conditions' => array (
'Subject.semester_id = Semester.id'
)
),
'Course' => array(
'foreignKey'=> false,
'type'=>'RIGHT',
'conditions' => array (
'Semester.course_id = Course.id'
)
),
'University' => array(
'foreignKey'=> false,
'type'=>'RIGHT',
'conditions' => array (
'Course.university_id = University.id'
)
)
)
));
$this->Paginator->settings=array(
'limit'=>10,
'page'=>1,
'conditions' => array('or'=>array(
'Topic.name LIKE' => '%'.$searched.'%',
'Topic.description LIKE' => '%'.$searched.'%'
)),
);
$data=$this->paginate('Topic');
//debug($data);
$this->set('searched',$searched);
$this->set('sresults',$data);
$this->set('uni_id',$unid);
}
my view's pagination part code is as below:
<div class="row"><!--Pagination links begins -->
<div class="col-sm-8 col-sm-offset-3">
<!-- Shows the next and previous links -->
<ul class="pagination largescreen">
<li class="disabled-page mobileview">
Page <?php echo $this->Paginator->counter(); ?>
</li>
<?php echo $this->Paginator->prev('«', array('tag'=>'li'), null, array('class' => 'disabled-page','tag'=>'li')); ?>
<!-- Shows the page numbers -->
<?php
echo $this->Paginator->numbers(array(
'tag'=>'li',
'separator'=>'',
'currentClass'=>'active',
'currentTag'=>'a',
'modulus'=>4
));
?>
<?php echo $this->Paginator->next('»', array('tag'=>'li'), null, array('class' => 'disabled-page','tag'=>'li')); ?>
</ul>
</div>
<div>
<ul class="pagination pagination-sm smallscreen" style="overflow:auto;">
<li class="disabled-page mobileview">
Page <?php echo $this->Paginator->counter(); ?>
</li>
<?php echo $this->Paginator->prev('«', array('tag'=>'li'), null, array('class' => 'disabled-page','tag'=>'li')); ?>
<!-- Shows the page numbers -->
<?php
echo $this->Paginator->numbers(array(
'tag'=>'li',
'separator'=>'',
'currentClass'=>'active',
'currentTag'=>'a',
'modulus'=>8
));
?>
<?php echo $this->Paginator->next('»', array('tag'=>'li'), null, array('class' => 'disabled-page','tag'=>'li')); ?>
</ul>
</div>
</div><!--Pagination links ends -->
Please help as i am stuck after searching a lot about this...

render elements in cakephp on home.ctp

I am using cakephp 2.4
in my apps I have a view in CategoriesController namely "Editorial" and I can show all the articles under that category by http://mydomain/categories/editorial
I am trying to show all the articles under "Editorial" category in the home.ctp by echo $this->element('editorials');
but it shows Notice (8): Undefined variable: articles [APP\View\Elements\Editorials.ctp, line 4]
CategoriesController.php
public function Editorial() {
$category = $this->Category->find('first', array(
'conditions' => array(
'Category.id' => 1
)
));
$this->set(compact('category'));
$this->Paginator = $this->Components->load('Paginator');
$this->Paginator->settings = array(
'Article' => array(
'recursive' => -1,
'contain' => array(
'Category'
),
'limit' => 5,
'conditions' => array(
'Article.category_id' => $category['Category']['id'],
),
'order' => array(
'Article.id' => 'ASC'
),
'paramType' => 'querystring',
)
);
$articles = $this->Paginator->paginate($this->Category->Article);
$this->set(compact('articles'));
}
View file:
<?php if (!empty($articles)): ?>
<?php echo $this->element('editorials'); ?>
<?php echo $this->element('pagination-counter'); ?>
<?php echo $this->element('pagination'); ?>
<?php endif; ?>
Elements/Editorials.ctp:
<div class="row">
<?php
$i = 0;
foreach ($articles as $art):
$i++;
if (($i % 4) == 0) { echo "\n<div class=\"row\">\n\n";}
?>
<div class="col col-sm-3">
<?php echo $this->Html->link($art['Article']['title'], array('controller' => 'articles', 'action' => 'view', 'id' => $art['Article']['id'])); ?>
<br />
</div>
<?php
if (($i % 4) == 0) { echo "\n</div>\n\n";}
endforeach;
?>
<br />
<br />
</div>
try to add $this->render('file'); in your Editorial action and replace file with view file name without.ctp
Something you can change:
1. In controller:
$this->set(compact('category', 'articles')); // one set()
2. In view: (Main Point)
<?php echo $this->element('editorials', array('articles' => $articles)); ?>
Here, you need to pass the articles variable to element like above, by default an element don't recognize the viewVars.

How to create link make cakephp with twitter bootstrap

example:
<a class="btn btn-mini" href="#"><i class="icon-star"></i> Star</a>
i try by Html::Helpers like
echo $this->Html->link($this->Html->tag('i','',
array('class' => 'btn')), array('action' => '../link'));
but it not work !
try:
echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'icon-star')) . " Star",
array('action' => 'your_action'),
array('class' => 'btn btn-mini', 'escape' => false)
);

Form checkbox and label with CakePHP and Bootstrap

I'm having some issues trying to get checkbox inputs and labels to output the correct HTML using CakePHP and Twitter Bootstrap.
The Bootstrap specific output should be:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Keep me logged in
</label>
</div>
</div>
However, using the inputDefaults mentioned here (http://stackoverflow.com/a/9496242/1247225), this Cake form input:
echo $form->input('auto_login', array(
'type' => 'checkbox',
'label' => __('Keep me logged in', true)));
Outputs this:
<div class="control-group">
<input type="hidden" name="data[User][auto_login]" id="UserAutoLogin_" value="0" />
<input type="checkbox" name="data[User][auto_login]" class="" value="1" id="UserAutoLogin" />
<div class="controls">
<label for="UserAutoLogin">Keep me logged in</label>
</div>
</div>
Any ideas how to adjust this individual input so it outputs the correct Bootstrap HTML, like above?
The best way to control the form output, or the format of the output, is by passing a 'format' argument. Try playing with this:
'format' => array('before', 'label', 'input', 'between', 'after', 'error')
I don't think it is document, but by reading the code, you can find it in there.
the prettiest way to do it is this:
<?php
echo '<div class="col-lg-2">';
echo $this->Form->input('Content.checkbox', array(
'div' => array(
'class' => 'input-group',
),
'label' => false,
'type' => 'checkbox',
'before' => '<span class="input-group-addon">',
'after' => '</span><span class="input-group-addon"><label for="ContentCheckbox">What does the fox say?</label></span>',
));
echo '</div>';
I using :
<?php
echo $this->Form->input('coupDeCoeur',
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>
You need to build the form widget manually for checkboxes instead of using FormHelper::input.
For example:
echo '<div class="control-group">';
echo $this->Form->label('Model.field', null, array('class' => 'control-label'));
echo '<div class="controls">';
echo $this->Form->checkbox('Model.field');
echo '</div>';
echo '</div>';
Try following code:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<?php echo $this->Form->input('auto_login', array('type'=>'checkbox','label' => false, 'div' => false,'class'=>false,'after'=>__('Keep me logged in')));?>
</label>
</div>
</div>
If you're using security component, you may have problems if you create your inputs without FormHelper::input. If that's the case, you should try with this:
echo "
<div class='control-group'>
<div class='controls'>
<label class='checkbox'>";
echo $this->Form->input('Model.field', array('label' => false, 'after' => 'Model.field'))."
</label>
</div>
</div>";
Here is the ready-to-use solution:
App::uses('FormHelper', 'View/Helper');
class InputHelper extends FormHelper {
// var $helpers = array('Form', 'Html');
public function create($model, $options = array()) {
$options['class'] = (isset($options['class']) && $options['class']) ? $options['class'] : 'form-horizontal table';
$options['inputDefaults'] = (isset($options['inputDefaults']) && $options['inputDefaults']) ? $options['inputDefaults'] : array(
'div' => 'control-group',
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>'
);
return parent::create($model, $options);
}
public function input($fieldName, $options = array()) {
$this->setEntity($fieldName);
$options = $this->_parseOptions($options);
if ($options['type'] == 'checkbox') {
$options['format'] = array('before', 'label', 'between', 'input', 'after', 'error');
}
fdebug($options);
return parent::input($fieldName, $options);
}
}
If you want a "one line" answer i got it:
echo $this->Form->input('Model.Field', array('class'=>'form-inline', 'after'=>'</br>'));
In my case, I used AdminLTE template with Bootstrap 3 and this code was working for me:
<?php
echo $this->Form->input('Model.fieldname', array(
'label' => false,
'type' => 'checkbox',
'before' => '<label>',
'after' => 'Field name label here</label>',
));
?>
Good luck here!!!
Here's a simple solution that works for me:
The CSS (LESS):
input.form-control[type="checkbox"] {
width: auto;
height: auto;
margin-right: 5px;
display: inline;
}
Then use the Form helper:
echo $this->Form->input(
'field_name',
array(
'div' => 'form-group',
'class' => 'form-control',
'type' => 'checkbox',
'label' => array(
'text' => 'Your label',
'class' => 'label-checkbox'
),
'format' => array('input', 'label')
)
);

Resources