我有一个 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>