• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Helper function to make input->post return empty string instead of false

#1
[eluser]Scared[/eluser]
Hi there

I'm new to CI. Usually, I do this:

Code:
$name = isset($_POST['name']) ?: '';

This means when I insert $name into my database, if it doesn't exist no error/warning is thrown, and an empty string gets inserted.

I've quickly found out that in CodeIgniter $this->input->post('name') returns false if the index doesn't exist, and it ends up being converted to 0 in my database. Even if $this->input->post('name') where to return null it's still a problem if the field in question doesn't allow null in the database.

So I've written a helper called "input_data" as follows:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('post'))
{
function post($index = '')
{
  $CI =& get_instance();
  return $CI->input->post($index) ?: '';
}  
}

Now instead of doing

Code:
$name = $this->input->post('name') ?: '';

I can do:

Code:
$name = post('name');

I wondered if this approach is OK, and if it's safe to call my helper function something as simple as post() ?

Thanks


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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