views:

112

answers:

3

I am developing an application and want to display a form that will be filled in if editing the form, but will not be if the form will be a new entry. I believe the least verbose way of doing this is to have just one form and to suppress any errors for echoing my variables so that nothing will be printed if it is a new form (since the variables will not exist if it is a new form). For example:

<?php 
if ( ! $new_item) {
    $variable = 'Example';
}
?>
<form>
    <input type="text" name="inputbox" value="<?php echo @$variable; ?>" />
</form>

Is this a good practice, or is there a better way of doing it? Should I make separate views for the new items and for the items to be edited? What are the performance repercussions of doing this?

+1  A: 

I think the best way is to always set the variable

if ( ! $new_item) {
    $variable = 'Example';
} else {
    $variable = 'Default value'; 
    //or
    $variable = '';
}

Using the @ error suppression is slow.

edit Note that when you print the variable into the input value attribute, you should first call htmlentities() or htmlspecialchars() on it.

2nd edit

You say in your comment you are fetching the data as on object, in that case you could also do:

class example {
    public $somefield = 'default value';
}

Then if it is an existing record you are editing do something like this, which will cause the returned object to be an instance of 'example':

$record = mysql_fetch_object($result, 'example');

or if it is a new record instanciate it yourself:

$record = new example();

Then you always know $record->somefield will be set and you can do :

<form>
    <input type="text" name="somefield" value="<?php echo htmlspecialchars($record->somefield); ?>" />
</form>
Tom Haigh
ok, well obviously it would be simple to do that with the example, but my actual case is a little more complicated - I'm getting an object from the database so I would have to set every member of the object.
Steven Oxley
why don't you define the object with default values then? and then overwrite them with data from the DB if there is any?
benlumley
i'm using CodeIgniter and I don't know if they have a way of fetching a result into my own custom object. I guess I could do it manually, though.
Steven Oxley
If you are using codeigniter then I would expect there will be tools/tutorials to help you do simple CRUD stuff like this
Tom Haigh
+2  A: 

i tend to use:

$variable = (isset($new_item) && $new_item) ? 'Example' : 'Default Value';

Overkill really, but i think going straight for !$new_item throws a notice if it isn't set. You can also switch the second clause out for !empty($new_item) depending on the behaviour you want.

benlumley
+4  A: 

You can use

<?php echo ((isset($value) && $value != '') ? $value : 'Default'); ?>

A cleaner solution to this problem is to create a helper function that does this for you:

function useDefault($value, $default = '')
{
    if (isset($value) && $value != '')
    {
        return $value;
    }
    else
    {
        return $default;
    }
}

You can then use

<?php echo useDefault($value, 'default value'); ?>
Aron Rotteveel
Tom Haigh