การแบ่งหน้าใน CodeIgniter 4 พร้อมตัวอย่าง
⚡ สรุปอย่างชาญฉลาด
การแบ่งหน้าใน CodeIgniter แบ่งชุดผลลัพธ์ขนาดใหญ่จากฐานข้อมูลออกเป็นหน้าเล็กๆ ที่สามารถนำทางได้โดยใช้ไลบรารีการแบ่งหน้าในตัว บทแนะนำนี้จะสร้างตัวอย่างการทำงานที่เรียกใช้ตารางผู้เขียน กำหนดขีดจำกัดต่อหน้า และแสดงลิงก์นำทางที่มีหมายเลขกำกับ

ฐานข้อมูลเปรียบเสมือนสิ่งมีชีวิต มันเติบโตขึ้นเรื่อยๆ ตามกาลเวลา เมื่อออกแบบแอปพลิเคชัน เราจำเป็นต้องคำนึงถึงจำนวนระเบียนมากมายที่ผู้ใช้จะต้องค้นหา การแบ่งหน้าช่วยให้เราสามารถแบ่งผลลัพธ์ออกเป็นส่วนเล็กๆ ที่จัดการได้ง่าย ภาพต่อไปนี้แสดงตัวอย่างการแบ่งหน้าในฐานข้อมูล Google ค้นหา.
ในบทเรียนนี้ คุณจะได้เรียนรู้วิธีการแบ่งหน้าผลลัพธ์จากฐานข้อมูล Codeการใช้งานไลบรารีการแบ่งหน้าใน Igniter บทช่วยสอนนี้สมมติว่าคุณคุ้นเคยกับพื้นฐานของการแบ่งหน้าอยู่แล้ว CodeIgniter Active Record หากคุณยังไม่รู้จัก สามารถอ่านบทแนะนำก่อนหน้านี้ได้ และข้อสันนิษฐานอีกประการหนึ่งคือ คุณได้ดาวน์โหลด Igniter Active Record แล้ว Codeตัวจุดไฟ
การกำหนดค่าฐานข้อมูล
เราจะเริ่มต้นด้วยการสร้างฐานข้อมูลและแทรกบันทึกจำลองบางส่วนลงไป มันจะเป็นฐานข้อมูลตารางเดียวที่มี 50 ระเบียนอยู่
เรียกใช้สคริปต์ต่อไปนี้กับ MySQL เพื่อสร้างตารางผู้เขียนฐานข้อมูล แทรกบันทึกจำลอง 50 รายการ
CREATE SCHEMA ci_pagination; DROP TABLE IF EXISTS `authors`; CREATE TABLE `authors` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `last_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `birthdate` date NOT NULL, `added` timestamp NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (1, 'Brielle', 'O\'Hara', 'alexandre67@example.com', '1974-05-21', '1999-09-17 19:17:28'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (2, 'Flavio', 'Lehner', 'mafalda85@example.net', '1971-10-14', '1998-09-09 00:25:06'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (3, 'Elmira', 'Johns', 'wellington52@example.org', '1983-11-28', '2015-02-07 15:56:43'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (4, 'Elza', 'Mitchell', 'marisol46@example.org', '1989-03-08', '1992-08-21 00:21:39'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (5, 'Viva', 'Greenfelder', 'era94@example.com', '1995-04-17', '2017-04-30 05:55:39'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (6, 'Maxwell', 'VonRueden', 'mcassin@example.net', '1994-07-01', '1996-05-08 23:30:14'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (7, 'Deontae', 'Becker', 'rory.kub@example.org', '1992-02-19', '2017-07-22 11:49:15'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (8, 'Sylvester', 'Christiansen', 'erohan@example.org', '1990-09-03', '2004-05-08 08:15:37'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (9, 'Torrey', 'Strosin', 'adams.luciano@example.net', '1999-10-09', '2009-08-30 21:30:44'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (10, 'Kelli', 'Smitham', 'daniel.catalina@example.com', '2012-11-07', '1986-01-22 20:52:57'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (11, 'Abigale', 'Schuppe', 'andreanne.hayes@example.net', '2018-02-18', '1994-05-07 06:26:36'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (12, 'Letha', 'Gleason', 'eldridge.heaney@example.net', '2009-02-06', '1998-05-25 04:37:54'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (13, 'Sienna', 'Upton', 'monique57@example.org', '2017-08-17', '2009-08-08 19:08:10'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (14, 'Harley', 'Gutkowski', 'breilly@example.net', '1987-11-30', '1998-07-31 11:08:01'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (15, 'Lois', 'Bednar', 'hpouros@example.net', '2002-02-11', '2001-07-26 15:04:16'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (16, 'Gail', 'McDermott', 'reina.kerluke@example.com', '1987-03-30', '2004-12-15 20:38:29'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (17, 'Sydney', 'Strosin', 'dweber@example.net', '1985-04-29', '2010-08-07 08:50:35'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (18, 'Anne', 'Cremin', 'fritz.schmitt@example.com', '1976-02-15', '1980-03-25 05:29:41'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (19, 'Norberto', 'Bergnaum', 'franecki.javon@example.net', '1971-03-11', '1993-03-20 23:36:25'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (20, 'Arvid', 'Johns', 'pschultz@example.org', '2016-01-03', '1995-04-30 17:07:15'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (21, 'Bernita', 'Fay', 'arne96@example.org', '1983-12-26', '1987-02-23 16:55:28'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (22, 'Gonzalo', 'Gorczany', 'velma.huels@example.com', '1987-10-19', '2016-10-18 19:25:46'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (23, 'Jackie', 'Klein', 'gretchen.howe@example.com', '1971-01-20', '2010-11-16 02:58:05'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (24, 'Andrew', 'Kessler', 'collins.faye@example.org', '1984-08-07', '1971-12-15 08:46:42'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (25, 'Claire', 'Hudson', 'madalyn.wunsch@example.org', '1984-12-19', '1991-07-04 14:35:53'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (26, 'Prudence', 'Runte', 'koch.laurie@example.net', '2008-12-27', '1985-09-30 04:57:55'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (27, 'Destinee', 'Romaguera', 'tromp.tiffany@example.com', '1978-11-09', '1997-11-09 19:58:08'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (28, 'Marianna', 'Harvey', 'clovis.schuppe@example.com', '2013-08-28', '1990-08-06 19:29:19'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (29, 'Eloy', 'Lakin', 'wmorissette@example.net', '1989-12-08', '1974-03-16 03:21:16'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (30, 'Rubie', 'McClure', 'haley.louisa@example.net', '1987-09-12', '1998-07-02 10:45:36'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (31, 'Marisa', 'Stracke', 'zachary76@example.org', '1975-05-28', '1975-03-19 00:57:35'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (32, 'Jacey', 'Beatty', 'dahlia.hermann@example.org', '1979-12-17', '1971-01-21 16:50:58'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (33, 'Idella', 'Ruecker', 'elda.reichert@example.com', '2009-09-15', '1996-04-19 22:27:31'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (34, 'Dimitri', 'Bednar', 'freida.parker@example.org', '1998-12-02', '2008-12-30 23:29:57'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (35, 'Elfrieda', 'Smitham', 'padberg.rex@example.org', '2018-07-23', '1972-04-01 07:52:25'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (36, 'Dwight', 'Walter', 'gmosciski@example.org', '2016-08-15', '1994-07-02 11:06:55'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (37, 'Macie', 'Fay', 'uschuppe@example.net', '1976-01-29', '2005-12-13 18:44:46'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (38, 'Lori', 'Kshlerin', 'mhansen@example.net', '1983-04-08', '1986-01-18 16:03:52'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (39, 'Jeffry', 'Paucek', 'alisha35@example.net', '2002-10-02', '2004-03-29 07:06:03'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (40, 'Yvonne', 'Bernhard', 'jaqueline21@example.net', '2017-11-04', '1986-12-15 23:55:23'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (41, 'Ludwig', 'Heathcote', 'marcelino.kirlin@example.org', '1981-02-09', '2000-08-02 20:45:48'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (42, 'Jadyn', 'Wiegand', 'georgianna.swift@example.net', '1982-06-16', '1980-12-05 13:09:37'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (43, 'Ferne', 'Smitham', 'melany22@example.com', '1996-12-17', '1984-11-18 19:26:27'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (44, 'Meta', 'Corkery', 'xharber@example.com', '1972-03-31', '2007-01-20 00:07:31'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (45, 'Toni', 'Wolf', 'wyman.crystal@example.org', '2012-04-15', '1973-06-22 12:14:37'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (46, 'Zack', 'Luettgen', 'deion.konopelski@example.org', '2003-05-29', '2018-11-19 05:03:21'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (47, 'Kameron', 'Dietrich', 'ronaldo.torphy@example.com', '1973-05-20', '1974-09-16 20:27:17'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (48, 'Zetta', 'Lebsack', 'timmothy.vandervort@example.com', '1996-11-04', '2001-03-06 01:33:01'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (49, 'Benny', 'Hane', 'leone.lueilwitz@example.net', '2008-02-02', '2006-01-27 22:12:39'); INSERT INTO `authors` (`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (50, 'Evans', 'Gleason', 'ccummerata@example.org', '1999-09-09', '1990-09-26 17:50:11');
เมื่อเราสร้างฐานข้อมูลสำเร็จและแทรกข้อมูลจำลองลงไปแล้ว ขั้นตอนต่อไปคือการตั้งค่าฐานข้อมูลของเรา Codeแอปพลิเคชัน Igniter ใช้สำหรับติดต่อสื่อสารกับฐานข้อมูล
เปิด application/config/database.php
ตั้งค่าพารามิเตอร์การเชื่อมต่อฐานข้อมูลคล้ายกับต่อไปนี้
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'melody', 'database' => 'ci_pagination', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
ที่นี่
- 'hostname' => 'localhost' ตั้งชื่อโฮสต์ของฐานข้อมูล
- 'username' => 'root' ระบุชื่อผู้ใช้ฐานข้อมูล
- 'password' => 'melody', ตั้งรหัสผ่านฐานข้อมูล
- 'database' => 'ci_pagination' ระบุชื่อฐานข้อมูล
เราจะทำการโหลดไลบรารีฐานข้อมูลโดยอัตโนมัติเมื่อแอปพลิเคชันของเราเริ่มต้นทำงาน มาเริ่มกันเลย เปิดไฟล์ application/config/autoload.php
โหลดไลบรารีฐานข้อมูลตามที่แสดงโดยโค้ดด้านล่าง
$autoload['libraries'] = array('database');
เพียงเท่านี้สำหรับการกำหนดค่าฐานข้อมูล
ต่อไปเรามาเริ่มสร้างแบบจำลองฐานข้อมูลสำหรับการแบ่งหน้ากันเลย
Codeโมเดลฐานข้อมูลการแบ่งหน้า Igniter
สร้างโมเดล Authors_model ใหม่ใน application/models
เพิ่มโค้ดดังต่อไปนี้:
<?php class Authors_model extends CI_Model { protected $table = 'authors'; public function __construct() { parent::__construct(); } public function get_count() { return $this->db->count_all($this->table); } public function get_authors($limit, $start) { $this->db->limit($limit, $start); $query = $this->db->get($this->table); return $query->result(); } }
ที่นี่
- protected $table = 'authors'; กำหนดตัวแปรแบบ protected ที่เก็บชื่อตารางในฐานข้อมูลสำหรับโมเดล
- public function __construct() {…} เรียกเมธอดตัวสร้างพาเรนต์
- ฟังก์ชันสาธารณะ get_count() {…} จะส่งคืนจำนวนระเบียนทั้งหมดในตารางฐานข้อมูล ซึ่งจำเป็นสำหรับวัตถุประสงค์ในการแบ่งหน้า
- ฟังก์ชันสาธารณะ get_authors($limit, $start) {…} กำหนดเมธอดที่จะใช้ในการดึงผลลัพธ์แบบแบ่งหน้าจากฐานข้อมูล โดยเราส่งค่า limit และ start เข้าไป limit กำหนดจำนวนระเบียนทั้งหมดที่จะส่งคืน ในขณะที่ start กำหนดจำนวนระเบียนที่จะข้ามไป
แค่นี้ก็เสร็จสิ้นสำหรับแบบจำลองฐานข้อมูลของเราแล้ว ต่อไปเรามาสร้างเส้นทางที่จะตอบสนองต่อผลลัพธ์แบบแบ่งหน้ากัน
Codeเส้นทางการแบ่งหน้าของ Igniter
เปิดไฟล์เส้นทางใน application/config/routes.php
เพิ่มเส้นทางดังต่อไปนี้
$route['authors/(:num)'] = 'authors';
ที่นี่
- เรากำหนดเส้นทางสำหรับผู้เขียนที่รับพารามิเตอร์เสริมเป็นตัวเลข โดยกำหนดพารามิเตอร์เสริมโดยใช้เครื่องหมายวงเล็บเปิดและปิด เส้นทางที่กำหนดนี้จะเรียกเมธอด index ในคอนโทรลเลอร์ Authors
ต่อไปเราจะมาดูตัวควบคุมสำหรับผลลัพธ์แบบแบ่งหน้ากัน
Codeตัวควบคุมการแบ่งหน้า Igniter
สร้างไฟล์ Authors.php ใหม่ในไดเร็กทอรี application/controllers
เพิ่มโค้ดดังต่อไปนี้:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Authors extends CI_Controller { public function __construct() { parent:: __construct(); $this->load->helper('url'); $this->load->model('authors_model'); $this->load->library("pagination"); } public function index() { $config = array(); $config["base_url"] = base_url() . "authors"; $config["total_rows"] = $this->authors_model->get_count(); $config["per_page"] = 10; $config["uri_segment"] = 2; $this->pagination->initialize($config); $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; $data["links"] = $this->pagination->create_links(); $data['authors'] = $this->authors_model->get_authors($config["per_page"], $page); $this->load->view('authors/index', $data); } }
ที่นี่
- ผู้เขียนคลาสขยาย CI_Controller {…} เรากำหนดผู้เขียนคลาสที่ขยายคลาส CI_Controller
- ฟังก์ชั่นสาธารณะ __construct() {…} วิธีการนี้เริ่มต้นตัวสร้างพาเรนต์และโหลดตัวช่วย url โมเดลผู้เขียนและไลบรารีการแบ่งหน้า
- public function index() {…} กำหนดวิธีการที่ตอบสนองต่อผู้เขียนเส้นทางของเรา
- $config[“base_url”] = base_url() . “authors”; ตั้งค่าการแบ่งหน้า URL ซึ่งจะนำมาใช้สร้างลิงก์การแบ่งหน้า
- $config[“total_rows”] = $this->authors_model->get_count(); กำหนดจำนวนแถวทั้งหมดที่ต้องแบ่งหน้า โดยค่าที่ได้จะมาจากโมเดลผู้เขียนโดยการเรียกใช้เมธอด get_count
- $config[“per_page”] = 10; กำหนดแถวที่ต้องการแสดงในแต่ละหน้า
- $config[“uri_segment”] = 2; ระบุค่า URL ส่วนที่ประกอบด้วยค่าที่จะใช้ในการข้ามระเบียน
- $this->pagination->initialize($config); จะเริ่มต้นใช้งานไลบรารีการแบ่งหน้าโดยใช้ค่าจากอาร์เรย์ config ที่เรากำหนดไว้
- $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; ตรวจสอบว่ามีการตั้งค่าหมายเลขข้ามในส่วนที่สองของ URI หรือไม่ หากไม่ได้ตั้งค่าไว้ ค่า 0 จะถูกกำหนดค่าให้กับตัวแปร $page
- $data[“links”] = $this->pagination->create_links(); สร้างลิงก์การแบ่งหน้าและกำหนดให้กับคีย์ลิงก์ของตัวแปรอาร์เรย์ $data
- $data['authors'] = $this->authors_model->get_authors($config[“per_page”], $page); ดึงข้อมูลผู้เขียนแบบแบ่งหน้าและกำหนดให้กับคีย์ authors ของตัวแปรอาร์เรย์ $data
- $this->load->view('ผู้เขียน/ดัชนี', $data); โหลดมุมมองดัชนีในไดเรกทอรีผู้เขียนและส่งผ่านตัวแปรอาร์เรย์ $data
ส่วนคอนโทรลเลอร์ของเราก็เสร็จเรียบร้อยแล้ว ต่อไปเรามาสร้างวิวที่จะแสดงผลลัพธ์จากฐานข้อมูลกัน
สร้างผู้เขียนไดเรกทอรีใหม่ในแอปพลิเคชัน/มุมมอง
สร้างไฟล์ใหม่index.phpในapplication/views/authors/index.php
เพิ่มโค้ดดังต่อไปนี้:
<!DOCTYPE html> <html> <head> <title>CodeIgniter Pagination</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https:0 </head> <body> <div class="container"> <h3 class="title is-3">CodeIgniter Database Pagination</h3> <div class="column"> <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth"> <thead> <tr> <th>ID</th> <th>Contact Name</th> <th>Contact Number</th> <th>Email</th> <th>City</th> </tr> </thead> <tbody> <?php foreach ($authors as $author): ?> <tr> <td><?= $author->id ?></td> <td><?= $author->first_name ?></td> <td><?= $author->last_name ?></td> <td><?= $author->email ?></td> <td><?= $author->birthdate ?></td> </tr> <?php endforeach; ?> </tbody> </table> <p><?php echo $links; ?></p> </div> </div> </body> </html>
ที่นี่
- วนลูปผ่านผลลัพธ์ของตัวแปร `$authors` และพิมพ์ผลลัพธ์ลงในตาราง
- พิมพ์ลิงก์การแบ่งหน้าที่ด้านล่างของตารางของเรา
ตอนนี้เรามาเริ่มใช้งานแอปพลิเคชันและดูผลลัพธ์กันเลย
ในบทช่วยสอนนี้ เราใช้เว็บเซิร์ฟเวอร์ PHP ในตัว แต่คุณสามารถใช้เว็บเซิร์ฟเวอร์ใดก็ได้ที่รองรับ PHP.
เปิดเทอร์มินัล
เรียกใช้คำสั่งต่อไปนี้
cd C:\Sites\ci-app php -S localhost:3000
ที่นี่
- คำสั่งดังกล่าวเรียกดูไดเรกทอรีรหัสแอปพลิเคชันและเริ่มเซิร์ฟเวอร์ในตัวบนพอร์ต 3000
หมายเหตุ: เส้นทางของแอปพลิเคชันต้องตรงกับเส้นทางที่คุณดาวน์โหลดไฟล์มา Codeโปรแกรม Igniter คุณสามารถใช้พอร์ตหมายเลขใดก็ได้ที่ว่างอยู่บนคอมพิวเตอร์ของคุณ ไม่จำเป็นต้องใช้พอร์ต 3000
เปิดเว็บเบราว์เซอร์และโหลดเนื้อหาต่อไปนี้ URL
http://localhost:3000/authors
คุณควรจะได้รับผลลัพธ์ที่คล้ายกับต่อไปนี้
วิธีการปรับแต่ง Codeลิงก์การแบ่งหน้าของ Igniter
โดยค่าเริ่มต้น เมธอด create_links() จะสร้างแท็ก anchor ธรรมดา คุณสามารถห่อหุ้มลิงก์เหล่านี้ด้วยมาร์กอัปของคุณเองเพื่อให้ตรงกับเฟรมเวิร์ก CSS เช่น Bootstrap หรือบูลม่า เพิ่มคีย์การจัดสไตล์ลงในอาร์เรย์ $config เดียวกันก่อนเรียกใช้ initialize()
$config['full_tag_open'] = '<nav><ul class="pagination">'; $config['full_tag_close'] = '</ul></nav>'; $config['num_tag_open'] = '<li class="page-item">'; $config['num_tag_close'] = '</li>'; $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link">'; $config['cur_tag_close'] = '</a></li>'; $config['next_link'] = 'Next »'; $config['prev_link'] = '« Previous';
ปุ่มตั้งค่าที่มีประโยชน์ที่สุดมีรายละเอียดดังต่อไปนี้
- full_tag_open / full_tag_close: โครงสร้างมาร์กอัปที่ครอบคลุมชุดลิงก์ทั้งหมด เหมาะสำหรับ หรือ คอนเทนเนอร์.
- จำนวนแท็กเปิด / จำนวนแท็กปิด: ใส่เครื่องหมายกำกับรอบลิงก์หมายเลขหน้าแต่ละลิงก์
- cur_tag_open / cur_tag_close: ใส่เครื่องหมายกำกับรอบหมายเลขหน้าปัจจุบันที่ใช้งานอยู่
- ลิงก์ถัดไป / ลิงก์ก่อนหน้า: ข้อความหรือสัญลักษณ์ที่ใช้สำหรับปุ่มถัดไปและปุ่มก่อนหน้า
เนื่องจากรูปแบบการแสดงผลทั้งหมดถูกจัดเก็บไว้ในไฟล์การกำหนดค่า คุณจึงสามารถปรับแต่งรูปแบบการแบ่งหน้าทั่วทั้งแอปพลิเคชันได้โดยการแก้ไขอาร์เรย์เพียงชุดเดียว โดยไม่ต้องแตะต้องตรรกะของคอนโทรลเลอร์เลย


