CodeIgniter Forums

Full Version: generate a form from a db table - not quite
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I am a beginner at this - and don't quite get it.
I am almost using "generate a form from a db table" which says I can use the formbuilder function by itself. I can't seem to get it to work though.
Using the example given I have in controller/managetables.php
Code:
<?php
Class Managetables Extends Controller
{
    function index()
    {
    $template['xajax_js'] = $this->xajax->getjavascript(null, 'http://localhost/custom/javascript/xajax.js');
    $attributes = array('name'     => 'sectionContentForm',
                      'id'       => 'sectionContentForm',
                      'onSubmit' => 'xajax_addUser(xajax.getFormValues(\'sectionContentForm\'));return false;');
      
    $formOutput = form_open('#',$attributes);
    $formElements = $this->_formBuilder('users');
    foreach($formElements as $name => $htmlTag){
        
    $formOutput .= '<label for="'.$name.'">';
    if(stristr($htmlTag,'type="hidden"')){
      $formOutput .= $this->$name.'</label><br />'.$htmlTag;
      $displayName = str_replace('&lt;input type="hidden" name="'.$name.'" value="','',$htmlTag);
      $displayName = str_replace('" /&gt;','',$displayName);
     $formOutput .= '<b> -- '.$displayName.'</b><br />';
    } else {
      $formOutput .= $this->$name.'</label><br />'.$htmlTag.'<br />';
    }
  }
  $formOutput .= form_submit('submitContentForm','Create').form_close();

    $template['formOutput'] =  $formOutput;
    $this->load->view('managetables', $template);
    }


function Managetables(){
  parent::controller();
  $this->load->library('xajax');
  $this->load->helper('url');
    $this->load->helper('form');
    $this->load->helper('html');
  $this->xajax->registerFunction(array('toggleManagement',&$this,'_toggleManagement'));
  lots more here ...
}
  function _formBuilder($table,$values=array(),$valuesAsHidden=FALSE){
  $columns = $this->db->query($this->db->_list_columns($table));
  if($columns->num_rows() > 0){
    foreach ($columns->result_array() as $columnInfo){
      foreach($columnInfo as $key => $val){
        if($key == 'Field'){
          $fieldName = $val;
          // if(!$this->data->getManagementStatus($table,$fieldName)) break;  //comment this out if you are using only this function and not the supporting "management" fuctions
        }
        $arrTableInfo[$fieldName][$key] = $val;
      }
    }
    $formDisplay = '';
lots more here ...
}
and in views/managetables.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;?php echo $xajax_js;?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo $formOutput ;?&gt;
&lt;/body&gt;
&lt;/html&gt;

I get an error message at the top for every field in the
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Managetables::$id
Filename: controllers/managetables.php
Line Number: 22

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Managetables::$last_name
Filename: controllers/managetables.php
Line Number: 22

and then I also get the form, but only the input line - no label?

anyhelp?
thanks

Katherine

El Forum

[eluser]Bunches McGinty[/eluser]
I don't know if this solves the problem, but you need the Constructor at the start of your class, not in the middle of it. Swap 'Managetables()' and 'index()' around.

I hope I'm right with that.

El Forum

[eluser]Unknown[/eluser]
Nope, that didn't do it...

El Forum

[eluser]sophistry[/eluser]
erm... this is a PHP error telling you that you haven't told the class that it has properties (Undefined property).

You need to understand classes and objects better - I suggest reading about object-oriented techniques in PHP.

the short answer:
put this right after the class opening bracket...
Code:
var $id;
var $last_name;
cheers

EDIT:
ps. if you post code, post *all* of the code... don't put "lots more here" and snip the code away.