How to get a variable inside view helper $this->view? - zend-framework

I need to run in my zend project, inside a controller something like:
<?php for($i=1; $i<10; $i++){
$this->view->someVariable.$i = $someClassName->someFunction();
} ?>
But it doesn't work. I tried to declare $this->view->someVariable, but it doesn't work any way . Any ideas?

$this->view->someVariable.$i is an expression not a variable so you cant assign a value to it. If $this->view->someVariable is an array you can assign values to its elements like,
<?php
for ($i=1; $i<10; $i++){
$this->view->someVariable[$i] = $someClassName->someFunction();
}
?>

Maybe you need curly braces:
$this->view->{$this->view->someVariable . $i} = $someClassName->function();
although I can't help but wonder what you're trying to achieve. Have you dumped the return value of $someClassName->function()?

Related

HTML table Class codeigniter

In the following code (codeigniter documentation), how can i put an <a href=""> on a specific field? Is it possible?
$this->load->library('table');
$query = $this->db->query('SELECT * FROM my_table');
echo $this->table->generate($query);
Thanks a lot.
The solution is to do a foreach on your dataset and then amend the element using the anchor function which is an HTML helper within CI. Something like this:
foreach ($data as $=>$row){
$data[$k]['field'] = anchor($row['URL'],$row['DisplayText']);
}

Zend Framework - Form Element - Remove ID

Is it possible to remove the 'id' attribute that Zend Framework adds to every Form Element by default?
I've looked at the documentation but don't seem to be able to find an answer to this rather straight forward question.
Possible Solution
Is there any cleaner way to do it other than setOption?
$submit = new Zend_Form_Element_Submit('submit');
$submit->setRequired(FALSE)
->setIgnore(TRUE)
->setDecorators($this->elementDecorators)
->setOptions(array('id' => ''));
A solution would be to override Zend_View_Helper_Form with your own View Helper.
But sincerely, don't take too much attention to this id attribute in your form, you will sooner or later need this id (if you're using Javascript for example) and the performance gain (to render the page) is too tiny to be taken into consideration. It will even be a performance loss since you're going to override a Helper.
If your purpose is different and you want to do that anyway, you need to write you own View Helper as follow:
class My_View_Helper_Form extends Zend_View_Helper_FormElement
{
public function form($name, $attribs = null, $content = false)
{
$info = $this->_getInfo($name, $content, $attribs);
extract($info);
$xhtml = '<form'
. $this->_htmlAttribs($attribs)
. '>';
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
return $xhtml;
}
}
Finally, you simply need to overload the default view helper using a plugin loader. Read the manual for more information about plugin loader.

How to get form element value inside Zend_Form::isValid() method?

I'm looking for best way of getting form element values inside isValid() method.
I had something like this isValid():
public function isValid($data) {
$start = (int)($data['start_hour'] . $data['start_minute']);
$end = (int)($data['end_hour'] . $data['end_minute']);
if ($start >= $end) {
$this->getElement('start_hour')->addError('Start time should be less than end time');
return false;
}
return parent::isValid($data);
}
but problem is that when my data structure changes I have to change validation too.
For example, now values of start_hour, start_minute, etc becomes elements of multidimensional array, and I need edit validation like
public function isValid($data) {
$start = (int)($data['send']['start_hour'] . $data['send']['start_minute']);
$end = (int)($data['send']['end_hour'] . $data['send']['end_minute']);
.......
}
It would be great to get value of element by permanent key (like element name), so my isValid could looks like:
public function isValid($data) {
$start = (int)($this->getElement('start_hour')->getValue() . $this->getElement('start_minute')->getValue());
$end = (int)($this->getElement('end_hour')->getValue() . $this->getElement('end_minute')->getValue());
.......
}
but $this->getElement('start_hour')->getValue() inside validation method return an empty value.
Is this possible to get element value in such way?
Thanks.
Try with $this->getValue('start_hour');
If this code takes place in the isValid() method of the form, then sure you could do it as you have described. It's just that isValid() usually needs some data passed - $form->isValid($_POST), for example - and you just end up ignoring it (assuming the parent is Zend_Form, which has an empty isValid() method; intermediate ancestors could potentially inspect the passed data). I would consider that to be potentially confusing.
Al alternative could be to create a custom validator, attach it to one of the form elements (say, the start_hour elements). The signature for the validator's isValid() can use the optional $context parameter - isValid($value, $context = null). When you call $form->isValid($data), it will pass that $data as the $context to the the element validator. You can then use that $context variable to inspect the other values (start_min, end_hour, end_min, etc).
Try calling
$form->populate($data)
Before calling isValid that way the data will be in your form.
Then $this->getValue('start_hour'); should work from within isValid().
So to be sure:
Somewhere in your code (probably controller) there is somthing like:
$this->view->form = new MyForm();
$this->populate($data); //add this
if($this->view->form->isValid($data)){
//do stuff
}

PHP: Echo variable name of an object?

I want to echo the name of the variable object i'm calling, in this case controller_01.
I'm using get_class, but it wont print the variable name, only the object type :(
<?php
class remoteControl{
private $chip = "Intel64<br />";
public function openCase(){
echo "The controler has a " .get_class($this);
return $this->chip;
}
}
$control_01 = new remoteControl();
echo $control_01-> openCase();
?>
You can't do that just like that. An object can have multiple references, but the object isn't itself aware of those references.
The only thing you could do, is to enumerate through every variable you can find and check if it points to the object. But those references could exists also in arrays, or in properties of other objects.
And your design is really flawed if you need the object to find its reference variables this way.

Zend_Paginator with result of Zend_Db_Table_Abstract

i have many class in model directory that those are extend of Zend_Db_Table_Abstract.
but i need use of zend_paginator and this need to result of Zend_Db_Select !!
so when i use of this code (productCat is a model class)
$productCat = new ProductCat();
$rows = $productCat->FetchOrderByPriority();
// Get a Paginator object using Zend_Paginator's built-in factory.
$paginator = Zend_Paginator::factory($rows);
$this->view->paginator = $paginator;
it don`t work!
it show me this error :
Catchable fatal error: Object of class Zend_Db_Table_Row could not be converted to string in
this is my view code :
<ul><?php foreach ($this->paginator as $item): ?>
<li><?php echo $item; ?></li><?php endforeach; ?></ul>
is there any idea?
Pagination definitely works. The problem is in your view where you're trying to echo $item.
And it obviously doesn't work since Zend_Paginator::factory($rows) has returned a rowset; so when you're iterating over $paginator object, you're getting objects of Zend_Db_Table_Row type, and you simply cannot echo them.
What you're trying to do, I believe, is to echo a particular property of the item object, something like:
echo $item->name;