Welcome Guest, Not a member yet? Register   Sign In
Best way to populate an edit form from a database
#2

[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>&lt;?php echo $title; ?&gt;</h1>
<br />
&lt;form action="controler_name/save" method="post"&gt;
  First Name:
  &lt;input type="text" name="first_name" value="&lt;?php set_value('first_name', $form['first_name']); ?&gt;" /&gt;
  <br />
  Last Name:
  &lt;input type="text" name="last_name"  value="&lt;?php set_value('last_name', $form['last_name']); ?&gt;" /&gt;
  <br />
  Shirt Size:
  &lt;?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']));
    
     ?&gt;
  &lt;input type="submit" name="submit" value="Submit" /&gt;
  &lt;input type="hidden" name="id" value="&lt;?php set_value('id', $form['id']); ?&gt;" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

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.


Messages In This Thread
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 09:19 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:14 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:25 AM
Best way to populate an edit form from a database - by El Forum - 05-01-2009, 10:01 AM
Best way to populate an edit form from a database - by El Forum - 05-22-2009, 10:15 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 02:02 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 07:19 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:10 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:33 PM
Best way to populate an edit form from a database - by El Forum - 01-06-2010, 07:02 AM
Best way to populate an edit form from a database - by El Forum - 01-08-2010, 08:20 AM
Best way to populate an edit form from a database - by El Forum - 02-15-2010, 07:06 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 05:58 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 09:03 AM
Best way to populate an edit form from a database - by El Forum - 03-23-2011, 11:33 AM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:40 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:45 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 02:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB