Welcome Guest, Not a member yet? Register   Sign In
using variables passed to views as global in function helpers
#1

[eluser]Mackstar[/eluser]
I have a custom helper that I want to use in my views

It contains a function that dynamically manipulates values added to the form. Because I want it to use dynamic variables, but using global doesn't seem to work. Even if I hard code my variables

This is what I am doing
Code:
function form_values($variable, $head=NULL)
{
    if($head)
        global ${$head};
    if(isset(${$head}[$variable]))
        return ${$variable};
    elseif($head)
        return set_value("{$head}[{$variable}]");
    else
        return set_value($variable);
}

I have a array set into $account and am calling it as in

Code:
<input type="text" name="account[name]" value="<?php echo form_values("name", "account"); ?>" size="50" />

I have complete access to the $account array in the view.

Why is this not registering the array as a global?

Thanks in advance

Richard
#2

[eluser]TheFuzzy0ne[/eluser]
Probably because the array is not in the global scope. Where are you defining it?
#3

[eluser]Mackstar[/eluser]
Thanks for the reply,

I am defining it in the controller

Code:
$data["account"]=$result[0];
$this->layout->view('/accounts/edit', $data);

Should I define it in the controller as a global? Or is there another way to get it into the global scope?

Thanks again

Richard
#4

[eluser]Mackstar[/eluser]
OK thanks, I worked it out myself but I wouldn't have thought about it without your prompt.

Rather than passing the array through the view loader, I defined it as a global.
Thanks fuzzy one!


Richard




Theme © iAndrew 2016 - Forum software by © MyBB