code igniter form_input

Question!

A couple questions here..

  1. What is the following syntax?
  2. What do all the pieces mean?

    (( ! is_array($data)) ? $data : '')
    
  3. How is it used in the function at the end?

    function form_input($data = '', $value = '', $extra = '')
    {
        $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
        return "<input "._parse_form_attributes($data, $defaults).$extra." />";
    }
    

Thankyou. Blake



Answers

1) It returns an input field;

2) Pardon?

3) Function takes three values. $value is the default value of the input field (the one you find in the textbox); $data will be the name of the form if it isn't an array, otherwise the name will be blank; $extra is a string containing other stuff that will go in the form. Please refer to the function _parse_form_attributes;

4) link

By the way, I sincerely hope that _parse_form_attributes() does something amazing, since that looks like an incredibly verbose function and codeigniter has some handy built-in form generation functions.



Ok this seems to generate the HTML for a form input with ome useful default values.... To answer your Qs in order:

  1. It's a function that takes optional parameters. You could call eg form_input('SomeData', 'SomeValue');
  2. It creates the HTML for the element and includes whatever information you give it, using defaults if no value is provided
  3. A difficult one to answer... In short you're defining a new function. If you provide parameters, they will be used to create a form element. If you omit them, defaults will be used - eg type will default to text for output like
By : Basic


It's an inline expression of:

function form_input($data = '', $value = '', $extra = '')
{
    if (is_string($data)) {   // or better: is_scalar()
        // leave as-is
    }
    else {
        $data = '';
    }
    ...

(Slightly variated.)
It basically ensures that the $data parameter is never an array. Maybe it was allowed to be an array in previous versions of the function, and this is safety code to ensure correct operation.

Note that type enforcement is another approach (often overkill, but sometimes advisable):

 function xy($data, $value, $z) {

      assert( ! is_array($data) );
      assert( is_string($value) );
By : mario


This video can help you solving your question :)
By: admin