[eluser]cwt137[/eluser]
I've had this issue and here is my solution. You can still use the same view and still use set_value() for both editing and for creating new information. For the create page, you create a null variable and set_value() will put a blank value. I don't mess around with the set_select() function because on the edit page it is hard to select the entry from the database and have the re-population from form validation work. So, I use a combination of both the form_dropdown() and the set_value() functions. Here below is a snippet from a controller to demonstrate my point.
Code:
function create() {
$data = array('title' => 'Add Page', 'form' => NULL);
$this->load->view('form', $data);
}
function edit($id) {
$sql = 'SELECT id, first_name, last_name, shirt_size ';
$sql .= ' FROM some_tbl WHERE id = ?';
$sql_params = array($id);
$query = $this->db->query($sql, $sql_params);
$form = $query->row_array();
$data = array('title' => 'Edit Page', 'form' => $form);
$this->load->view('form', $data);
}
The view would look something like this:
Code:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<br />
<form action="controler_name/save" method="post">
First Name:
<input type="text" name="first_name" value="<?php set_value('first_name', $form['first_name']); ?>" />
<br />
Last Name:
<input type="text" name="last_name" value="<?php set_value('last_name', $form['last_name']); ?>" />
<br />
Shirt Size:
<?php
$options = array(
'' => 'Select Shirt',
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
echo form_dropdown('shirt_size', $options, set_value('shirt_size', $form['shirt_size']));
?>
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="id" value="<?php set_value('id', $form['id']); ?>" />
</form>
</body>
</html>
I still have problems with check boxes and radio boxes. I still don't know the best way to have the right things pre-selected from the database and have the right stuff re-populated when the form validation comes back.