如何获得屏幕分辨率并将其放入PHP变量中

如何获得屏幕分辨率并将其放入PHP变量中,php,javascript,jquery,screen,Php,Javascript,Jquery,Screen,大家好,我有个大问题要解决。我知道我必须使用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

大家好,我有个大问题要解决。我知道我必须使用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值将根据用户的窗口屏幕进行更改。即=如果窗口屏幕为(某个值),则$cols=(某个值)

我真的很感激在这方面的任何帮助


干杯

您可以在页面加载时使用ajax脚本之类的操作。这将使您的网站速度变慢,但它应该具有正确的效果:

HTML

PHP



这将允许您返回所需的任何列布局。尽管如此,我相信制作一些好的CSS来制作所需的列会更容易。

好吧,使用一些Javascript来获得分辨率,然后通过AJAX发送,或者作为get请求发送给PHP脚本,并将其存储在会话变量中。我建议通过JS设置cookie,然后在PHP中获取cookie的值。只要通过JS将cookie设置在尽可能靠近HTML文档顶部的位置,如果您使用的话,我会将它放在您的字符集声明之后。我还建议您使用CSS。媒体查询在这方面很好,但您也可以浮动元素以支持旧浏览器。
这会使您的站点变慢
您的意思是创建AJAX请求会显著降低站点的呈现速度吗?如果是这样的话,我怀疑它是否会引人注目。但是,如果您考虑到AJAX请求的延迟,那么您是对的,用户的延迟将在这里扮演重要角色。很好的CSS调用,这是这里使用的正确技术。是的,我的意思是AJAX请求将根据用户延迟而延迟。尤其取决于从数据库中提取多少对象。谢谢你!
<div class="container">
    <!-- Place columns here -->
</div>
$(document).load(function(){
    var winWidth = $(window).width();
    $.post('phpscript', {width: winWidth}, function(data) {
        $('.container').html(data);
    });
});
<?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
?>