【问题标题】:How to get the screen resolution and put it into a PHP variable [duplicate]如何获取屏幕分辨率并将其放入 PHP 变量 [重复]
【发布时间】:2013-04-19 19:10:53
【问题描述】:

大家好,我在尝试解决这个问题时遇到了一个大问题。我知道我必须使用 JavaScript,但我就是不明白如何使用。这是我的脚本:

<?php 
$posts = query_posts('order=DESC&cat=-2');
$cols = 4;
$filas = array_chunk($posts, $cols);
$columnas = array();
foreach( $filas as $row => $articulos ) {
foreach( $articulos as $ix => $articulo ) {
  $columnas[$ix] = isset($columnas[$ix]) ? $columnas[$ix] : array();
  $columnas[$ix][] = $articulo;
}
}
?>
<?php foreach($columnas as $posts):?> etc, etc ... <?php endforeach; ?>

$cols 值将根据用户的窗口屏幕而变化。 IE。 = 如果窗口屏幕是(某个值)那么 $cols =(某个值)

我将非常感谢任何帮助。

干杯!

【问题讨论】:

  • 好吧,使用一些 Javascript 来获取分辨率,然后通过 AJAX 或作为 GET 请求发送到 PHP 脚本,将其存储在会话变量中。只有之后你才能在脚本中使用它。
  • 我建议通过 JS 设置一个 cookie,然后在 PHP 中获取该 cookie 的值。只需通过 JS 将 cookie 设置为尽可能靠近 HTML 文档的顶部,如果您使用它,我会将它放在您的 charset 声明之后。
  • 我是否也建议使用 CSS。媒体查询对此非常有用,但您也可以浮动元素以支持旧浏览器。'

标签: php javascript jquery screen


【解决方案1】:

您可以执行类似使用 ajax 脚本加载页面的操作。这会使您的网站变慢,但应该有正确的效果:

HTML

<div class="container">
    <!-- Place columns here -->
</div>

jQuery

$(document).load(function(){
    var winWidth = $(window).width();
    $.post('phpscript', {width: winWidth}, function(data) {
        $('.container').html(data);
    });
});

PHP

<?PHP
    //Your PHP script to format your page in however many columns
    //Have your script here take the width of your page
    //Do do math to find how many columns
    //Query whatever you need to get those columns
    //Return the HTML
?>

这将允许您返回您想要的任何列布局。不过,我确信制作一些好的 CSS 来制作尽可能多的列会更容易。

【讨论】:

  • This will make your site slower 的意思是创建 AJAX 请求会显着减慢网站的呈现速度吗?如果是这样,我怀疑它会引起注意。但是,如果您正在考虑 AJAX 请求的延迟,那么您是对的,用户的延迟将在这里发挥重要作用。对 CSS 的好调用,这是在这里使用的正确技术。
  • 是的,我的意思是 AJAX 请求会因用户延迟而滞后。特别是取决于您从数据库中提取的对象数量。谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多