php-抓取屏幕分辨率并根据大小显示结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-抓取屏幕分辨率并根据大小显示结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4119字,纯文字阅读大概需要6分钟。
内容图文
![php-抓取屏幕分辨率并根据大小显示结果](/upload/InfoBanner/zyjiaocheng/695/cf44fb98fdbe405bb89235ef633dc119.jpg)
我试图显示基于屏幕分辨率大小的数据库结果.上周,我发布了一个有关如何获取宽度的问题,并借助此处的一些想法提出了这个jquery,但无法完成第二个方面,即根据尺寸显示结果:
<script type="text/javascript">
$(document).ready(function() {
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize = 1600) {
//**This is where I need to define the $maxresults value, but how?**
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
</script>
现在,这是第二部分:
<?php
// get the function
include_once ('function.php');
$maxresults = 21;
if ($_GET['page']) {
$page = $_GET['page'];
}
else {
$page = 0;
}
$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM books WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
//Show <maxresults> pages at a time
$result = GetBooksByRangeID($page, $maxresults);
DisplayResults($result);
?>
要显示的最大结果数设置为21,但是我想使用上面的jquery根据用户的屏幕分辨率来定义数量;我将有各种尺寸.换句话说,我需要if语句说“根据找到的大小显示很多结果”.那我该怎么写if语句呢?
*上次修订=部分工作,(无论分辨率大小,都将所有结果设置为最大6)*
<?php
// get the function
include_once ('function.php');
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;
…..
解决方法:
使用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表).为此,只需根据$_GET [‘w’]和$_GET [‘h’]变量的大小设置$page变量.见下文:
<?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
查看:http://en.wikipedia.org/wiki/Display_resolution#Computer_monitors
调整
您用于填充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,依此类推,具体取决于您的屏幕宽度.学习对代码进行一些基本的调试.如果没有所有代码,我很难调试您的代码.
内容总结
以上是互联网集市为您收集整理的php-抓取屏幕分辨率并根据大小显示结果全部内容,希望文章能够帮你解决php-抓取屏幕分辨率并根据大小显示结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。