PHP code example of lavdiu / php-grid

1. Go to this page and download the library: Download lavdiu/php-grid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lavdiu / php-grid example snippets




use PhpGrid\PhpGrid;
use PhpGrid\Column;
use PhpGrid\ActionButton;

$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

$grid = new PhpGrid($pdo, 'contacts_list');
$grid->setTitle('List of all contacts')
    ->setRowsPerPage(10)
    ->setSqlQuery("SELECT id, name, email, created_on FROM contact_list")
    ->addColumn(new Column('id', 'Contact Id', true, true, '?mod=contact&id={id}', '_blank'))
    ->addColumn(new Column('email', 'Email Address'))
    ->addActionButton(new ActionButton('View', '?mod=contact&id={id}', 'fa fa-eye'))
    ->addActionButton(new ActionButton('Update', '?mod=contact&id={id}&action=update', 'fa fa-pencil'));

/**
 * Setting custom attributes to the button
 */
$deleteButton = new ActionButton('Delete', '?mod=contact&id={id}&action=delete', 'fa fa-trash');
$deleteButton->addAttribute('onclick', "return confirm('Are you sure?');");
$grid->addActionButton($deleteButton);

/**
 * Set custom style/classes to the cell itself
 */
$col1 = new Column('name', 'Full Name');
$col1->setOuterElementCssClass('text-center');
$col1->setOuterElementCssStyle('background-color:silver');
$grid->addColumn($col1);

/**
 * Set custom style/classes to the content of the cell
 */
$col2 = new Column('created_on', 'Registration Date', true);
$col2->setInnerElementCssClass('border border-danger');
$col2->setInnerElementCssStyle('color:red;cursor:pointer;');
$grid->addColumn($col2);

$grid->setDebug(true); #output additional debugging info in json responses

if ($grid->isReadyToHandleRequests()) {
    $grid->bootstrap();
}

echo $grid->draw();