Wiki: CMS Grid Helper v5.5 - 2016.07.07

Topnew grid helper (cms/cms_grid.php) is a PHP function to display data into HTML grid automatically.

<?php cms_grid($data, $col, $fk); ?>

$data is an array of information to be displayed into HTML grid

$col is an array of column configuration as how the grid to be displayed

$fk is an array of foreign key values used by $data lookup

$data

$data = cms_rows('select id, name, company_id, male from users');
//data of above SQL might look like as following:
$data = [
    ['id'=>1,  'name'=>'Topnew Geo', 'company_id'=>5, 'male'=>1],
    ['id'=>800,'name'=>'Ben Osolle', 'company_id'=>9, 'male'=>1],
    ['id'=>9,  'name'=>'Marry Cool', 'company_id'=>3, 'male'=>0],
    ...
];

Without any configuration, the grid might be shown as following:

$data = cms_rows('select id, name, company_id, male from users');
cms_grid($data);
ID Name CompanyID Male
1 Topnew Geo 5 1
800 Ben Osolle 9 1
9 Marry Cool 3 0

$fk : foreign keys

Let's start with the easy and simple part: foreign keys. As you noticed above, you might need to display company name instead of company id, and male / female instead of 1 / 0:

$data = cms_rows('select id, name, company_id, male from users');
$col = [
    'id' => [],
    'name' => [],
    'company_id' => ['fk'=>1],
    'male' => ['fk'=>'gender'],
];
$fk['gender'] = ['F', 'M'];
$fk['company_id'] = cms_list('select id,company from companies');
cms_grid($data, $col, $fk);

And here is the new grid result:

ID Name CompanyID Male
1 Topnew Geo Happy Farm Inc M
800 Ben Osolle Fortune City Ent. M
9 Marry Cool Google P/L F

As you noticed above 'fk' can be 2 values:

fk = 1 will be default to same key value of the column name, in above example, company_id::fk=1 means $fk['company_id'], and it will be same if you specified as 'fk'=>'company_id'

fk = key_name as above male.fk = gender is getting foreign keys from $fk['gender']

$col['table'] : grid table, header, footer settings

$col = [
    'table' => [
        'table_class' => 'css class for table',
        'table_style' => 'css style for table',
        'thead_class' => 'css class for thead',
        'thead_style' => 'css style for thead',
        'tfoot_class' => 'css class for tfoot',
        'tfoot_style' => 'css style for tfoot',
        'foot' => 1,//to display tfoot, otherwise none
    ],
    //rest of col definitions
];

$col : grid column definitions

If $col not set, it will display all data column and in the default sequence

You can setup $col which columns of data to show and in which sequence and format:

$col = [
    'field_name' => [
        label => Text For thead, if null default to Field Name
        label_class => css class for thead.th,
        label_style => css style for thead.th,
        value => field_name, unique key for data[][key]
        value => can also like this: {{addr}} {{city}} {{state}} {{zip}} etc where keys are of data[][key]
        class => css class for td
        style => css style for td
        url => /path/{{id}}.html, print a href
        img => /path/{{id}}.jpeg, print img
        url_class => css class for url or img
        url_style => css style
        foot => NULL,any text,sum,count,min,max,avg
        format => format|2, substr|0|5, data|Y-m-d, dollar, etc
        fk => 1 == $fk[field_name][value]
        fk => user == $fk['user'][value]
        pref => text before a value
        post => text after a value
    ],
    'field_name2' => -1,//do not show this field in grid
    ...
];

Now let's do with a real example:

$data = cms_rows('select id, name, company_id, male from users');
$col = [
    'table' => [
        'table_class' => 'table',
        'thead_class' => 'bg-gold',
        'tfoot_class' => 'bg-dark',
        'foot' => 1,
    ],
    'id' => [
        'url' => '/user/{{id}}.html',
        'foot' => 'Do not click',
    ],
    'name' => [
        'foot' => 'count',
    ],
    'company_id' => [
        'fk' => 1,
        'label' => 'Company',
        'foot' => 'max',
    ],
    'male' => [
        'label' => 'Gender',
        'fk' => 'gender',
        'class' => 'ac',
    ],
];
$fk['gender'] = ['F', 'M'];
$fk['company_id'] = cms_list('select id,company from companies');
cms_grid($data, $col, $fk);

And here is the result:

ID Name Company Gender
1 Topnew Geo Happy Farm Inc M
800 Ben Osolle Fortune City Ent. M
9 Marry Cool Google P/L F
Do not click 3 Happy Farm Inc

Projects - 其他项目:

bank BenSon Bank
Cash Manager

blog Blog Forum
Blog BBS Ticket

chart SVG Chart
PHP SVG Chart

save Page Maker
Page Maker

topnew SIDU DB GUI
Database tool