CodeIgniter Forums
Array values that arent set - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Array values that arent set (/showthread.php?tid=18738)



Array values that arent set - El Forum - 05-15-2009

[eluser]a.somervell[/eluser]
Heya, anyone got an intelligent answer to this, its escaping me:

I'm doing a simple profile management form for an account

In my form i've got

Code:
<input type="text" name="first_name" value="<?=set_value("first_name",$account['first_name'])?>" />

first_name is obviously the field name (and database field name), if I don't see a post I grab the defaults from the database and pass the row_array() to the $account variable in the view.

If someone goes in and theres no first_name in the database $account['first_name'] isn't set and I get a php notice that it is undefined.

Now, I could ignore notices or go...

Code:
$account['first_name'] = (isset($account['first_name']) ? $account['first_name'] : "";

... for every bloody line, but i'd rather just have a simple solution and there's *gotta* be one in PHP I just dont know about?


Array values that arent set - El Forum - 05-16-2009

[eluser]Colin Williams[/eluser]
I did a screencast awhile back that explains this. It is the controller's job to provide the view will all the data it needs. So simply provide those variables via the controller.

1.) Have all variables available with default values (even if they are all empty)
2.) If the form needs to be populated from the DB, get values from the model
3.) If dealing with a failed form submission, populate the values with the $_POST values (naturally, just use the $_POST values)

Basically, you seem to be missing step 1


Array values that arent set - El Forum - 05-16-2009

[eluser]TheFuzzy0ne[/eluser]
Colin, a link to that screencast would be much appreciated. Smile


Array values that arent set - El Forum - 05-16-2009

[eluser]Dregond Rahl[/eluser]
why not something like this?

Code:
foreach($account as $key => $value){
    if(isset($account[$key])){
        $account[$key] = $value;
    } else {
        $account[$key] = "";
    }
}

But honestly best practice to define everything first with defaults.


Array values that arent set - El Forum - 05-16-2009

[eluser]a.somervell[/eluser]
Seriously though, setting array variables with empty strings? I'm lazy, I don't wanna have to do that! :'(

Thanks for the replies BTW




*trudges off to set stupid $account['first_name'] = ""; everywhere


Array values that arent set - El Forum - 05-16-2009

[eluser]Jondolar[/eluser]
One day, far, far in the future, you will benefit from doing this. I hate initializing variables and am waiting patiently to see when it actually benefits me (other than stopping notices).


Array values that arent set - El Forum - 05-16-2009

[eluser]Colin Williams[/eluser]
If you hate it so much, lower your error reporting threshold and carry on happily.


Array values that arent set - El Forum - 05-17-2009

[eluser]TheFuzzy0ne[/eluser]
It benefits you from the start by making your code more readable. It can help you see what type a variable is, by the default value assigned to it. It can also make your code easier to read.


Array values that arent set - El Forum - 05-17-2009

[eluser]Dam1an[/eluser]
When I first started coding, I hated and didn't see the point of it, but done it because I had to (it was Java, and woulnd't compile otherwise)... several years later, I do it by choice...
Its not exactly much effort to do, and it DOES make your code easier to understand