Swop_segment_url

function swop_segment_url($params = array(), $query = array())
{
	$ci = &get_instance();

	$qstring = $_SERVER['QUERY_STRING'];
	parse_str($qstring, $parts);
	if ( ! is_array($params))
	{ 
		$params = array($params); 
	}

	$p = $params + array_splice($ci->uri->segment_array(), 0);
	ksort($p);
	$query = $query + array_splice($parts, 0);
	foreach($query as $k => $v)
	{
		if($v == '')
		{
			unset($query[$k]);
		}
	}
	$q = '';
	if( ! empty($query)) { $q = '?'.http_build_query($query); }
	return site_url($p).'/'.$q;
}

MY_Router - Underscore to Dash Converter

<?php
class MY_Router extends CI_Router
{
    
function _set_request($segments = array()) {
        parent
::_set_request(str_replace('-''_'$segments));
    
}
}
?> 

link_to helper

if (!function_exists('link_to'))
{
       
function link_to($object, $text, $method="view")
       
{
            get_instance
()->load->helper('inflector');
           
return anchor(plural(strtolower(get_class($object)))."/".$method."/".$object->id, $text);
       
}
}
An helper method I use in my CodeIgniter apps. Now you can type:

<?= link_to($user, $user->name); ?>

instead of:

<?= anchor("users/view/".$user->id, $user->name); ?>

So you don't need to manually type the URL.

My most used helpers


function mpr($d, $echo = TRUE)
{
   if($echo)
   {
	   echo '<pre>'.print_r($d, true).'</pre>';
   }
   else
   {
      return '<pre>'.print_r($d, true).'</pre>';
   } 
}

function mprd($d) 
{ 	
   mpr($d); 	
   die; 
}

function mvr($d) 	
{
   echo '<pre>'.var_dump($d, true).'</pre>'; 
}

function mvrd($d) 
{
   mvr($d);
   die; 
}

Result to Select

Isn’t it annoying that the form_dropdown cannot accept an Activerecord result() or result_array() directly?

This is where result_to_select comes in.

function result_to_select($result, $blank = FALSE)
{
    if (is_array($result))
    {
        $options = $keys = array();
        
        foreach ($result AS $row)
        {
            if (count($row) !== 2)
            {
        		show_error('function ' . __function__ . ": Array having more than 2 or less columns");
            }
    		
             foreach ($row AS $key => $value)
            {
                $keys[] = $key;
            }
            
            for($i = 0; $i < count($keys); $i++)
            {
                $options[$row[$keys[0]]] = $row[$keys[1]];
            }
        }
        
        if($__blank)
        {
        	$options = add_blank_option($options, $blank);
       	}
        return $options;
    }
    show_error("Passed wrong array options parameter");
}

Usage is result_to_select($result_array, 'choose');

The helper will only accept a result_array() from activerecord, if anyone can make it accept a result() please let me know.

Add Blank Option to Dropdown

So, you have an array of data prepared for the form_dropdown() helper, but you want to add a blank option at the top.


function add_blank_option($options, $blank_option = '') 
{
    if (is_array($options) && is_string($blank_option))
    {
       if (empty($blank_option))
       {
          $blank_option = array( NULL => '--');
       }
       else
       {
          $blank_option = array( NULL => $blank_option);
       }
       $options = $blank_option + $options;
       return $options;
} else { show_error("Wrong options array passed"); } }

Usage is $data = add_blank_option($array, 'Choose');

asker

eyoosuf asked: no more snipts?

More coming soon, please feel free to submit your own.

Currency Helper

function currency($number, $symbol = TRUE)
{
   if($symbol)
   {
      return money_format('%.2n', $number);
   }
   else
   {
      return money_format('%!.2n', $number);
   }
}

Used in conjunction with set_locale()

URL helpers

function self_url()
{
    return get_instance()->uri->ruri_string();
}

function refresh()
{
    redirect(self_url());
}