PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- INIT database -- Create the table CREATE TABLE courses ( course_id INT, course_name VARCHAR(50), course_parent_id INT, course_status INT ); -- Insert data into the table INSERT INTO courses (course_id, course_name, course_parent_id, course_status) VALUES (1, 'linux', 0, 1), (2, 'html', 0, 0), (3, 'css', 0, 1), (4, 'php', 0, 1), (5, 'linuxbasics', 1, 1), (6, 'linuxadvance', 1, 1), (7, 'commands', 5, 1), (8, 'newcommands', 6, 1), (9, 'htmlbasics', 2, 1), (11, 'cssbasics', 3, 1), (12, 'cssadvance', 3, 1), (13, 'phpbasics', 4, 1), (14, 'phpadvance', 4, 1), (15, 'phpexpert', 4, 1);
Copy Clear
Copy Format Clear
<?php $page = 1; // change using the list in pages $limit = 100; $offset = ($page - 1) * $limit; $stmt = $pdo->query("SELECT COUNT(*) FROM courses"); $total_records = $stmt->fetchColumn(); $total_pages = ceil($total_records / $limit); echo "pages: "; for ($i = 1; $i <= $total_pages; $i++) { echo "$i | "; } echo "\n\n"; if($page > $total_pages) die('invalid page'); $stmt = $pdo->prepare("SELECT * FROM courses LIMIT :limit OFFSET :offset"); $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt->bindParam(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $records = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($records as $record) { echo json_encode($record) . "\n"; }
Show:  
Copy Clear