使用Ajax,这就是需要做的事情
<html><body>
<div id="content"></div>
....
<script>
$(function(){
$.ajax({
url: 'file.php',
type: 'GET',
data: {h: screen.height, w: screen.width}
}).done(function ( data ) {
document.getElementById("content").innerHTML=data;
});
});
</script>
然后,该 file.php 将运行您的数据库查询,并根据通过 ajax 发送的 h 和 w 参数返回一组格式良好的结果(即 html 表),这些参数告诉您屏幕的高度和宽度。为此,只需根据页面的大小设置 $page 变量即可$_GET['w']
and $_GET['h']
变量。见下文:
<?php
// get the function
include_once ('function.php');
$maxresults = 21;
$page = ( $_GET['page'] ? $_GET['page'] : 0 );
if(($_GET['w']) && ($_GET['h'])) {
$w = $_GET['w'];
$h = $_GET['h'];
//$maxresults = // calculate this depending on w and h
// i.e. if h > 1600 $maxresults = 20, else = 10
$currentpage = $page;
$page = $page * $maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM books WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetBooksByRangeID($page, $maxresults);//Show <maxresults> pages at a time
//DisplayResults($result);
echo $results; // in a nice format (i.e. table) to be inserted into div via ajax
?>
您可以按如下方式计算 $maxresults:
if($w > 640) && ($h > 480) $maxresults = 5;
if($w > 800) && ($h > 600) $maxresults = 7;
if($w > 1024) && ($h > 768) $maxresults = 12;
....
if($w > 2560) && ($h > 1600) $maxresults = 21;
或者您可以按宽度对这些语句进行分组(因为我认为宽度不如高度重要,这意味着用户向下滚动的次数更少):
if ($w <= 1024) {
if ($h >= 768) $maxresults = 12; // 1024x(768 or higher)
else if ($h >= 600) $maxresults = 8; // 1024x(600~768)
else $maxresults = 6; // 1024x(599 or below)
}
else if ($w <= 1280) {
if ($h >= 1024) $maxresults = 14; // 1280x(1024 or higher)
else if ($h >= 960) $maxresults = 12; // 1280x(960~1024)
else if ($h >= 800) $maxresults = 10; // 1280x(800~960)
else if ($h >= 768) $maxresults = 8; // 1280x(768~800)
else $maxresults = 6; // 1280x(768 or below)
}
//and so on
see: http://en.wikipedia.org/wiki/Display_resolution#Computer_monitors http://en.wikipedia.org/wiki/Display_resolution#Computer_monitors
Revision
您用于填充 maxresults 的代码写得非常糟糕,这就是它不起作用的原因。尝试这个:
<?php
include_once ('function.php');
$maxresults = -1;
if(($_GET['w']) && ($_GET['h'])) {
$w = $_GET['w'];
$h = $_GET['h'];
if ($w == 1920) {
$maxresults = 24;
} else if ($w == 1600) {
$maxresults = 24;
} else if ($w == 1440){
$maxresults = 12;
} else if ($w == 1366) {
$maxresults = 10;
} else if ($w == 1024) {
$maxresults = 8;
} else
$maxresults = 6;
}
echo $maxresults;
如果 w 和 h 没有被发送,这将输出 -1,或者将返回 24 或 12 等等,具体取决于您的屏幕宽度。学习对代码进行一些基本的调试。如果我没有掌握所有代码,我很难调试您的代码。