Part of the EllisLab Network
   
 
generate a form from a db table - not quite
Posted: 17 July 2007 09:20 AM   [ Ignore ]  
Summer Student
Total Posts:  2
Joined  07-17-2007

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

<?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('<input type="hidden" name="'.$name.'" value="','',$htmlTag);
      
$displayName = str_replace('" />','',$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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<?php echo $xajax_js;?>
</head>
<
body>
<?php echo $formOutput ;?>
</body>
</
html>

I get an error message at the top for every field in the

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

Profile
 
 
Posted: 17 July 2007 11:13 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  6
Joined  07-05-2007

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.

Profile
 
 
Posted: 20 July 2007 09:36 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  2
Joined  07-17-2007

Nope, that didn’t do it…

Profile
 
 
Posted: 20 July 2007 02:18 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  722
Joined  08-06-2006

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…

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.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64457 Total Logged-in Users: 20
Total Topics: 80969 Total Anonymous Users: 1
Total Replies: 435701 Total Guests: 169
Total Posts: 516670    
Members ( View Memberlist )