[英]PHP/MySQL copy data in chunks from one table to another via a load more button
I am sure their is probably a better way... Is it possible to copy items form table1(column)
to table2(column)
in data row chunks of 25 with a load more button? 我确信它们可能是更好的方法...是否可以通过加载更多按钮将数据table2(column)
项从table1(column)
复制到table2(column)
,数据行的块数为25?
How would I do this? 我该怎么做? Where would I find information on this? 我在哪里可以找到有关此信息? Thank you. 谢谢。
I hope this will help you. 我希望这能帮到您。 You can implement it in php to load more action. 您可以在php中实现它以加载更多操作。
every time after clicking more button change offset and limit in mysql query 每次单击更多按钮后,更改mysql查询中的偏移量和限制
INSERT INTO table2 SELECT * FROM table1 LIMIT 0, 25;
load more...
INSERT INTO table2 SELECT * FROM table1 LIMIT 25, 25;
load more...
INSERT INTO table2 SELECT * FROM table1 LIMIT 50, 25;
.... ....
.... ....
Commplete code. 完整的代码。
1.Just copy and paste following code in page1.php 1.只需将以下代码复制并粘贴到page1.php中
<div id='message'></div>
<a href='#' id='LoadMore' >Load More</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
type="text/javascript"></script>
<script>
$(function() {
var page = 1;
$("#LoadMore").click(function(){
$.ajax({
type:"GET",
url:"page2.php",
data:{page:page},
success: function(response) {
$("#message").append(response);
page++;
}
});
});
});
</script>
2.Copy following code in page2.php 2.将以下代码复制到page2.php中
and change mysql_server, mysql_user, mysql_password, database_name argument in first two line 并在前两行更改mysql_server,mysql_user,mysql_password,database_name参数
<?php
//set argument as your mysql server
$connect = mysql_connect("mysql_server","mysql_user","mysql_password");
mysql_select_db("database_name",$connect);
$page = isset($_GET["page"]) ? $_GET["page"] : 1;
$limit = 25;
$offset = ($page - 1) * $limit;
$sql = "INSERT INTO table2 SELECT * FROM table1 limit $offset, $limit";
mysql_query($sql);
$rows = mysql_affected_rows();
echo "$rows rows added to table2 from table1<br>";
?>
3.Run page1.php in browser... and load data to table2 3.在浏览器中运行page1.php ...并将数据加载到table2
Now showing data from table2 without refreshing page as required (user2714387 said in comment) 现在显示来自table2的数据,而无需按要求刷新页面(user2714387在评论中说)
4.Copy and paste following code in page3.php 4.将以下代码复制并粘贴到page3.php中
<table width="300" border="1" id='data_grid'></table>
<a href='javascript://' id='LoadMore' >Load More</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
type="text/javascript"></script>
<script>
$(function() {
var page = 1;
$("#LoadMore").click(function(){
$.ajax({
type:"GET",
url:"page4.php",
data:{page:page},
success: function(response) {
$("#data_grid").append(response);
page++;
}
});
});
});
</script>
4.Copy following code in page4.php 4.将以下代码复制到page4.php中
<?php
//set argument as your mysql server
$connect = mysql_connect("mysql_server","mysql_user","mysql_password");
mysql_select_db("database_name",$connect);
$page = isset($_GET["page"]) ? $_GET["page"] : 1;
$limit = 25;
$offset = ($page - 1) * $limit;
$sql = "SELECT * FROM table2 limit $offset, $limit";
$result = mysql_query($sql);
$numRows = mysql_num_rows($result);
if($numRows>0) {
while($row = mysql_fetch_array($result)) {
//get field data and set to the following row
echo "<tr><td>field 1</td><td>field 2</td><td>field 3</td></tr>";
//edit row as you table data
}
} else {
echo "<tr><td colspan='3'> No more data </td></tr>";
}
exit;
?>
6.Run page4.php in browser 6,在浏览器中运行page4.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.