CodeIgniter POST / GET默认值 [英] CodeIgniter POST/GET default value

查看:145
本文介绍了CodeIgniter POST / GET默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果POST / GET数据为空/假,例如

  $ this->,则可以为POST / GET数据设置默认值。输入>后(varname,value-if-falsy)

因此,我不必像

  $ a = $这样的代码this-> input-> post(varname)? 
$ this-> input-> post(varname):
value-if-falsy

谢谢。

解决方案

您必须重写默认行为。 b
$ b

在application / core中创建MY_Input.php

  class MY_Input extends CI_Input 
{
函数post($ index = NULL,$ xss_clean = FALSE,$ default_value = NULL)
{
//检查字段是否已被提供
if($ index = == NULL AND!empty($ _ POST))
{
$ post = array();

//遍历整个_POST数组并返回它
foreach(array_keys($ _ POST)as $ key)
{
$ post [$ key] = $ this-> _fetch_from_array($ _ POST,$ key,$ xss_clean);
}

return $ post;

$ b $ ret_val = $ this-> _fetch_from_array($ _ POST,$ index,$ xss_clean)
if(!$ ret_val)
$ ret_val = $ default_value

返回$ ret_val;


然后在你的控制器中:

  $ this-> input-> post(varname,,value-if-falsy)


Can I set default value for POST/GET data if it's empty/false, something like

$this->input->post("varname", "value-if-falsy")

?

So I don't have to code like

$a = $this->input->post("varname") ? 
     $this->input->post("varname") :
     "value-if-falsy"

Thanks.

解决方案

You have to override the default behaviour.

In application/core create MY_Input.php

class MY_Input extends CI_Input
{
        function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
        {
            // Check if a field has been provided
            if ($index === NULL AND ! empty($_POST))
            {
                $post = array();

                // Loop through the full _POST array and return it
                foreach (array_keys($_POST) as $key)
                {
                    $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
                }

                return $post;
            }

            $ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean)
            if(!$ret_val)
                $ret_val = $default_value

            return $ret_val;
        }
}

And then in your controller :

$this->input->post("varname", "", "value-if-falsy")

这篇关于CodeIgniter POST / GET默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆