Chan
Chan

Reputation: 1959

Show default value when param is not exist in view

controller

class Chan extends CI_Controller
{
    public function index()
    {
        $this->load->helper('chan');
        $this->load->view('chan', ['age' => 35]);
    }
}

chan_helper.php

function getValue($name = null) {
    global ${$name};

    if (isset(${$name})) {
        return ${$name};
    }
}

view/chan.php

<?php echo getValue('age'); ?>

I want to use the same view to do the insert and update stuff, when you doing insert method will be no data passing to the view, we can do something like

<input name="age" value="<?php echo isset($age) ? $age : ''; ?>">

But for me, I feel it's too ugly, therefore I want to build a helper called getValue to do this, but I can't get the value was set or not in the helper, how to make this function come true.

Upvotes: 1

Views: 48

Answers (1)

Didin Sino
Didin Sino

Reputation: 306

try this one:

function getValue(&$variable, $default_value = '') {
  return isset($variable) ? $variable : $default_value;
}

Upvotes: 1

Related Questions