4

I have finally managed to get some kind of control panel area created, but cant figure out how to transfer my array over to the view page.

My aim is to create an array from the database, then display the value of that field in the view page.

mcp index function public function index() {

$this->EE->cp->set_variable('cp_page_title', $this->EE->lang->line('currency_module_name'));

    $this->EE->load->helper('form');
    $this->EE->load->library('table');
    $this->EE->load->library('form_validation');         

    $query = $this->EE->db->query("SELECT * FROM exp_currency");

    $data = array (
        "usd" => $query->row('usd'),
        "eur" => $query->row('eur'),
        "gbp" => $query->row('gbp')
    );      

    return $this->EE->load->view('add_currencies', $data, TRUE);

}

View file

<?=form_open()?>

    <?php

        $this->table->set_heading(
            lang('USD'),
            lang('EUR'),
            lang('GBP')
        );

        $this->table->add_row(
            form_input('usd', set_value('usd', $data['usd']), 'id="usd"'),          
            form_input('eur', set_value('eur', $data['eur']), 'id="eur"'),                          
            form_input('gbp', set_value('gbp', $data['gbp']), 'id="gbp"')                           
        );

        echo $this->table->generate();

        $this->table->clear(); 

    ?>

    <input type="submit" class="submit" value="<?=lang('update_currencies')?>" />

<?=form_close()?>

If anyone can point me in the right direction, that would be great :)

1
  • Low's answer below is great. You can read up a bit in the CodeIgniter docs on how that data becomes accessible in your view files. Jan 24, 2013 at 12:47

1 Answer 1

6

When you pass an array of data on to a view file, the keys become the variable names in that view file. So...

$data = array(
    'foo' => 'bar',
    'lorem' => 'ipsum'
);

return $this->EE->load->view('my_file', $data, TRUE);

...will mean that in the my_file view file you can then use the variables $foo and $lorem.

In your case, that would mean you can use $usd, $eur and $gbp. That might not be too useful, so I'd pass on those in their own array:

$data = array(
    'currencies' => array(
        "usd" => $query->row('usd'),
        "eur" => $query->row('eur'),
        "gbp" => $query->row('gbp')
    )
);

...and in your view file:

foreach ($currencies AS $key => $val)
{
    form_input($key, set_value($key, $val), 'id="'.$key.'"');
}

and so on.

3
  • 1
    Also, be sure to check out my screencast on add-on development: mijingo.com/products/screencasts/…
    – Low
    Jan 24, 2013 at 12:42
  • Thanks Low. That makes a lot more sense now. How would I use the foreach with the table add row? If I place the foreach within the table add row it throws errors.
    – ccdavies
    Jan 24, 2013 at 13:29
  • 1
    You don't necessarily have to use the foreach approach. Look at the docs of the Table class and just try stuff out; it's the best way to learn. ellislab.com/expressionengine/user-guide/development/usage/…
    – Low
    Jan 24, 2013 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.