Tuesday, April 30, 2013

CakePHP get host name


cakephp - get the hostname and document root via env()

The env() function gets the environmental variables and as such we can get the http_host and document_root with its help.
eg env('HTTP_HOST') or env('DOCUMENT_ROOT').

It is advisable to use env() function instead of directly using $_SERVER etc.

Monday, April 29, 2013

Changing default error layout in CakePHP

app/Controller/ErrorsController.php

class ErrorsController extends AppController {
    public $name = 'Errors';

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('error404');
    }

    public function error404() {
        //$this->layout = 'default';
    
}   


Refer -
http://stackoverflow.com/questions/9620363/cakephp-2-0-how-to-make-custom-error-pages

Friday, April 26, 2013

Focus selected option in Drop down box


Show the dropdown

$options=array('M'=>'Male','F'=>'Female');
echo $form->select('gender',$options, array('selected' => 'M'));

Disable Close Button in JQuery UI Modal Box

Script

$("#myDialogID").dialog({ closeOnEscape: false, beforeclose: function (event, ui) { return false; }, dialogClass: "noclose" });


This CSS is to be added newly. In jqueryui.css.


.noclose .ui-dialog-titlebar-close
{
    display:none;
}


Monday, April 15, 2013

Creating an optgroup dynamically


$results = $database->loadObjectList();
        $groups = array();
        foreach ($results as $result) {
            $groups[$result->regionname][$result->locid] = $result->locname;
        }
        foreach($groups as $label => $opt):
        $options[] = JHTML::_('select.optgroup', $label);
        foreach ($opt as $id => $name):
        $options[] = JHTML::_('select.option', $id, $name);
        endforeach;
        $options[] = JHTML::_('select.option', '');
        endforeach;
       
        JHTML::_('select.genericlist', $options, 'locid', 'class="chzn-select" style="width:122px;" tabindex="4"','value', 'text', '', false);
        $select = JHTML::_(
         'select.genericlist', // Because we are creating a 'select' element
         $options,             // The options we created above
         'locid',        // The name your select element should have in your HTML
         'size="1" ',          // Extra parameters to add to your element
         'value',              // The name of the object variable for the option value
         'text',               // The name of the object variable for the option text
         'selected_key',       // The key that is selected (accepts an array or a string)
         false                 // Translate the option results?
        );

   
        print_r($select);

Create OPTGROUP in CakePHP


// Define arrays filled with test data; would normally come from your database
    $cars = array('Ferrari', 'Bugatti', 'Porsche');
    $babes = array('Megan Fox', 'Alyssa Milano', 'Doutzen Kroes');
 
    // Create an empty array to be filled with options
    $options = array();
 
    // Create the initial option
    $options[] = JHTML :: _('select.option', '', '- What do you like most -');
 
    // Open our 'Cars' optgroup
    $options[] = JHTML::_('select.optgroup', 'Cars');
 
    // Loop through the 'Cars' data
    foreach($cars as $key => $text) {
     // Create each option tag within this optgroup
     $options[] = JHTML::_('select.option', $key, $text);
    }
 
    // Use the hack below to close the optgroup
    $options[] = JHTML::_('select.option', '');
 
    // Now open our 'Babes' optgroup
    $options[] = JHTML::_('select.optgroup', 'Babes');
 
    // Loop through the 'Babes' data this time
    foreach($babes as $key => $text) {
     // Create each option tag within this optgroup
     $options[] = JHTML::_('select.option', $key, $text);
    }
 
    // Use the hack below to close this last optgroup
    $options[] = JHTML::_('select.option', '');
 
    // Generate the select element with our parameters
    $select = JHTML::_(
     'select.genericlist', // Because we are creating a 'select' element
     $options,             // The options we created above
     'select_name',        // The name your select element should have in your HTML
     'size="1" ',          // Extra parameters to add to your element
     'value',              // The name of the object variable for the option value
     'text',               // The name of the object variable for the option text
     'selected_key',       // The key that is selected (accepts an array or a string)
     false                 // Translate the option results?
    );
 
echo $select;
    ?>

Creating optgroup in Joomla


// Define arrays filled with test data; would normally come from your database
$cars = array('Ferrari', 'Bugatti', 'Porsche');
$babes = array('Megan Fox', 'Alyssa Milano', 'Doutzen Kroes');

// Create an empty array to be filled with options
$options = array();

// Create the initial option
$options[] = JHTML :: _('select.option', '', '- What do you like most -');

// Open our 'Cars' optgroup
$options[] = JHTML::_('select.optgroup', 'Cars');

// Loop through the 'Cars' data
foreach($cars as $key => $text) {
 // Create each option tag within this optgroup
 $options[] = JHTML::_('select.option', $key, $text);
}

// Use the hack below to close the optgroup
$options[] = JHTML::_('select.option', '');

// Now open our 'Babes' optgroup
$options[] = JHTML::_('select.optgroup', 'Babes');

// Loop through the 'Babes' data this time
foreach($babes as $key => $text) {
 // Create each option tag within this optgroup
 $options[] = JHTML::_('select.option', $key, $text);
}

// Use the hack below to close this last optgroup
$options[] = JHTML::_('select.option', '');

// Generate the select element with our parameters
$select = JHTML::_(
 'select.genericlist', // Because we are creating a 'select' element
 $options,             // The options we created above
 'select_name',        // The name your select element should have in your HTML 
 'size="1" ',          // Extra parameters to add to your element
 'value',              // The name of the object variable for the option value
 'text',               // The name of the object variable for the option text
 'selected_key',       // The key that is selected (accepts an array or a string)
 false                 // Translate the option results?
);
 
// Display our select box
echo $select;

Access Dropdown box selected value

<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
To access value( result will be 2, 3, ...)

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
To access text of option do following
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;