• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter helpful hints

#31
[eluser]Pygon[/eluser]
Mine is a personal tip regarding extending libraries/etc:

Where possible, use the original library methods but wrap them in code or replace the code using a condition to extend the functionality rather than duplicating and changing the original. Makes it quite a bit easier to upgrade if the original function changes.

MY_Lib.php
Code:
<?php
class MY_Lib extends CI_Lib
{
  function MY_Lib
  {
    //Standard Initialization
    parent::CI_Lib();
  }

  function _extended_function()
  {
    if(condition)
    {
      //my version of code
      return;
    }

    parent::_extended_function();
    return;
  }
}
?>

I find that I can accomplish most tasks using this method with very little copied code and it is easier to update should the base function change (since I don't have to figure out what I changed).

Not an end-all be-all but more of a preference for keeping things clean and updateable.

#32
[eluser]Lone[/eluser]
Heres a new one for helping with $_POST data as I wanted an easy to use function that gets all post data and xss_cleans it for me without setting the config variable required to do this.

Best used on a page that is receiving form data eg. 'do_add_item'

Extend the Input library with the following function:

Code:
function post_all() {
        $post = array();
        foreach ($_POST as $key => $value) {
            if (is_array($value)) {
                $post[$key] = array();
                foreach ($value as $arrayKey => $arrayValue) {
                    $post[$key][$arrayKey] = $this->xss_clean($arrayValue);
                }
            } else {
                $post[$key] = $this->xss_clean($value);
            }
        }
        return $post;
    }

To use in a controller etc. just use...

Code:
$post = $this->input->post_all();

$someVar = $post['name'];

Let me know what you think of this method please if there is something wrong/awesome about it Tongue

#33
[eluser]JOKERz[/eluser]
SUPERB THREAD!!
Should be sticky.

Thanx for all the hints guys... Smile

#34
[eluser]xwero[/eluser]
Reviving the thread. Try to take advantage of the class variables. An example

#35
[eluser]BaRzO[/eluser]
i am using in index.php BASEDIR cons to find where is the base directory.
Code:
define('BASEDIR', realpath(dirname(__FILE__)).'/');

#36
[eluser]Lone[/eluser]
I just updated my above post to handle arrays in $_POST as well as it errored out on it today for us.

#37
[eluser]xwero[/eluser]
a few php hints :

- Instead of doing this
Code:
$i = 0;
foreach($array as $item)
{
  // do something with the item
  $i++;
}
You could do this in many cases
Code:
foreach($array as $key => $item)
{
   // do something with the item
}

- Instead of doing this
Code:
substr($str,0,strlen($str)-1);
You better do this
Code:
substr($str,0,-1);

Learn from my code sins Smile

#38
[eluser]xwero[/eluser]
a comment hint :

If you use the curly backet new line rule to format your code you could put single line comments behind the opening backet.
Code:
// some comment
if($true)
{

}
else // other comment
{

}
versus
Code:
if($true)
{ // some comment

}
else
{ // other comment

}

#39
[eluser]xwero[/eluser]
Keeping the thread warm.

If you keep CI above the public directory it's a bit tricky to move from one server to another because the full path is added to the bootstrap file (index.php).

To make it easier on yourself you can move the EXT, FCPATH and SELF constant creation above the $system_folder and $application_folder variables.

This makes it possible to create a base_folder like this
Code:
$base_folder = str_replace('public/'.SELF,'codeigniter/',FCPATH);

In this example CI is located in a directory on the same level as the public directory.

This $base_folder variable you can be used to prefix the $system_folder and $application_folder variables.

Together with the Automatic config[base_url] it saves you a few headaches

#40
[eluser]xwero[/eluser]
back with some more hints:

This time about the uri library

To get the last segment you can do
Code:
$this->uri->segment($this->uri->total_segments());

To check if a segment value is present
Code:
in_array('value',$this->uri->segment_array());
This also means you can use array_search if you want a slice of the array.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.