render elements in cakephp on home.ctp - cakephp

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.

Related

Cakephp 3 Three Column View based on an Id

This is Cakephp 3.5, So I have three businesses 1, 2, 3. I have a view to show Estimated Revenue From Fees.
On the index page I have a three column Div
<div class="row">
<div class="col-md-12">
<h2><?= __('Estimated Revenue From Fees') ?></h2>
<div class="row">
<div class="column">
<h4>Business One</h4>
<ul>
<?php foreach ($estimatedRevenueFromCFeesBiz1 as $estimatedRevenueFromFeeBiz1): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeBiz1->year), ['action' => 'view', $estimatedRevenueFromFeeBiz1->id]) ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="column">
<h4>Business Two</h4>
<?php foreach ($estimatedRevenueFromFeesNauBiz2 as $estimatedRevenueFromFeeBiz2): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeNauBiz2->year), ['action' => 'view', $estimatedRevenueFromFeeBiz2->id]) ?>
</li>
<?php endforeach; ?>
</div>
<div class="column">
<h4>Business Three</h4>
<?php foreach ($estimatedRevenueFromCourseFeesUa as $estimatedRevenueFromCourseFeeUa): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeBiz3->year), ['action' => 'view', $estimatedRevenueFromFeeBiz3->id]) ?>
</li>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
In my controller I've set up like this:
public function index()
{
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFeesBiz1->find('list', array( 'conditions' => 'business_id' == 1));
$estimatedRevenueFromFeesBiz2 = $this->EstimatedRevenueFromFeesBiz2->find('list', array( 'conditions' => 'business_id' == 2));
$estimatedRevenueFeesBiz3 = $this->EstimatedRevenueFromFeesBiz3->find('list', array( 'conditions' => 'business_id' == 1));
$this->set(compact('estimatedRevenueFromFeesBiz1', 'estimatedRevenueFromFeesBiz2', 'estimatedRevenueFromFeesBiz3'));
}
I'm getting a Call to a member function find() on boolean
Error in: ROOT\src\Controller\EstimatedRevenueFromCourseFeesController.php, line 26 which is the
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFeesBiz1->find('list', array( 'conditions' => 'business_id' == 1)); line.
I figured it out. I changed the controller back to just calling the whole table:
public function index()
{
$this->paginate = [
'contain' => ['Businesses']
];
$estimatedRevenueFromFees = $this->paginate($this->EstimatedRevenueFromFees);
$this->set(compact('estimatedRevenueFromFees'));
}
I then added a condition into the index.ctp file:
<h4>Business One</h4>
<ul>
<?php foreach ($estimatedRevenueFromFees as $estimatedRevenueFromFee): ?>
<?php if ($estimatedRevenueFromFee['business_id'] == 1): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFee->year), ['action' => 'view', $estimatedRevenueFromFee->id]) ?>
</li>
<?php else: ?>
<?php continue; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Well, based on what you said and on your own answer, it looks like what you were trying to do was this:
public function index() {
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 1]]);
$estimatedRevenueFromFeesBiz2 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 2]]);
$estimatedRevenueFeesBiz3 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 1]]);
$this->set(compact('estimatedRevenueFromFeesBiz1', 'estimatedRevenueFromFeesBiz2', 'estimatedRevenueFromFeesBiz3'));
}
What changes is your condition (business_id). But you are always querying over the same table (EstimatedRevenueFromFees).
Bare in mind that in your solution you are paginating. So you might end up having every record in a page with a certain business_id, or whatever combination.

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...

Cakephp 2.1 - AjaxHelper

I am using latest(2.1.1)version of cakePhp.
I was trying to implement ajaxHlper(http://www.cakephp.4uk.pl/) . but stuck in between.
<head>
<script type="text/javascript">
$(document).ready(function(){
$("#EmployeeAddForm").validate();
});
</script>
</head>
<div class="employees form">
<?php echo $this->Form->create('Employee');?>
<fieldset>
<legend><?php echo __('Add Employee'); ?></legend>
<?php
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
//echo $this->Form->input('age');
echo $this->Form->input('age', array('class' => 'required number'));
echo $this->Form->input('sex');
echo $this->Form->input('Adress.first_line');
echo $this->Form->input('Adress.second_line');
echo $this->Form->input('Adress.city');
echo $this->Form->input('Adress.state');
echo $ajax->autoComplete('Department.name', '/ajax/autoComplete')
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Employees'), array('action' => 'index'));?></li>
<li><?php echo $this->Html->link(__('List Adresses'), array('controller' => 'adresses', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Adress'), array('controller' => 'adresses', 'action' => 'add')); ?> </li>
</ul>
This is my add.ctp file where Department.name I am trying to autocomplete using ajaxHelper.
In my EmployeesController.php I have autocomplete function which is like
function autoComplete() {
echo $this->params['url']['q'] . "---";
$this->loadModel("Department");
$this->set('departments', $this->Department->find('all', array(
'conditions' => array(
'Department.name LIKE ' => '%'.$this->params['url']['q'].'%'
),
'limit' => $this->params['url']['limit'],
'fields' => array('name')
)));
$this->layout = 'ajax';
}
It's not working. What could be the mistake I am doing?
it gives me following error:
Notice (8): Undefined variable: ajax [APP\View\Employees\add.ctp, line 84]
Fatal error: Call to a member function autoComplete() on a non-object in C:\xampplite\htdocs\cakephp\app\View\Employees\add.ctp on line 84
Assuming you have added the helper to the $helpers array of your controller, you have to access the helper with $this->Ajax->autoComplete() instead of $ajax->autoComplete().

cakephp pagination with count issue

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

Resources