 |
ขอความช่ว่ยเหลือหน่อ่ยครับ เรื่องการดึงข้อมูลจาก database มาโชว์ครับ |
|
 |
|
|
 |
 |
|
เขียนโปรแกรมให้แสดง 10 รายการได้หรือยัง เขียนเสร็จแล้ว เอามาแสดง แล้วจะบอกต่อให้ว่า จะแก้ไขตรงไหน
ถ้าทำให้ทั้งหมด บอกตรงๆ ขี้เกียจเหมือนกัน
|
 |
 |
 |
 |
Date :
2023-03-31 10:18:36 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมทำได้แต่ ดึงข้อมูลมาโชว์ทั้ง 40 รายการเลยอะครับ ยังแบ่งไม่เป็นเลยครับ ผมต้องใช้ คีย์เวิดอะไรในการไปเซริทหาวิธี แบ่งทีละ 10 รายการครับ
|
 |
 |
 |
 |
Date :
2023-04-08 00:32:13 |
By :
wattana2812 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (JavaScript)
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var batch = 0; // Current batch number
// Function to fetch and display the next batch of records
function fetchNextBatch() {
$.getJSON('get_records.php', {batch: batch}, function(data) {
if (data.length > 0) {
// Append the records to your HTML element
// For example, assuming you have a <div> element with id "records"
var recordsDiv = $('#records');
$.each(data, function(index, record) {
recordsDiv.append('<p>Record ' + record.id + ': ' + record.data + '</p>');
});
// Increment the batch number for the next fetch
batch++;
}
});
}
// Fetch the first batch of records
fetchNextBatch();
// Call the fetchNextBatch() function every 10 seconds
setInterval(fetchNextBatch, 10000);
</script>
get_records.php
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "my_database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Get the current batch number from the request
$batch = isset($_GET['batch']) ? intval($_GET['batch']) : 0;
// Calculate the offset for the LIMIT clause
$offset = $batch * 10;
// Fetch 10 records from the database
$query = "SELECT * FROM my_table LIMIT $offset, 10";
$result = $mysqli->query($query);
// Fetch the records as an array
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
// Close the database connection
$mysqli->close();
// Return the records as JSON data
header('Content-Type: application/json');
echo json_encode($rows);
?>
|
 |
 |
 |
 |
Date :
2023-04-08 10:23:14 |
By :
009 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : wattana2812 เมื่อวันที่ 2023-04-10 00:25:50
รายละเอียดของการตอบ ::
ผมแยกให้แล้ว client/server scripts
คือคุณต้องการให้แสดงข้อมูลชุดละ 10 วิ นั่นแหละตัวอย่าง
การจะปรับแก้อะไรที่เกิดขึ้นกับ user โดยตรง...ทำที่ client-side
ซึ่งก็คือ JS และการสั่งให้โปรแกรมทำงานอัตโนมัติทุกๆ ช่วงระยะเวลาที่เรากำหนด
ใช้
1. setTimeout();
https://www.w3schools.com/jsref/met_win_settimeout.asp
หรือ
2. setInterval();
https://www.w3schools.com/jsref/met_win_setinterval.asp
แต่ถ้าอยากทำใน PHP อย่างเดียว (ฝืนธรรมชาติ) มีทางเลือก คือ ทำ event loop
หรือไป websocket ซึ่งอันหลังแม้จะดีสุด แต่ก็ไกลตัว
นี่คือที่มาทำไมผมให้ตัวอย่างนี้
ไม่เข้าใจตรงไหนให้ถามเป็นจุดๆ
ไม่เข้าใจทั้งหมด ศึกษาพื้นฐาน HTML/JS ประกอบเพิ่มเติม
(จริงๆ บรรทัดไหนทำอะไร มีคอมเมนต์อธิบายไว้แล้ว แปลไม่ออก ใช้ google translate)
|
 |
 |
 |
 |
Date :
2023-04-10 07:46:34 |
By :
009 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
web server มันไม่ส่ง content type text/html หรือยังไงนั่นน่ะ
อย่างกับมันส่งเป็น text/plain เฉยๆเลย
|
 |
 |
 |
 |
Date :
2023-04-18 15:47:01 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|