Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

Category:Library | Category:Library::Community | Category:Library::CodeGenerators This will generate CRUD code and output to one single page. All you need to do is copy/paste the code blocks in each appropriate file and you're set!

libraries/CRUD_Buster.php

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /*****

class CRUD_Buster {

var $CI;

function __construct()
{
    $this->CI =& get_instance();
}

function output_controller_code()
{
    $this->CI->data['field_list'] = $this->CI->input->xss_clean($_POST['field_list']);
    $this->CI->data['description_list'] = $this->CI->input->xss_clean($_POST['description_list']);
    $data_type = strtolower($this->CI->input->xss_clean($_POST['data_type']));
    $field_type = strtolower($this->CI->input->xss_clean($_POST['field_type']));
    $database_table = $this->CI->input->xss_clean($_POST['database_table']);
    $dbfield_type = strtolower($this->CI->input->xss_clean($_POST['dbfield_type']));
    
    $controller_name = ucfirst($data_type); // Ex: Post
    if ($controller_name[strlen($controller_name)-1] == "y")
    {
        $controller_name_mod = $controller_name;
        $controller_name_mod[strlen($controller_name_mod)-1] = "i";
        $pl_controller_name = $controller_name_mod."es"; // Ex: Companies
    }
    else
    {
        $pl_controller_name = $controller_name."s"; // Ex: Posts
    }
    $model_name = $controller_name."_model"; // Ex: Post_model
    $lc_controller_name = strtolower($controller_name); // Ex: post
    $field_type_id = $lc_controller_name."_id"; // Ex: post_id
    
    $controller_file_name = "controllers/".$data_type.".php"; // Ex: controllers/post.php
    $model_file_name = "models/".$data_type."_model.php"; // Ex: models/post_model.php
    $view_add_base = $data_type."/".$data_type; // Ex: post/post
    $view_all_file_name = "views/".$view_add_base."_view_all.php"; // Ex: views/view_view_all.php
    $view_add_file_name = "views/".$view_add_base."_add_".$data_type.".php"; // Ex: views/view_add_view.php
    $view_edit_file_name = "views/".$view_add_base."_edit_".$data_type.".php"; // Ex: views/view_edit_view.php
    $view_delete_file_name = "views/".$view_add_base."_delete_".$data_type.".php"; // Ex: views/view_delete.php
    $view_delete_base = "delete".$lc_controller_name."_confirmed"; // Ex: deletepost_confirmed
    
    $add_to_model = "\$this->".$controller_name."_model->add".$controller_name."();"; // Ex: $this->Post_model->addPost();
    $edit_to_model = "\$this->".$controller_name."_model->edit".$controller_name."();"; // Ex: $this->Post_model->editPost();
    
    $add_to_view = "\$this->template->load('mainTemplate', '".$view_add_base."_add_".$data_type."', \$data);"; // Ex: $this->template->load('mainTemplate', 'post/post_add_post', $data);
    $add_to_view_success = "\$this->template->load('mainTemplate', 'common/blank', \$data);"; // Ex: $this->template->load('mainTemplate', 'common/blank', $data);
    $edit_to_view = "\$this->template->load('mainTemplate', '".$view_add_base."_edit_".$data_type."', \$data);"; // Ex: $this->template->load('mainTemplate', 'post/post_add_post', $data);
    
    $fieldName_array = explode("|",$this->CI->data['field_list']);
    $fieldDesc_array = explode("|",$this->CI->data['description_list']);
    $fieldType_array = explode("|",$field_type);
    $dbfieldType_array = explode("|",$dbfield_type);
    
    $code = <<<EOD
<form>

MySQL:

<textarea name="code" rows="10" cols="100" id="myCpWindow9" class="codepress sql linenumbers-on"> USE database_name; CREATE TABLE table_name (

EOD;

    $i = 1;
    foreach ($dbfieldType_array as $df)
    {
        if ($i < count($dbfieldType_array))
        {
            $code .= "&nbsp;&nbsp;".$df.",\n";
        }
        elseif ($i = count($dbfieldType_array))
        {
            $code .= "&nbsp;&nbsp;".$df."\n";
        }
        $i++;
    }

    $code .= <<<EOD

); </textarea>



Controller: $controller_file_name

<textarea name="code" rows="25" cols="100" id="myCpWindow1" class="codepress php linenumbers-on"> <?php

class $controller_name extends Controller {

    function $controller_name()     {        parent::Controller();        $this->load->library('session');        //$this->output->enable_profiler(TRUE);     }\n     function view()     {        if ( !$this->session->userdata('logged_in') )        {           redirect('user/login/', 'location');        }        else        {           if ( $this->uri->segment(3) == NULL )           {              redirect('$lc_controller_name/view/all/', 'location');           }

          if ( $this->uri->segment(3) == "all" )           {              $this->load->library('pagination');              $config['base_url'] = base_url() . '{$lc_controller_name}/view/all/';              $config['total_rows'] = $this->db->count_all('$database_table');              $config['uri_segment'] = '4';

             $config['per_page'] = '10';              $config['num_links'] = '4';

             $config['full_tag_open'] = '

    ';              $config['full_tag_close'] = '
';

             $config['cur_tag_open'] = '

  • ';              $config['cur_tag_close'] = '
  • ';

                 $config['main_link_open'] = '

  • ';              $config['main_link_close'] = '
  • ';              $config['main_link_class'] = 'id="dbresultsnav"';

                 $config['main_curlink_open'] = '

  • ';              $config['main_curlink_close'] = '
  • ';              $config['main_curlink_class'] = 'id="dbresultsnavcur"';

                 $this->pagination->initialize($config);

                 $num = $config['per_page'];

                 if ($this->uri->segment(4) != NULL)              {                 $offset = $this->uri->segment(4);
                 }              else              {                 $offset = 0;              }

                 $data['pagination'] = $this->pagination->create_links();

                 $this->load->model('$model_name', '', TRUE);              $data['All$pl_controller_name'] = $this->{$model_name}->findAll{$pl_controller_name}($num,$offset);              $data['head_title'] = 'My Site - View All $pl_controller_name';              $this->template->load('mainTemplate', '$lc_controller_name/{$lc_controller_name}_view_all', $data);           }           else           {              redirect('user/profile/', 'location');           }        }
        }     function add()     {        if ( !$this->session->userdata('logged_in') )        {            redirect('user/login/', 'location');        }        else        {            $rules = array(\n EOD;

        $i = 1;
        foreach ($fieldName_array as $f)
        {
            if ($i < count($fieldName_array))
            {
                if ($f == "email")
                {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$f."' => \"trim|valid_email|xss_clean\",\n";
                }
                else
                {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$f."' => \"trim|xss_clean\",\n";
                }
            }
            elseif ($i = count($fieldName_array))
            {
                $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$f."' => \"trim|xss_clean\"\n";
            }
            $i++;
        }
        
        $code .= <<<EOD
    

                   );

               $fields = array(\n EOD;

        $i = 1;
        foreach ($fieldDesc_array as $d)
        {
            if ($i < count($fieldDesc_array))
            {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$fieldName_array[$i-1]."' => \"".$d."\",\n";
            }
            elseif ($i = count($fieldDesc_array))
            {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$fieldName_array[$i-1]."' => \"".$d."\"\n";
            }
            $i++;
        }
    
        $code .= <<<EOD
    

                   );\n            $this->validation->set_rules($rules);            $this->validation->set_fields($fields);            $this->validation->set_error_delimiters('

    ', '
    ');\n            if ($this->input->post('submit'))            {               if ($this->validation->run() == FALSE)               {
                      $data['head_title'] = 'My Site - Add $controller_name';                   $add_to_view               }               else               {                   $this->load->model('$model_name', '', TRUE);                   $data['query'] = $add_to_model                   $add_to_view_success               }            }            else            {               $data['head_title'] = 'My Site - Add $controller_name';               $add_to_view            }         }     }

        function edit()     {        if ( !$this->session->userdata('logged_in') )        {            redirect('user/login/', 'location');        }        else        {            $rules = array(\n EOD;

        $i = 1;
        foreach ($fieldName_array as $f)
        {
            if ($i < count($fieldName_array))
            {
                if ($f == "email")
                {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$f."' => \"trim|valid_email|xss_clean\",\n";
                }
                else
                {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$f."' => \"trim|xss_clean\",\n";
                }
            }
            elseif ($i = count($fieldName_array))
            {
                $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$f."' => \"trim|xss_clean\"\n";
            }
            $i++;
        }
        
        $code .= <<<EOD
    

                   );

               $fields = array(\n EOD;

        $i = 1;
        foreach ($fieldDesc_array as $d)
        {
            if ($i < count($fieldDesc_array))
            {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$fieldName_array[$i-1]."' => \"".$d."\",\n";
            }
            elseif ($i = count($fieldDesc_array))
            {
                    $code .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'".$fieldName_array[$i-1]."' => \"".$d."\"\n";
            }
            $i++;
        }
    
        $code .= <<<EOD
    

                   );\n            $this->validation->set_rules($rules);            $this->validation->set_fields($fields);            $this->validation->set_error_delimiters('

    ', '
    ');\n            if ($this->input->post('submit'))            {               if ($this->validation->run() == FALSE)               {
                      $data['head_title'] = 'My Site - Edit $controller_name';                   $edit_to_view               }               else               {                   $this->load->model('$model_name', '', TRUE);                   $data['query'] = $edit_to_model                   $add_to_view_success               }            }            else            {               $data['head_title'] = 'My Site - Edit $controller_name';               $edit_to_view            }        }     }

    }

    ?> </textarea>



    Model: $model_file_name

    <textarea name="code" rows="25" cols="100" id="myCpWindow2" class="codepress php linenumbers-on"> <?php

    class $model_name extends Model {

        function $model_name()     {        parent::Model();     }

        function add$controller_name()     {\n EOD;

        $i = 1;
        foreach ($fieldType_array as $t)
        {
            $fn = $fieldName_array[$i-1];
            $fd = $fieldDesc_array[$i-1];
            
        $code .= <<<EOD
    

           $this->data['$fn'] = $this->input->xss_clean($_POST['$fn']);\n EOD;

            $i++;
        }
    
        $code .= <<<EOD
    

    \n       $this->db->insert('$database_table', $this->data);     }\n     function edit$controller_name()     {\n        $this->data['$field_type_id'] = $this->input->xss_clean($_POST['$field_type_id']);\n EOD;

        $i = 1;
        foreach ($fieldType_array as $t)
        {
            $fn = $fieldName_array[$i-1];
            $fd = $fieldDesc_array[$i-1];
            
        $code .= <<<EOD
    

           $this->data['$fn'] = $this->input->xss_clean($_POST['$fn']);\n EOD; $i++; }

        $code .= <<<EOD
    

    \n       $this->db->insert('$database_table', $this->data, array('$field_type_id' => $this->data['$field_type_id']));     }\n EOD;

        $code .= <<<EOD
    

    \n    function delete$controller_name($$field_type_id)     {        $this->db->delete('$database_table', array('$field_type_id' => $$field_type_id));

           $messagedata = array(                 'msg_text' => '$controller_name deleted successfully.',                 'msg_col' => 'MsgBkgGrn'                 );

           $this->session->set_userdata($messagedata);
        } \n    function findAll$pl_controller_name($limit = NULL, $offset = NULL)     {        $this->db->select('*');        $this->db->from('$database_table');        $this->db->orderby("$field_type_id DESC");        $this->db->limit($limit, $offset);

           $query = $this->db->get();        return $query;
        }

        function displayThis$controller_name()     {
           $this->db->select('*');        $this->db->from('$database_table');        $this->db->where('job_id', $this->uri->segment(3));        $this->db->limit(1);

           $query = $this->db->get();        return $query;
        }

    \n    function findThis$controller_name($$field_type_id)     {        $this->db->select('*');        $this->db->where('$field_type_id', $this->uri->segment(3));        $this->db->limit(1);        $query = $this->db->get('$database_table');        return $query;     } } ?> </textarea>



    View: $view_all_file_name

    <textarea name="code" rows="25" cols="100" id="myCpWindow10" class="codepress php linenumbers-on">

    View $pl_controller_name



    <?php $color1 = "odd"; $color2 = "even"; $row_count = 0; ?>

    \n EOD;
        $i = 1;
        foreach ($fieldDesc_array as $d)
        {
            if ($i <= count($fieldDesc_array))
            {
                    $code .= "&nbsp;&nbsp;<th>".$d."</th>\n";
            }
            $i++;
        }
    
        $code .= <<<EOD
    

    <?php foreach($All{$pl_controller_name}->result() as $rowAll{$pl_controller_name}): ?> <?php $row_color = ($row_count % 2) ? $color1 : $color2; ?>

    EOD;
        $i = 1;
        foreach ($fieldName_array as $f)
        {
            if ($i <= count($fieldName_array))
            {
                $code .= "&nbsp;&nbsp;<td class=\"&lt;?php echo \$row_color; ?&gt;\">&lt;?php echo \$rowAll{$pl_controller_name}->".$f."; ?&gt;&nbsp;</td>\n";
            }
            $i++;
        }
    
        $code .= <<<EOD
    
    <?php \$row_count++; ?> <?php endforeach; ?>
    <?php echo anchor('{$lc_controller_name}/view/'.\$rowAll{$pl_controller_name}->{$lc_controller_name}_id, \$rowAll{$pl_controller_name}->{$lc_controller_name}, array('class' => 'dbplink')); ?> 

    <?php echo $pagination; ?>

    Add a New {$controller_name} Add a New {$controller_name} </textarea>



    View: $view_add_file_name

    <textarea name="code" rows="25" cols="100" id="myCpWindow3" class="codepress php linenumbers-on">

    Add a new $lc_controller_name



    <?php echo form_open('$lc_controller_name/add'); ?>\n\n EOD;

        $i = 1;
        foreach ($fieldType_array as $t)
        {
            $fn = $fieldName_array[$i-1];
            $fd = $fieldDesc_array[$i-1];
            
            if ($i <= count($fieldType_array))
            {
                if ($t == "input")
                {                
                    $code .= <<<EOD
    

        

                          \n\n EOD; } elseif ($t == "checkbox") { $code .= <<<EOD                       \n     \n\n EOD; } elseif ($t == "radio") { $code .= <<<EOD                       \n     \n\n EOD; } elseif (($t == "dropdown") && ($fn == "state")) { $code .= <<<EOD                            \n\n EOD; } elseif (($t == "dropdown") && ($fn != "state")) { $code .= <<<EOD            <?php echo $this->validation->error('$fn'); ?>            <?php            $choices_{$fn}s = array('' => 'Select one:', 'Choice1'=>"Value1", 'Choice2'=>"Value2");
               ?>            <?php echo form_dropdown('{$fn}', $choices_{$fn}s, ($this->input->post('$fn')) ? ($this->input->post('$fn')) : '$defaultValue'); ?>              \n\n EOD; }
    } $i++; }
        $code .= <<<EOD
    

        

                          \n </form>
    $fd:            <?php echo $this->validation->error('$fn'); ?>            <input type="text" name="$fn" value="<?php echo $this->validation->value('$fn'); ?>" size="50" />         
    $fd:            <?php echo $this->validation->error('$fn'); ?>            <input type="text" name="$fn" value="<?php echo $this->validation->value('$fn'); ?>" size="50" />         
    $fd:            <?php echo $this->validation->error('$fn'); ?>            <input type="text" name="$fn" value="<?php echo $this->validation->value('$fn'); ?>" size="50" />         
    $fd:            <?php echo $this->validation->error('$fn'); ?>            <?php            $choices_states = array('' => 'Select one:', 'AL'=>"Alabama", 'AK'=>"Alaska", 'AZ'=>"Arizona", 'AR'=>"Arkansas",
               'CA'=>"California", 'CO'=>"Colorado", 'CT'=>"Connecticut", 'DE'=>"Delaware",
               'DC'=>"District Of Columbia", 'FL'=>"Florida", 'GA'=>"Georgia", 'HI'=>"Hawaii",
               'ID'=>"Idaho", 'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",
               'KY'=>"Kentucky", 'LA'=>"Louisiana", 'ME'=>"Maine", 'MD'=>"Maryland", 'MA'=>"Massachusetts",
               'MI'=>"Michigan", 'MN'=>"Minnesota", 'MS'=>"Mississippi", 'MO'=>"Missouri",
               'MT'=>"Montana", 'NE'=>"Nebraska", 'NV'=>"Nevada", 'NH'=>"New Hampshire",            'NJ'=>"New Jersey", 'NM'=>"New Mexico", 'NY'=>"New York", 'NC'=>"North Carolina",            'ND'=>"North Dakota", 'OH'=>"Ohio", 'OK'=>"Oklahoma", 'OR'=>"Oregon",
               'PA'=>"Pennsylvania", 'RI'=>"Rhode Island", 'SC'=>"South Carolina", 'SD'=>"South Dakota",            'TN'=>"Tennessee", 'TX'=>"Texas", 'UT'=>"Utah", 'VT'=>"Vermont", 'VA'=>"Virginia",
               'WA'=>"Washington", 'WV'=>"West Virginia", 'WI'=>"Wisconsin", 'WY'=>"Wyoming");            ?>            <?php echo form_dropdown('state', $choices_states, ($this->input->post('state')) ? ($this->input->post('state')) : '$defaultValue'); ?>         
     
                <input name="submit" type="submit" id="submit" value="Submit" style="font-family:Verdana; font-size:8pt; font-weight:bold; color:#FFFFFF; height:20; background:#000066; border:1 solid #808080; cursor:hand">         
    </textarea>



    View: $view_edit_file_name

    <textarea name="code" rows="25" cols="100" id="myCpWindow4" class="codepress php linenumbers-on">

    Edit $lc_controller_name



    <?php echo form_open('$lc_controller_name/edit'); ?>\n <?php echo form_hidden('$field_type_id', $this->uri->segment(3)); ?> <?php echo form_hidden('modified_by', $this->session->userdata('user_id')); ?> <?php echo form_hidden('modified_on', date('Y-m-d h:i:s')); ?>\n\n EOD;

        $i = 1;
        foreach ($fieldType_array as $t)
        {
            $fn = $fieldName_array[$i-1];
            $fd = $fieldDesc_array[$i-1];
            
            if ($t == "input")
            {                
                $code .= <<<EOD
    

        

                          \n\n EOD; } elseif ($t == "checkbox") { $code .= <<<EOD                       \n     \n\n EOD; } elseif ($t == "radio") { $code .= <<<EOD                       \n     \n\n EOD; } elseif (($t == "dropdown") && ($fn == "state")) { $code .= <<<EOD                            \n\n EOD; } elseif (($t == "dropdown") && ($fn != "state")) { $code .= <<<EOD            <?php echo $this->validation->error('$fn'); ?>            <?php            $choices{$fn}s = array('' => 'Select one:', 'Choice1'=>"Value1", 'Choice2'=>"Value2");
               ?>            <?php echo form_dropdown('{$fn}', $choices_{$fn}s, ($this->input->post('$fn')) ? ($this->input->post('$fn')) : '$defaultValue'); ?>              \n\n EOD; }
    $i++; }
        $code .= <<<EOD
    

        

                          

    </form>

    $fd:            <?php echo $this->validation->error('$fn'); ?>            <input type="text" name="$fn" value="<?php echo ($this->validation->value('$fn')) ? ($this->validation->value('$fn')) : ($row->$fn);?>" size="25" />         
    $fd:            <?php echo $this->validation->error('$fn'); ?>            <input type="text" name="$fn" value="<?php echo $this->validation->value('$fn'); ?>" size="50" />         
    $fd:            <?php echo $this->validation->error('$fn'); ?>            <input type="text" name="$fn" value="<?php echo $this->validation->value('$fn'); ?>" size="50" />         
    $fd:            <?php            if ((isset($POST)) AND (!$this->input->post('state')))            {               echo "One must be selected
    ";            }            ?>            <?php            $choices_states = array('' => 'Select one:', 'AL'=>"Alabama", 'AK'=>"Alaska", 'AZ'=>"Arizona", 'AR'=>"Arkansas",
               'CA'=>"California", 'CO'=>"Colorado", 'CT'=>"Connecticut", 'DE'=>"Delaware",
               'DC'=>"District Of Columbia", 'FL'=>"Florida", 'GA'=>"Georgia", 'HI'=>"Hawaii",
               'ID'=>"Idaho", 'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",
               'KY'=>"Kentucky", 'LA'=>"Louisiana", 'ME'=>"Maine", 'MD'=>"Maryland", 'MA'=>"Massachusetts",
               'MI'=>"Michigan", 'MN'=>"Minnesota", 'MS'=>"Mississippi", 'MO'=>"Missouri",
               'MT'=>"Montana", 'NE'=>"Nebraska", 'NV'=>"Nevada", 'NH'=>"New Hampshire",            'NJ'=>"New Jersey", 'NM'=>"New Mexico", 'NY'=>"New York", 'NC'=>"North Carolina",            'ND'=>"North Dakota", 'OH'=>"Ohio", 'OK'=>"Oklahoma", 'OR'=>"Oregon",
               'PA'=>"Pennsylvania", 'RI'=>"Rhode Island", 'SC'=>"South Carolina", 'SD'=>"South Dakota",            'TN'=>"Tennessee", 'TX'=>"Texas", 'UT'=>"Utah", 'VT'=>"Vermont", 'VA'=>"Virginia",
               'WA'=>"Washington", 'WV'=>"West Virginia", 'WI'=>"Wisconsin", 'WY'=>"Wyoming");            ?>            <?php /* echo form_dropdown('state', $choices_states, ($this->input->post('state')) ? ($this->input->post('state')) : '$defaultValue'); */ echo form_dropdown('state', $choices_states, $row->state); ?>         
     
                <input name="submit" type="submit" id="submit" value="Submit" style="font-family:Verdana; font-size:8pt; font-weight:bold; color:#FFFFFF; height:20; background:#000066; border:1 solid #808080; cursor:hand">         
    </textarea>



    View: $view_delete_file_name

    <textarea name="code" rows="5" cols="100" id="myCpWindow5" class="codepress php linenumbers-on">
    <?php echo anchor('$lc_controller_name/$view_delete_base/'. $this->uri->segment(3), 'Yes, remove $lc_controller_name', array('class' => 'dbplink')); ?> | <?php echo anchor('$lc_controller_name/view', 'Cancel', array('class' => 'dbplink')); ?> </textarea>

    </form>



    EOD;
        return $code;    
    }
    

    }

    ?> [/code]

    application/controllers/crud.php

    [code] <?php

    class Crud extends MY_Controller {

    function __construct()
    {
        parent::MY_Controller();
        $this->load->library('session');
        $this->load->library('CRUD_Buster');
        //$this->output->enable_profiler(TRUE);
    }
    
    function index()
    {
        if ( !$this->session->userdata('usertype') == "Admin" )
        {
            redirect('home', 'location');
        }
        else
        {
            $rules = array(
                'data_type' => "trim|required|xss_clean",
                   'field_list' => "trim|required|xss_clean",
                   'description_list' => "trim|required|xss_clean",
                   'database_table' => "trim|required|xss_clean",
                   'dbfield_type' => "trim|required|xss_clean",
                   'field_type' => "trim|required|xss_clean"
                   );  
        
            $fields = array(
                'data_type' => "Data Type",
                'field_list' => "List of Fields",
                'description_list' => "Description List",
                'database_table' => "Database Table",
                'dbfield_type' => "Database Field Type",
                'field_type' => "Field Type"
                );
    
            $this->validation->set_rules($rules);
            $this->validation->set_fields($fields);
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            
            if ($this->validation->run() == FALSE)
            {                        
                $data['head_title'] = 'Create Code';
                $this->template->load('mainTemplate', 'crud_buster/code_create', $data);
            }
            else
            {
                $data['code'] = $this->crud_buster->output_controller_code();
                $data['javascript'] = ('');
                
                $data['head_title'] = 'View Code';
                $this->template->load('mainTemplate', 'crud_buster/code_view', $data);
            }
        }            
    }
    

    } ?> [/code]

    views/crud_buster/code_create.php

    [code]

    Create a form

    <?php echo form_open('code_generator/index'); ?>

    <?php echo form_hidden('added_by', $this->session->userdata('user_id')); ?> <?php echo form_hidden('added_on', date('Y-m-d h:i:s')); ?>

    <tr>
        <td valign="top"><span class="FormFieldTitle">Type of data this form will handle<br />(singular -- do not add an "S" at the end!):<br />Example: user</span></td>
        <td valign="top">
            &lt;?php echo $this->validation->error('data_type'); ?&gt;
            &lt;input type="text" name="data_type" value="&lt;?php echo $this-&gt;validation->value('data_type'); ?&gt;" size="25" />
        </td>
    </tr>
    
    <tr>
        <td valign="top"><span class="FormFieldTitle">Field List (separate by |):<br />Example: first_name|last_name|address1</span></td>
        <td valign="top">
            &lt;?php echo $this->validation->error('field_list'); ?&gt;
            &lt;textarea name="field_list" rows="4" cols="70"&gt;&lt;?php echo $this->validation->value('field_list'); ?&gt;&lt;/textarea&gt;
        </td>
    </tr>    
    
    <tr>
        <td valign="top"><span class="FormFieldTitle">Field Descriptions (separate by |):<br />Example: First Name|Last Name|Street Address</span></td>
        <td valign="top">
            &lt;?php echo $this->validation->error('description_list'); ?&gt;
            &lt;textarea name="description_list" rows="4" cols="70"&gt;&lt;?php echo $this->validation->value('description_list'); ?&gt;&lt;/textarea&gt;
        </td>
    </tr>
    
    <tr>
        <td valign="top"><span class="FormFieldTitle">Database Table:</span></td>
        <td valign="top">
            &lt;?php echo $this->validation->error('database_table'); ?&gt;
            &lt;textarea name="database_table" rows="4" cols="70"&gt;&lt;?php echo $this->validation->value('database_table'); ?&gt;&lt;/textarea&gt;
        </td>
    </tr>    
    
    <tr>
        <td valign="top"><span class="FormFieldTitle">Database Field Types (separate by |):<br />Example: first_name VARCHAR(200) NULL PRIMARY KEY auto_increment|last_name VARCHAR(150) NULL|address1 VARCHAR (150) NULL</span></td>
        <td valign="top">
            &lt;?php echo $this->validation->error('dbfield_type'); ?&gt;
            &lt;textarea name="dbfield_type" rows="4" cols="70"&gt;&lt;?php echo $this->validation->value('dbfield_type'); ?&gt;&lt;/textarea&gt;
        </td>
    </tr>     
    
    <tr>
        <td valign="top"><span class="FormFieldTitle">Form Field Types (separate by |):<br />Valid types: input|textarea|dropdown|checkbox|radio</span></td>
        <td valign="top">
            &lt;?php echo $this->validation->error('field_type'); ?&gt;
            &lt;textarea name="field_type" rows="4" cols="70"&gt;&lt;?php echo $this->validation->value('field_type'); ?&gt;&lt;/textarea&gt;
        </td>
    </tr>                          
    
    <tr>
        <td valign="top">&nbsp;</td>
        <td valign="top"><br>
    
            &lt;input name="submit" type="submit" id="submit" value="Create Code" style="font-family:Verdana; font-size:8pt; font-weight:bold; color:#FFFFFF; height:20; background:#000066;
        border:1 solid #808080; cursor:hand"&gt;
    
        </td>
    </tr>
    

    </form>

    [/code]

    views/crud_buster/code_view.php

    [code] <?php echo $code; ?> [/code]

    Clone this wiki locally