Inserting a text string or paragraph into Zend_Form

If you want to add text after element in form just use description of form element.

textElement = new Zend_Form_Element('someElement'); 
$textElement->setDescription('TEXT'); 

or in the form file:

        $this->addElement(
            'text',
            'some element',
            array(
                'label'         => 'Some element:',
                'description'   => 'TEXT',
            )
        );

If you are wanting something more complex you must define your own decorator.

Posted in Php, Zend Framework at September 27th, 2012. No Comments.

Zend framework MySQL query random records

Hello, sometimes we need a query which will return random rows from table. In your model create this:

    public static function getRandom()
    {
        $model = new self();
        return $model->fetchAll($model->select()
            ->order('RAND()')
            ->limit(8)
            ->from($model)
        );
    }

This will return 8 random rows from the table.
The problem with RAND() function is performing slowly. It needs to assign a random id to each row in database table and find a Unique row. For a small amount of rows will be OK to use RAND() function.
And this work only for MySQL.

That’s all for me. Good luck.

Posted in MySQL, Php, Zend Framework at March 28th, 2012. No Comments.

Assigning multi options to drop down in Zend framework

Today I will show you how to add multi options to select tag. It is simple, just add this code to your controller.

    public function testAction() {
        //we are creating Zend_Form instance
        $dropdown = new Zend_Form_Element_Select("dropdown");
        //set label for select tag
        $dropdown->setLabel("Option:");
        //set options
        $dropdown->setMultiOptions(array(
            "Select 1", "Select 2", "Select 3", "Select 4"
        ));
        //add form to the view
        $this->view->form = $form->addElement($dropdown);
    }

And in the view of the controller, show the form.

echo $this->form;

If you check the code, you will see this:

<select name="dropdown" id="dropdown">
    <option value="0" label="Select 1">Select 1</option>
    <option value="1" label="Select 2">Select 2</option>
    <option value="2" label="Select 3">Select 3</option>
    <option value="3" label="Select 4">Select 4</option>
</select>

If you want to specify the values of option tags then use this array:

array(
“1” => “Select 1”,
“2” => “Select 2”,
“3” => “Select 3”,
“4” => “Select 4”
)

And the result will be:

<select name="dropdown" id="dropdown">
    <option value="1" label="Select 1">Select 1</option>
    <option value="2" label="Select 2">Select 2</option>
    <option value="3" label="Select 3">Select 3</option>
    <option value="4" label="Select 4">Select 4</option>
</select>

And that’s all. Good luck.

Posted in Php, Zend Framework at October 15th, 2011. No Comments.

How to set page titles in Zend Framework

Welcome, today I will show you how to set page title on page in Zend Framework.
In Zend Framework, you can set the page title in the controller, like in the example below:

<?php
class Panel_TestsController extends Zend_Controller_Action
{
  public function testAction() {
    //  setting the page title
    $this->_helper->layout()->getView()->headTitle('View all students');
  }
}

or in the view:

/**
 * views/scripts/tests/index.phtml
 */
$this->headTitle('View all students');

And that’s all. Good luck

Posted in Php, Zend Framework at September 17th, 2011. 3 Comments.

How can I get the current module, controller and action in Zend Framework?

Today I will show you how to get current module, controller and action name in Zend Framework. In the default routing, both the module, controller and the action are always passed along with the request. And the code to retrieve them is very simple:

$moduleName = $this->getRequest()->getModuleName();
$controllerName = $this->getRequest()->getControllerName();
$actionName = $this->getRequest()->getActionName();

And that’s all. Good luck.

Posted in Php, Zend Framework at July 27th, 2011. No Comments.