Welcome Guest, Not a member yet? Register   Sign In
array_alternator helper, addition to array_helper
#1

[eluser]cahva[/eluser]
I feel silly posting this as its almost the same code than alternator() function in string helper but for array. I needed one and here it is:

application/helpers/MY_array_helper.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Array Alternator
*
* Allows arrays to be alternated. This works almost like alternator in
* string helper but takes only one argument(array)
*
* Example:
*
* $foo = array('cat','dog','mouse');
*
* for ($i=1; $i<10; $i++)
* {
*        echo array_alternator($foo);
* }
*
* With each iteration of your loop the next element will be returned from array.
* It will start from first element the next time when reached to the end of the array.
*
*
* @access    public
* @param    array
* @return    string
*/

if ( ! function_exists('array_alternator'))
{
    function array_alternator($arr = FALSE)
    {
        static $i;

        if ($arr === FALSE || !is_array($arr))
        {
            $i = 0;
            return '';
        }

        return $arr[($i++ % count($arr))];
    }
}

/* End of file MY_array_helper.php */
/* Location: ./application/helpers/MY_array_helper.php */

I used this for my project where I needed(well I didnt need to but wanted to Smile ) to alternate asset urls in my views(Parallelization, paraller downloads, HTTP pipelining..). In other words balance the loading of images using different hostnames to get paraller downloads.

And Yay!, got Overall performance score 90 (Grade A) in Yslow and 96 points out of 100 in google Page Speed for my site with this(with other optimizations ofcourse) Wink Without multiple asset urls google page speed gave 91 points so its worth 5 points.

For example I had this set in config:
Code:
$config['asset_urls'] = array(
    'http://assets1.mydomain.com',
    'http://assets2.mydomain.com',
    'http://assets3.mydomain.com'
);

..and then used in views like this:
Code:
<img src="&lt;?php echo theme_url('img/buttons/btn_1.png') ?&gt;" alt="btn1" />
<img src="&lt;?php echo theme_url('img/buttons/btn_2.png') ?&gt;" alt="btn2" />
<img src="&lt;?php echo theme_url('img/buttons/btn_3.png') ?&gt;" alt="btn3" />
<img src="&lt;?php echo theme_url('img/buttons/btn_4.png') ?&gt;" alt="btn4" />
That will give output using all the asset urls and optimized parallelization.

theme_url() is just a helper that I use and it contains the usage of array_alternator:
Code:
function theme_url($uri = '')
{

    if (config_item('asset_urls'))
    {

        $urls = config_item('asset_urls');
        if (!empty($urls))
        {
            return array_alternator($urls).site_url(config_item('theme').$uri);
        }
    }

    return site_url(config_item('theme').$uri);
}

Oh.. And if someone plans on using that theme_url function, just make sure your $config['base_url'] is set to '/' or '/my_app/' for example. So no full url to your site.

Wow.. I intended to write short post but failed miserably and got sidetracked Smile




Theme © iAndrew 2016 - Forum software by © MyBB