Saturday 21 November 2015

Joomla database escape() and quote() functions

The escape() function is used to escape bad characters in order to protect against SQL injection.

The quote() function is used to quote strings, because different database dialects have different quoting characters.
So depending on the database system you use, Joomla will choose the appropriate quoting characters.

Friday 20 November 2015

To add a filter to Joomla component side bar using options

To add a filter to Joomla component side bar  using options

$db   = JFactory::getDbo();
$query = 'SELECT mycolumn FROM MyTable';
$db->setQuery($query);
$rows = $db->loadObjectList();
$options = array();
foreach ($rows as $row) {
   $options[] = JHtml::_('select.option', "$row->mycolumn", JText::_($row->mycolumn));

}
JHtmlSidebar::addFilter('- Select mycolumn -','mycolumn', JHtml::_('select.options',$options,'value', 'text', $this->state->get('filter.mycolumn'), true));

Monday 2 November 2015

PHP- function for format file size unit

function formatSizeUnits($bytes) {
    if ($bytes >= 1073741824) {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 1) {
        $bytes = $bytes . ' bytes';
    } elseif ($bytes == 1) {
        $bytes = $bytes . ' byte';
    } else {
        $bytes = '0 bytes';
    }
    return $bytes;
}

PHP alternative syntax for some of its control structures: if, while, for, foreach, and switch

for PHP 4, PHP 5

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

<?php 
if ($a == 5): ?>

A is equal to 5
<?php endif; ?>

In the above example, the HTML block "A is equal to 5" is nested within an if statement written in the alternative syntax. The HTML block would be displayed only if $a is equal to 5.

The alternative syntax applies to else and elseif as well. The following is an if structure with elseif and else in the alternative format:

<?php
if ($a == 5):
    echo 
"a equals 5";
    echo 
"...";
elseif (
$a == 6):
    echo 
"a equals 6";
    echo 
"!!!";
else:
    echo 
"a is neither 5 nor 6";
endif;

?>

Note
:  
Mixing syntaxes in the same control block is not supported.