Helper function to make input->post return empty string instead of false - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Helper function to make input->post return empty string instead of false (/showthread.php?tid=60121) |
Helper function to make input->post return empty string instead of false - El Forum - 01-19-2014 [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'); 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 |