PHP code example of simp / modal

1. Go to this page and download the library: Download simp/modal 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/ */

    

simp / modal example snippets


$db = new PDO('mysql:host=database;dbname=lamp', 'lamp', 'lamp');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$userModal = UserTestModal::getModal($db);
$roleModal = RoleTestModal::getModal($db);

$db->exec("DELETE FROM {$roleModal->getTable()}");
$db->exec("ALTER TABLE {$roleModal->getTable()} AUTO_INCREMENT = 1");
$db->exec("DELETE FROM {$userModal->getTable()}");
$db->exec("ALTER TABLE {$userModal->getTable()} AUTO_INCREMENT = 1");

$aliceId   = $userModal->fill(['name' => 'Alice', 'email' => '[email protected]', 'password' => 'secret'])->insert();
$bobId     = $userModal->fill(['name' => 'Bob', 'email' => '[email protected]', 'password' => 'pass'])->insert();
$charlieId = $userModal->fill(['name' => 'Charlie', 'email' => '[email protected]', 'password' => '1234'])->insert();

$roleModal->fill(['role_name' => 'Admin', 'uid' => $aliceId])->insert();
$roleModal->fill(['role_name' => 'Editor', 'uid' => $bobId])->insert();
$roleModal->fill(['role_name' => 'Subscriber', 'uid' => $bobId])->insert();
$roleModal->fill(['role_name' => 'Viewer', 'uid' => $charlieId])->insert();

$usersSelected = $userModal
    ->select(['id', 'name'])
    ->limit(3)
    ->orderBy('id', 'ASC')
    ->get();

$alice = $userModal->where('id', '=', $aliceId)->first();
$aliceRole = $roleModal->where('uid', '=', $alice['id'])
                       ->select(['role_name', 'uid'])
                       ->first();

$bob = $userModal->where('id', '=', $bobId)->first();
$bobRoles = $roleModal->where('uid', '=', $bob['id'])
                      ->select(['role_name','uid'])
                      ->get();

$editorRole = $roleModal->where('role_name', '=', 'Editor')->first();
$editorUser = $userModal->where('id', '=', $editorRole['uid'])
                        ->select(['name', 'email'])
                        ->first();

$complexUsers = $userModal
    ->whereIn('id', [$aliceId, $bobId])
    ->orWhere('name', '=', 'Charlie')
    ->select(['id', 'name', 'email'])
    ->orderBy('id', 'DESC')
    ->get();

// Attach hasMany roles for each user
foreach ($complexUsers as &$user) {
    $user['roles'] = $roleModal->where('uid', '=', $user['id'])
                               ->select(['role_name','uid'])
                               ->get();
}

$updateCount = $userModal->where('id', '=', $aliceId)
                         ->fill(['name' => 'Alice Updated'])
                         ->update();

$updatedUser = $userModal->where('id', '=', $aliceId)->first();

$deleteCount = $roleModal->where('role_name', '=', 'Viewer')->delete();
$remainingRoles = $roleModal->get();