I trying to pass a variable in cakephp to a view and getting error Undefined variable: view [APP\View\ItQueries\add.ctp, line 9] and line 9 is this
<?php echo $this->Form->hidden('status_type', array('value'=>$view)); ?>
Here is how im defining my variable in the controller
class ItQueriesController extends AppController {
var $view = 'Open';
public function index() {
$this->ItQuery->recursive = 0;
$this->set('view', $this->view);
}
//Other Code
}
and here is where im trying to pass the variable as a hidden field
<?php echo $this->Form->create('ItQuery'); ?>
<?php echo __('Add It Query'); ?></legend>
<?php
echo $this->Form->input('status_type', array('type' => 'hidden', 'value'=>$view));
?>
<?php echo $this->Form->end(__('Submit')); ?>
Can some please show me how to fix this
You need to set the variable as part of the viewVars.
To do this add this to your controller action:
$this->set('view', $this->view);
E.g.
class ItQueriesController extends AppController {
var $view = 'Open';
function index() {
$this->set('view', $this->view);
}
}
You can then access it in the view directly using $view
Your hidden field would look like:
echo $this->Form->input('status_type', array('type' => 'hidden', 'value'=>$view));
Related
I have created a collection.php model and added a addOrderFilter() function, but when I try to call addOrderFilter() in my phtml file, it gives me
Fatal error: Call to a member function addOrderFilter() on boolean in [...]\magento\app\design\adminhtml\default\default\template\paketid\shipping.phtml on line 5
This is my shipping.phtml :
<?php echo $this->getChildHtml('PaketId_Shipping');?>
<h1>Test custom block</h1>
<?php $order = $this->getOrder()?>
<?php $shipping = Mage::getModel('paketid_shipping/result')->getCollection()->addOrderFilter($order) ?>
<?php if(count($shipping)):
foreach($shipping as $shipping): ?>
<?php echo $this->__('Booking Code') ;?>
<?php echo $shipping->getBookingCode(); ?>
<?php endforeach; ?>
<?php endif; ?>
My collection.php model :
class PaketId_Shipping_Model_Resource_Result_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
protected $order;
protected function _construct()
{
$this->_init('paketid_shipping/result');
}
public function addOrderFilter($order)
{
if ($order instanceof Mage_Sales_Model_Order) {
$order = (int) $order->getId();
}
if (!is_array($order)) {
$order = array($order);
}
$this->getSelect()->where("main_table.order_id IN (?)", $order);
//$this->getSelect()->where("main_table.order_id IN (?)", $order)->order('id DESC');
return $this;
}
The confusing part is, why does my phtml doesn't render my collection model ? Am I doing something wrong ?
Make sure magento is not running in compilation mode.
OR
There is something wrong in syntax Mage::getModel('paketid_shipping/result')->getCollection() . This is not returning correct collection object. Try debugging whether it returns collection object or not.
OR
You can try different similar syntax
Mage::getResourceModel('paketid_shipping/result_collection')->addOrderFilter($order);
home.ctp
<?php
echo $this->element('distromob/featured');
?>
WebsitesController.php
<?php
class WebsitesController extends AppController {
public $components = array('Paginator');
public function index(){
$images = $this->paginate('Website');
if (isset($this->params['requested'])) {
return $images;
} else {
$this->set('images', $images);
}
}
featured.ctp
<?php
$images = $this->requestAction('/Websites/index');
?>
<ul>
<?php
foreach($images as $image): ?>
<?php $domain = $image['Website']['domain'];?>
<li><?php echo $this->Html->image('websites/' . $image['Website']['image'],array('width'=>'234px','height' =>'208px','class' => 'random'));
?>
</li>
<?php endforeach;?>
</ul>
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php $this->Paginator->counter(); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
AppController.php
class AppController extends Controller {
public function beforeFilter(){
$this->Paginator->settings=array(
'limit'=>4
);
}
}
Im new to cakephp I found some tutorial on the web but it seems not fit on my needs. My question was, why is it the previous and the next pagination data is not clickable, It seems that the pagination data is base on the limit i set on
public function beforeFilter(){
$this->Paginator->settings=array(
'limit'=>4
);
}
whenever i change the limit it will also display data but i cannot click the next and the previous
make the pagination data available in the element i.e $this->params['paging']
//index method
if ($this->params['requested'])
return array('images'=>$this->paginate('WebSite'), 'paging' => $this->params['paging']);
$this->set('images', $this->paginate('WebSite') );
then in your home.ctp do this
$images = $this->requestAction(array('controller'=>'websites','action'=>'index'));
// if the 'paging' variable is populated, merge it with the already present paging variable in $this->params. This will make sure the PaginatorHelper works
if(!isset($this->params['paging'])) $this->params['paging'] = array();
$this->params['paging'] = Hash::merge( $this->params['paging'] , $images['paging'] );
Try replacing ->counter() with ->numbers() to see if you have any page numbers
I'd like to make website, with news and comments attached to them.
I made Model Infos.php ( connected to news on my page) and Model Infos_coms.php ( where comment should be saved for each news). For each Model I've got controlleres as follows
InfosController.php
class InfosController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('inform', $this->Info->find('all'));
//$this->loadModel('Infos_com');
//$this->set('com', $this->Infos_com->find('all'));
}}
, Info_comsController.php.
<?php
class Infos_comsController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('com', $this->Infos_com->find('all'));
}}
and there is my problem, couse i don't know how to display both tables (news and their comments on one page)
here is my index, i've red twice cookbook chapter about view and i didn't find there answer for solving my problem.
here is my Index file in folder (View/Infos)
<body>
<?php echo $this->Html->link(__('Dodaj newsa',true),array('action'=>'add')); ?>
<div class="container">
<?php
foreach ($inform as $news) : ?>
<h3>
<?php echo $news['Info']['title']; ?>
</h3>
<p>
<?php echo $news['Info']['body']; ?>
</p>
<small>
<?php echo $news['Info']['created']; ?>
</small>
<small>IP:
<?php echo $news['Info']['ip']; ?>
</small>
<!-- existing comments -->
<?php foreach ($com as $comment): ?>
<h4>
<?php echo $comment['Infos_com']['body']; ?>
</h4>
<small>
<?php echo $comment['Infos_com']['created']; ?><br>
<?php echo $comment['Infos_com']['ip']; ?>
</small>
<!-- adding comments -->
<div class="form-group">
<?php echo $this->Form->create('Infos_com'); ?>
<?php echo $number = $comment['Info']['id']; ?>
<?php echo $this->Form->input(__('mail',true),array('class'=>'form-control')) ?>
<?php echo $this->Form->input(__('Comment body',true), array('class'=>'form-control')); ?>
<?php echo $this->Form->submit(__('Dodaj komentarz',true),array('class'=>'btn btn-info')); ?>
<?php echo $this->Form->end(); ?>
</div>
<?php endforeach; ?>
<?php endforeach; unset($news); unset($comment);?>
i will be gratefull for any tip
Edit.
I followed Guillemo Mansilla suggest, but now i got issue with database. I've used Guillemo Mansilla code, with changed names, and also adde something i think correctly. There are my Modal
Info.php
<?php
class Info extends AppModel
{
public $hasMany = array('Infos_com');
public $validate = array(
'title'=>array(
'rule'=>'notEmpty'
),
'body'=>array(
'rule'=>'notEmpty'
)
);
}
?>
Infos_com.php
<?php
class Infos_com extends AppModel
{
public $belongsTo = array('Info');
public $validate = array(
'mail'=>array(
'requierd'=>array(
'rule'=>'notEmpty',
'message'=>'Write your email'
)
),
'body'=>array(
'rule'=>'notEmpty',
'message'=>'Write a comment'
)
);
}
?>
i changed my index.ctp inside body part to
<?php if (isset($inform)) {
foreach($inform as $info) {
echo $info['Info']['title'];
foreach($info['Infos_com'] as $comment) {
echo $comment['Infos_com']['body'];
}
}
} ?>
now i'm getting error
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Infos_com.info_id' in 'field list'
SQL Query: SELECT Infos_com.id, Infos_com.id_infos, Infos_com.username, Infos_com.mail, Infos_com.ip, Infos_com.created, Infos_com.body, Infos_com.info_id FROM blogdb.infos_coms AS Infos_com WHERE Infos_com.info_id IN (1, 2, 3, 4, 5)
i have both tables so i don't understand where is mistake.
I will assume that your associations are correct, that is, Info hasMany Infos_com, so in your Info Model you have something like
public $hasMany => array('Infos_com')
I am going to assume that both models are correctly created (You didn't paste your models)
Now, in your controller InfosController, just do this
$this->Info->recursive = 1;
$this->set('inform', $this->Info->find('all'));
In your view, you will have all "infos" along with its comments in a var named $inform, just iterate over it recursively
if (isset($inform)) {
foreach($inform as $info) {
echo $info['Info']['title']; //I dont even know if you have a field named "title"
foreach($info['Infos_com'] as $comment) {
echo $comment['Infos_com']['comment'];
}
}
}
bear in mind that this piece of code probably don't work because we use different names, just adapt it to what you have in there
I finnaly make it , after all night !!!
i Changed column name in my Infos_com from id_infos to info_id
next i edit my index code to
<?php if (isset($inform)) {
foreach($inform as $info) {
echo $info['Info']['title']; echo '<br>';
echo $info['Info']['body']; echo '<br>';
foreach($info['Infos_com'] as $comment) {
echo $comment['body']; echo '<br>';
echo $comment['mail']; echo '<br>';
}
}
} ?>
and finnaly i got my comment under my news, THANK YOU !
i am using linux-mint os
i have to put a search form on front-end on my web's home page:
i have done something like this:
Model for my app:
var/www/project/app/model/search.php
<?php
class Search extends AppModel {
var $name = 'Search';
}
?>
My controller :
var/www/project/app/controller/searches_controller.php
<?php
class SearchesController extends AppController {
var $name = 'Searches';
var $helpers = array('Html', 'Form');
function search()
{
$this->layout = 'default';
return $searches = $this->Search->find('all');
}
}
?>
After following the above steps i have create an Element to show the form on my home page
My Element :
var/www/project/app/views/element/search.ctp
<?php
$searches = $this->requestAction('searches/search');
?>
<?php
echo $form->create('Search', array('controller'=>'searches',
'action' => 'search'));?>
<select name="search">
<option>------Select------</option>
<?php foreach($searches as $search) { ?>
<option value="<?php echo $search['Search']['id'];?>">
<?php echo $search['Search']['media'];?></option>
<?php } ?>
</select>
<?php echo $form->end('submit'); ?>
And i put code in my default.ctp like this :
<?php echo $this->element('search');?>
Having done all this, i tried to view my home page but it shows an error message:
Fatal error: Call to a member function create() on a non-object in
/var/www/Emedia/app/views/elements/search.ctp on line 4
Notice (8): Undefined variable: form [APP/views/elements/search.ctp, line 4]
The form should be:
//in your element
//remove echo $element->create... and use
echo $form->create('Search', array(
'id' => 'someform',
'url' => array(
'controller' => 'searches',
'action' => 'search')
)
);
//and form end
echo $form->end('submit'); //not $element->end('submit');
there shouldnt be any "return" - why are you doing that?
use "cake bake" if you are not yet familiar with how it works.
anyway, that should help:
$searches = $this->Search->find('all');
$this->set(compact('searches'));
If you are using CakePHP 2.x then I thinks you cant access FormHelper directly through variable $form
Try:
$this->Form->create('Search',array('action'=>'yourAction'));
echo $form->create('Search', array('controller'=>'searches',
'action' => 'search'));?>
instead of using :
echo $this->Form->create('Search', array('controller'=>'searches',
'action' => 'search'));?>
it will Resolve :
Fatal error: Call to a member function create() on a non-object in
/var/www/Emedia/app/views/elements/search.ctp on line 4
My error code is:
Notice: Undefined variable: form in
c:\AppServ\www\applogic\app\views\users\index.ctp on line 1
Fatal error: Call to a member function create() on a non-object in
c:\AppServ\www\applogic\app\views\users\index.ctp on line 1)))
(index.ctp)
<?php echo $form->create(null, array('action' => 'index'));?>
<fieldset>
<legend>Enter Your Name</legend>
<?php echo $form->input('name'); ?>
</fieldset>
<?php echo $form->end('Go');?>
(users_controller.php)
<?php
class UsersController extends AppController {
var $name = 'Users';
var $uses = array();
function index() {
if (!empty($this->data)) {
//data posted
echo $this->data['name'];
$this->autoRender = false;
}
}
}
?>
Did you set the $helpers in app_controller or users_controller? You need to include 'Form' in it.
If you are using 2.0, I think you need to use $this->Html (not $html)