Configurate PHP Errors by htaccess

Disable all php errors by .htaccess file ↓

php_value error_reporting 0
php_flag display_errors off
php_flag html_errors off
php_flag log_errors off
php_flag ignore_repeated_errors off
php_flag ignore_repeated_source off
php_flag display_startup_errors off
php_flag report_memleaks off
php_flag track_errors off
php_value docref_root 0
php_value docref_ext 0
php_value log_errors_max_len 0

Also, you can set the PHP error logging in custom log file

php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/custom_error_file.log

How to create/use Joomla! editor? JFactory::getEditor()

Example (short version in J!3.x)

$default_value = JFactory::getApplication()->input->get('your_field_name','');
$editor = JFactory::getConfig()->get('editor');
$editor = JEditor::getInstance($editor);
echo $editor->display( 'your_field_name', $default_value, 250, 250, 20, 20);

In Joomla! 2.5 the parameters list for the display() function has been lengthened, so to use specific display parameters in 2.5:

$editor =& JFactory::getEditor();
$params = array( 'smilies'=> '0' , 'style'  => '1' , 'layer'  => '0' , 'table'  => '0' ,'clear_entities'=>'0' );
echo $editor->display( 'desc', '', '400', '400', '20', '20', false, null, null, null, $params );

Allow html code in JRequest

How to create HTML select lists in Joomla!

If you need create some HTML select list with items then you can use Joomla API.
Joomla! provide special class JHTML for creating form elements, such as selectlist, radiolist, booleanlist and etc…
Most popular. Get data from database and create selectlist.
Example ↓

$selected = JFactory::getInt('selectlist_name', 0); 

$db = JFactory::getDBO();
    $query = "SELECT `id` AS `value`, `title` AS `text` FROM `#__your_table` WHERE `cid`=1";
$db->setQuery($query);
$items_from_db = $db->loadObjectList();

$selectlist_data[] = JHTML::_('select.option',  '0', "- Select item -", 'value', 'text' );
$selectlist_data = array_merge( $selectlist_data, $items_from_db);

$list = JHTML::_( 'select.genericlist',
                    $selectlist_data, 
                    'selectlist_name', 
                    'size="1" class="inputbox selectlist"', 
                    'value', 'text', 
                    $selected, 'selectlist_id', 
                    true
);
echo $list;

Result ↓

Simple bubble sort on PHP

“Bubble Sort” is a classic sort. It used ~76% of programmers.
Example ↓

function bubbleSort ($data_array = array() ) {
$result_array = $data_array;
$size = sizeof($result_array);
if ($size)
  for( $i = 0 ; $i < $size; $i++) {      
    for( $j = $size-1; $j > $i; $j-- ) { 
      if ( $result_array[$j-1] > $result_array[$j] ) {
            $buf = $result_array[$j-1]; 
            $result_array[$j-1] = $result_array[$j]; 
            $result_array[$j] = $buf;
		}
	}
  }
return $result_array;
}
 
$test_array = array(1, 9, 5, 2, 4, 5, 7, 0, 3, 8, 6); 
$result = bubbleSort ($test_array);
echo '<pre>'; print_R($result); echo '</pre>';

Results ↓
Continue reading Simple bubble sort on PHP

How to display/hide strict/all errors in php?

If you need to display all php errors on the web-page before production, you can use it settings
For display ALL errors/notices/warnings/deprecated info:
If you have access to php.ini then you can change 2 params.
Example (in php.ini) ↓

error_reporting = E_ALL
display_errors = On

Often, you need to check a separate part of functional and you don’t want display errors on the all site.
In this way, you can insert 2 lines in your php file with debug functional.
Example ↓

//Show all errors, notices
error_reporting(E_ALL);
ini_set('display_errors', 1);

Note: Display errors could be turned off in the php.ini or your Apache config file.
Continue reading How to display/hide strict/all errors in php?

Set phone number formatting in PHP

To display formatted phone number using php you can use this function.
It consist of 5 patterns for numbers that can be easily changed to format what you need.
First pattern for 6 numbers: XX-XX-XX
Second pattern for 7 numbers: XXX-XXXX
Third pattern for 10 numbers: (XXX) XXX-XXXX
Fourth pattern for 12 numbers: +XXX (XX) XX-XX-XXX
Fifth pattern for 13 numbers: +XXX (XX) XXXX-XXXX

function getFormatedPhone($phone_number='')
{
	$phone = preg_replace("/[^0-9]/", '', $phone_number); //get only numbers
	$phone_length = strlen($phone);
	
switch ($phone_length) {
    case 6:
        $phone_formated = preg_replace("/([0-9]{2})([0-9]{2})([0-9]{2})/", "$1-$2-$3", $phone);
    break;
    case 7:
        $phone_formated = preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
    break;
    case 10:
        $phone_formated = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
    break; 
    case 12:
        $phone_formated = preg_replace("/([0-9]{3})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3})/", "+$1 ($2) $3-$4-$5", $phone);
    break;
    case 13:
        $phone_formated = preg_replace("/([0-9]{3})([0-9]{2})([0-9]{4})([0-9]{4})/", "+$1 ($2) $3-$4", $phone);
    break;
    default:
        $phone_formated = $phone;
    break;	
	}
return $phone_formated;
}

for ( $i=0; $i<10; $i++)
{
    $test_random_phone_number = rand(0,1)?(rand(10,99).rand(10,99).rand(10,99)):(rand(0,1)?(rand(100,999).rand(100,999).rand(1000,9999)):(rand(100,9999).rand(10,99).rand(1000000,9999999)));
    echo $test_random_phone_number.' formated to: ';
    echo getFormatedPhone($test_random_phone_number);
    echo "<br/>";
}

Results ↓
Continue reading Set phone number formatting in PHP

Set and get cookies in Joomla CMS

Now, it’s time to learn how to use cookies in Joomla!
If you want save some information in cookie or get some value from cookies then you can use $jcookie = JFactory::getApplication()->input->cookie;
Example ↓

$app = JFactory::getApplication();
$jinput = $app->input;
$jcookie  = $jinput->cookie;

$cookie_value = $jcookie->get( 'cookie_name', null); // Get cookie data  ($name, $defaultValue)

$jcookie->set( 'cookie_name', 'cookie_value', 0); // Set cookie data ($name, $value, $expire) $expire == 0: cookie lifetime is of browser session.

Continue reading Set and get cookies in Joomla CMS

How to set/get session value in Joomla! CMS?

Now, it’s time to learn how to use sessions in Joomla!
If you want save some information in session or get some value from session then you can use $session = JFactory::getSession();
Example ↓

$param_value = 'test session text'; // Note: also, you can store objects and arrays
$session = JFactory::getSession();
$session->set('param_value_name',$param_value); // set session value
$param_value = $session->get('param_value_name'); // get session value

More about JSession here: About JSession
Also, you cas see article about set/get cookies in Joomla!

How to set right page encoding using PHP (UTF-8 HTTP header)?

Quite frequently, many are faced with the problem: incorrect definition of the browser page coding.
The easy way to fix it – set the right page encoding by PHP.
Example (we use the encoding utf-8) ↓

<?php
header('Content-Type: text/html; charset=utf-8');
/*....*/
?>

Additionally, you can specify the encoding using HTML
Continue reading How to set right page encoding using PHP (UTF-8 HTTP header)?

How to use modal windows in Joomla! ?

Firstly you need write in the View file:

JHTML::_( 'behavior.modal' ); 

After you need write html a link with special params:

  • class=”modal”
  • rel=”{handler: “iframe”, size: {x: 500, y: 500}, onClose: function() {}}”

Example ↓

<a class="modal" rel="{handler: "iframe", size: {x: 500, y: 500}, onClose: function() {}}" href="http://blog.tormix.com" title="">
Modal link text
</a>

How to validate email address in JavaScript?

The best way is using regular expressions.
Example ↓

<script type="text/javascript">
function isValidEmail(email) {
    var regex_pattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return regex_pattern.test(email);
}
</script>

Also you can use some another reg.exps:

Continue reading How to validate email address in JavaScript?

Parameter in XML form structure field for allowing HTML tags

When you are using XML form structure you can use editor form field type.
In that case for allowing HTML tags you have to use the attribute:

filter=’safehtml’

to allow saving the content with html tags.
Example ↓

<field
name="field_desc"
type="editor"
filter="safehtml"
width="350"
height="80"
label="COM_YOURCOMPONENT_EDITOR_FIELD_LABEL"
description="COM_YOURCOMPONENT_EDITOR_FIELD_DESC"
cols="20"
rows="3"
class=""
default=""
/>

Continue reading Parameter in XML form structure field for allowing HTML tags

Allow HTML code in JRequest

use html

If you need to get HTML code from JRequest then you need to use special parameter:

JREQUEST_ALLOWHTML

Example (Joomla! 1.6.x-3.x) ↓

$value = JRequest::getVar( 'field_name', '', 'post', 'string', JREQUEST_ALLOWHTML );

Note ↓

      JREQUEST_NOTRIM – prevents trimming of whitespace
      JREQUEST_ALLOWRAW – bypasses filtering
      JREQUEST_ALLOWHTML – allows most HTML. If this is not passed in, HTML is stripped out by default.

Continue reading Allow HTML code in JRequest