Welcome Guest, Not a member yet? Register   Sign In
Array values that arent set
#1

[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?
#2

[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
#3

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

[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.
#5

[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
#6

[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).
#7

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

[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.
#9

[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




Theme © iAndrew 2016 - Forum software by © MyBB