我有一个PHP脚本,它在一个漂亮的网格中显示一堆数据库结果并对它们进行分页。
我正在试图弄清楚如何使用下拉系统创建一种方法来更改每页显示的结果数量,以便我开始使用AJAX。
我有一个变量来确定要显示的项目数量,所以我已经创建了几个具有完全相同的PHP代码的页面,但该变量的编号不同。它完成了它的预期目标,但我不禁感到我已经开始以最低效的方式实现这一点,因为我必须使用不同的变量创建另外三个页面。
如何改进?
这是php代码。我曾经在页面本身中使用它,但是将它移动到ajax看到的另一个文件。
<?php
//Add the file that connects to the database
include("C://wamp/www/site/config.php");
//Selects which database to get the data from
mysql_select_db("products");
//Variables for pagination
$per_page = 24;
// get the number of items that actually have Oven in their name
$sql = "SELECT SUM(rowcount)
FROM (
SELECT COUNT(1) AS rowcount
FROM brand1
WHERE description LIKE '%oven%'
UNION ALL
SELECT COUNT(1)
FROM brand2
WHERE description LIKE '%oven%'
) AS counts";
$pages_query = mysql_query ($sql) ;
//Ceil rounds up to the nearest number so that we don't get pages with decimals on the end
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
//append a page=1,2,3 at the end of the URL to indicate which page the user is on
//set : 1 so that if a person doesn't pick a page it automatically sets them on page 1.
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page -1) * $per_page;
//Selects which table to extract the data from
$i = 0;
$result = mysql_query("SELECT * FROM brand1 WHERE description LIKE '%oven%' UNION ALL SELECT * FROM brand2 WHERE description LIKE '%oven%' LIMIT $start, $per_page") ;
$dyn_table = '<table border ="1" cellpadding ="10">';
//create a variable to store the data in
while($row = mysql_fetch_array($result))
{
//Variables to store vendors information, product number and image as retrieved from the database
$brand = $row["Brand"];
$model_number = $row["Model Number"];
$product_image = $row["Image"];
//Set up a loop that puts the information into a grid rather than a single line
if($i%4==0) //First number sets the column number
{
// Starts to draw the table. Adds the vendor name, the product number then breaks line to draw the image underneath the titles
$dyn_table .= '<tr><td>' . $brand . " " . $model_number . '<br>' . $product_image .'</td>';
}
else
{
//This line does the same thing, but once the first if condition hits 4, it jumps to this line to contine drawing.
$dyn_table .= '<td>' . $brand . " " . $model_number . '<br>' . $product_image . '</td>';
}
//Simply adds 1
$i++;
}
//Adds the ending of the table
$dyn_table .= '</tr></table>';
//Draws the table
echo $dyn_table;
//Set the pagination links at the bottom of the page
if ($pages >=1 && $page <= $pages)
{
for($x=1; $x<=$pages; $x++)
{
echo ($x == $page) ? '<strong><a href ="?page='.$x.'">'.$x.'</a></strong> ' : '<a href ="?page='.$x.'">'.$x.'</a> ';
}
}
?>
和AJAX(我刚刚从W3中抓住了一个例子
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://localhost/site/products/ovens/products.php",true);
xmlhttp.send();
}
</script>
答案 0 :(得分:1)
在这里,有一个很无聊的答案。
你的代码很好,超出了mysql_
家族和史前手册ajax之外的整个污点。您的分页方法是可靠的,并遵循通常的模式,找出您可能有多少页,然后根据该限制抓取当前页面的结果。你也拥有所有正确的验证位。
你说得对,这是有点低效的,但除非你有一个方便的地方坚持预先计算出的最大结果数/最大页数,否则很少有办法解决它。你可能已经有了一个。仔细检查您的数据库结构。您还应确保具有适当的索引。是时候玩EXPLAIN
了。
我唯一能推荐的可能是不相关的。看起来您可能需要read up on database normalization。您的表名(brand1,brand2)表明您可能有两个完全相同的表格,唯一的区别是它们包含针对两个不同“品牌”的数据。这可以很容易地在一个表中完成,并将品牌添加为另一个列。
答案 1 :(得分:0)
根据您的描述,我假设您有类似page1.php
,page2.php
......
制作page.php
,阅读$_GET['page']
以查看您应该在哪个页面进行操作,并在HTML链接中找到page.php?page=2
及类似内容。
正如查尔斯所说,这与我提供的详细程度一样多。