简体   繁体   中英

Grabbing screen resolution and displaying results based on the size

I'm trying to show results from a database based on screen resolution size. I posted a question last week about how to get the width and was able to come up with this jquery with the help of a few ideas here, but was unable to complete the second aspect, which is to display results based on the size:

<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>

Now, this is the second part:

<?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); 
?>

The max amount of results to show is set to 21, but I want to use the jquery above to define the amount based on a user's screen resolution; I will have various sizes. In other words, I need the if statement to say "show this many results based on the size found." So how do I write that if statement?

* LAST REVISION= Partially WORKING, (sets all results to max 6 despite resolution size) *

<?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; 

.....

Using Ajax, this is what needs to be done

<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>

That file.php would then run your database query and return a nicely formatted set of results (ie html table) based on the h and w parameters sent via ajax which tell you the height and width of the screen. To do this just set the $page variable depending on the size of the $_GET['w'] and $_GET['h'] variables. See below:

<?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
?>

You can calculate $maxresults as follows:

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;

Or you can group these statements by width (as I think thats less important than height, meaning less scrolling down for the user):

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

Revision

You code for populating maxresults is very poorly written, thats why it does not work. Try this:

<?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;

This will either output -1 if w and h are not being sent or it will return 24 or 12 and so on depending on your screen width. Learn to do some basic debugging on your code. It is difficult for me to debug your code without having all of it to hand.

Just load the ajax by passing the answer via de $_GET['windowsize'] and use the results that are returned via the "data" variable(this can be HTML that is inserted into the DOM but also an json string or what ever you want it to be.

<script type="text/javascript">
$(document).ready(function() {
    var $window = $(window);
    function checkWidth() {
        var windowsize = $window.width();
        if (windowsize = 1600) {
            $.ajax('/path/to/page?windowsize='+encodeURIComponent(windowsize)).done(function(data){
                        // the stuff to do when the php is loaded, for example, document.getElementById('x').innerHTML=data;
                        });
        }
    }
    // Execute on load
    checkWidth();
    // Bind event listener
    $(window).resize(checkWidth);
});

</script>

in the php script you could post:

<?php
$windowsize = (int)$_GET['windowsize'];
if($windowsize >= 1200 && $windowsize < 1400)
    {
    echo "maxsize:200;minsize:300;";
    }
if($windowsize >= 1400 && $windowsize < 1600)
    {
    echo "maxsize:300;minsize:400;";
    }

then in the javascript you could use:

..........done(function(data) {
    $results = data.split(';');
    for(c=0;c<$results.length;c++)
        {
        $tmp = $results[c].split(':');
        if($tmp[0] == 'minsize')
            {
            minsize=parseInt($tmp[1]);
            }
        if($tmp[0] == 'maxsize')
            {
            maxsize=parseInt($tmp[1]);
            }
        });

One quick solution would be to submit the screen-resolution per Ajax to the php script. Then use the resolution to determine the amount and return the movies for example as json.

Something like this:

**EDIT

javascript:

$(document).ready(function() {'use strict';

   var screenResolution, request;

   //set screen resolution here
   screenResolution = 1024;

   request = $.ajax({
     url : "url_to_php_script",
     type : "POST",
     dataType : "json",
     contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
     data : {
        "screenResolution" : screenResolution               
     }
  });
   request.fail(function() {
      alert("fail");
   });

   request.done(function(movies) {
      console.log(movies);
   });
});

php:

$screenResolution = $_POST['screenResolution'];

//DO your normal Stuff

$jsonResult= json_encode($result);

return $jsonResult; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM