Welcome Guest, Not a member yet? Register   Sign In
Using helper to format table fields in controller
#1

[eluser]mlynno[/eluser]
Sorry if this is a repetitive question, I have not been able to find an answer by searching. I'm trying to create a table view that takes several fields and pulls them together (using a helper) to apply special display rules to those fields. I'm using the generate tables function, CI 2.02. Here is my helper (it is autoloaded and works fine when called directly into view files):
Code:
<?php
function display_full_botanical_name($record) {
    printf("%s %s %s %s %s %s %s %s %s %s %s",
        $record['genus'] ? format_genus($record['genus'],$record['cross_genus']) : "",
        $record['specific_epithet'] ? format_species($record['specific_epithet'], $record['cross_species']) : "",
        $record['infraspecific_epithet'] ? format_subspecies($record['infraspecific_epithet_designator'], $record['infraspecific_epithet']) : "",
        $record['cultivar'] ? format_cultivar($record['cultivar']) : "",
        $record['trade_name'] ? format_trade_name($record['trade_name']) : "",
        $record['registered'] ? format_registered_name($record['registered']) : "",
        $record['trademark'] ? format_trademark_name($record['trademark']) : "",
        $record['plant_patent_number'] ? format_patent_number($record['plant_patent_number']) : "",
        $record['plant_patent_number_applied_for'] ? "PPAF" : "",
        $record['plant_breeders_rights'] ? format_breeders_rights() : "",
        $record['plantname_group'] ? format_plantname_group($record['plantname_group']) : ""
    );
}
// followed by a number of display rules ie
function format_genus($genus, $genus_cross) {
    $genus_cross = $genus_cross ? "χ" : "";
    $genus = ucfirst($genus);
    return "<span class='genus'>$genus_cross$genus</span>";
}
Here is the applicable part of my controller:
Code:
function index($page = 0)
    {
        $this->load->model('listplants_model');
        $records = $this->listplants_model->get_records($page);
        $total = $this->db->count_all_results('plant_data');
        $path = "listplants/index";
        $this->show_plants($page, $records, $total, $path);
    }
// search stuff ...//
function show_plants($page, $records, $total, $path, $query = '') { if ($records->num_rows() > 0)
            {
                $table = array();
                $table[] = array(
                    'ID',
                    'Family',
                    'Genus',
                    'X Genus',
                    'Specific Epithet',
                    'Designator',
                    'Infraspecific Epithet',
                    'x Species',
                    'Group',
                    'Cultivar',
                    'Trade Name',
                    'PP#',
                    'PPAF',
                    'PBR',
                    'View',
                    'Edit',
                    'Images',
                    'Delete'
                    );
                foreach ($records->result() as $row)
                {
                    $table[] = array(
                        $row->id,
                        $row->family,
                        $row->genus,
                        ...etc.
                        }                }
                $data['records'] = $table;
            }
and model:
Code:
class Listplants_model extends CI_Model {
    function get_records($page) {
        $this->db->limit(30, $page);
        return $this->db->get('plant_data');        
    }
and view:
Code:
if ( !empty($records)) {
        echo $this->table->generate($records);  
    } else {
        echo "No records found.";
    }
This shows the plant list table with all the fields just fine; but is there a way I can apply that plant_helper function in the controller so that the fields "family" through "PBR" are replaced by the display_full_botanical_name rules instead? Sorry, that is a lot of code for what is probably a simple question. Any help would be greatly appreciated. Thank you!
#2

[eluser]mlynno[/eluser]
Never mind -- problem solved... need a return function not a print.




Theme © iAndrew 2016 - Forum software by © MyBB