public static function create($sqliteFile, $newRecords = 32) { if (!file_exists($sqliteFile)) { $dbh = Sqlite::connect($sqliteFile); try { $sql = "CREATE TABLE IF NOT EXISTS user (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT,\n email TEXT,\n address TEXT,\n phone TEXT);"; $create = $dbh->prepare($sql); $create->execute(); // Start adding dummy data to the db with the help of Faker $faker = Faker\Factory::create(); for ($i = 1; $i <= $newRecords; $i++) { $sth = $dbh->prepare('INSERT INTO user (name, email, address, phone) VALUES (:name, :email, :address, :phone)'); $sth->bindParam(':name', $faker->name, PDO::PARAM_STR); $sth->bindParam(':email', $faker->email, PDO::PARAM_STR); $sth->bindParam(':address', $faker->address, PDO::PARAM_STR); $sth->bindParam(':phone', $faker->phoneNumber, PDO::PARAM_STR); $sth->execute(); // Echo a point for each new record echo '. '; } echo '<br><strong>' . $newRecords . ' new records were created</strong><hr>'; } catch (PDOException $ex) { echo $ex->getMessage(); } } else { echo 'Attention! Remove <code>UserFaker::create()</code> line from your code. ' . $sqliteFile . ' database already exists.<hr>'; } }
<?php require_once 'vendor/autoload.php'; use leoshtika\libs\Pagination; use leoshtika\libs\Sqlite; use leoshtika\libs\UserFaker; $sqliteFile = 'demo.sqlite'; // Create a new sqlite db if not exists and load some dummy data. // After the database is created, you don't need this line of code anymore UserFaker::create($sqliteFile, 120); $dbh = Sqlite::connect($sqliteFile); // Get the total number of records $totalRecords = $dbh->query('SELECT count(*) FROM user')->fetch(PDO::FETCH_COLUMN); // Instantiate the Pagination $pagination = new Pagination($_GET['page'], $totalRecords, 10); // Get records using the pagination $sth = $dbh->prepare('SELECT * FROM user LIMIT :offset, :records'); $sth->bindValue(':offset', $pagination->offset(), PDO::PARAM_INT); $sth->bindValue(':records', $pagination->getRecordsPerPage(), PDO::PARAM_INT); $sth->execute(); $users = $sth->fetchAll(PDO::FETCH_OBJ); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Pagination</title> <!-- Bootstrap --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> </head> <body class="container-fluid">