Crud-through-a-Json-API
Crud through a JSON API
ArtiGrid can also generate a full CRUD grid from external JSON data sources such as APIs.
In this example, data is fetched using cURL, transformed into an array structure, and then passed to the grid using crudJson()
<?php
// 1. Consume the API with cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/todos");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// 2. Convert to array
$data = json_decode($response, true);
// 3. Take only the first 20 records
$data = array_slice($data, 0, 20);
// 4. Create rows for the grid with the fields you need
$rows = [];
foreach ($data as $item) {
$id = $item['id'];
$rows[] = [
'id' => $id,
'name' => "Usuario $id",
'email' => "usuario$id@mail.com",
'created_at' => date('Y-m-d', strtotime("-$id days")),
'title' => $item['title'],
'completed' => $item['completed'] ? 'Sí' : 'No'
];
}
// 5. Define grid columns
$columns = [
['name'=>'id','label'=>'ID','type'=>'number'],
['name'=>'name','label'=>'Nombre','type'=>'text'],
['name'=>'email','label'=>'Correo','type'=>'text'],
['name'=>'created_at','label'=>'Fecha','type'=>'date'],
['name'=>'title','label'=>'Título','type'=>'text'],
['name'=>'completed','label'=>'Completado','type'=>'text'],
];
// 6. Create and configure the grid
$grid2 = new ArtiGrid();
$grid2->perPage(10);
$grid2->addCustomBtn(
'btn btn-sm btn-info', // class
'ver', // action JS
'', // icon
[] // conditions
);
// 7. Prepare JSON for the grid
$jsonData = [
'columns' => $columns,
'rows' => $rows
];
// 8. Render the grid
echo $grid2->crudJson($jsonData)->render();
?>
CRUD Generated from PHP Session Data
ArtiGrid can also build a complete CRUD grid using data stored in PHP sessions.
This is useful when you want to work with temporary data without saving anything to a database.
In this example, we store products inside $_SESSION, transform the session array into grid rows, and render the grid dynamically.
<?php
session_start();
// 1. Create session data (only if it doesn't exist)
if (!isset($_SESSION['cart_products'])) {
$_SESSION['cart_products'] = [
[
'id' => 1,
'product' => 'Laptop',
'price' => 1200,
'quantity' => 1,
'date_added' => date('Y-m-d')
],
[
'id' => 2,
'product' => 'Mouse',
'price' => 25,
'quantity' => 2,
'date_added' => date('Y-m-d')
],
[
'id' => 3,
'product' => 'Keyboard',
'price' => 80,
'quantity' => 1,
'date_added' => date('Y-m-d')
]
];
}
// 2. Get rows from session
$rows = $_SESSION['cart_products'];
// 3. Define grid columns
$columns = [
['name'=>'id','label'=>'ID','type'=>'number'],
['name'=>'product','label'=>'Product','type'=>'text'],
['name'=>'price','label'=>'Price','type'=>'number'],
['name'=>'quantity','label'=>'Quantity','type'=>'number'],
['name'=>'date_added','label'=>'Date Added','type'=>'date'],
];
// 4. Prepare JSON structure
$jsonData = [
'columns' => $columns,
'rows' => $rows
];
// 5. Create ArtiGrid instance
$grid = new ArtiGrid();
$grid->perPage(5)
->modal();
// 6. Render grid from session data
echo $grid->crudJson($jsonData)->render();
?>