开发者

code igniter form_input

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 开发者_StackOverflow中文版: ''), 'value' => $value);
        return "<input "._parse_form_attributes($data, $defaults).$extra." />";
    }
    

Thankyou. Blake


Ok let me try as far as I understand you question.

What is the following syntax?

It is a function definition.

What do all the pieces mean?

This is the ternary operator. It means: If $data is not an array (!is_array($data)), return $data (? $data) otherwise return an empty string (: '')

It is a shorthand for if-else.

How is it used in the function at the end?

Not sure what you mean here. A function _parse_form_attributes($data, $defaults) is called witch seems to return a string.

If it in your question refers to $defaults, then it is just an array that gets build and that contains the following values:

Array (
    'type' => 'text',
    'name' => $data, // or empty string if $data is an array,
    'value' => $value
);

It is used to build an input element, that will look like this:

<input type="text" 
       name="(value of $data or empty)" 
       value="(value of $value)"
       (value of $extra)
       />


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 <input type="text" [blah]>
  4. I'm not quite sure what you mean here - Append as key-value pairs eg <input type="text" key="Value" [blah]> ? If this is the case, I would personally lose the $extra variable and have an array of key->value pairs instead. Failing that, you need to et $extra as appropriate eg key1="value1" key2="value2"


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) );


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜