shenkwen
shenkwen

Reputation: 3880

What is the best practice to detect users' screen width?

In my joomla website, I have a module that outputs 1 item by default, in its help.php file, there is something like

$items = 1;
$db->setQuery($query, 0, $items)

Now I want to detect if users' screen width is less than a certain value, if yes, then get only 1 item, else get 4 items. What is the best practice to do it?

I can just set $items to 4 and use javascript to eliminate the other 3 items, but this is not a perfect solution because excessive data will be downloaded. My questions are: 1) can PHP alone detect client's screen width? 2) If no and I have to use javascript/ajax, I am thinking add

<script>
    var w = screen.width;
    href = location.href;
    if (location.search){
       href = href + '&screenWidth=' + w;
    } else
    href = href + '?screenWidth=' + w;
</script>

to every page and use $_GET['screenWidth] to get the screen width value in the helper.php file, is this a good practice? Is this gonna mess up with output buffer sif there is any) set by my CMS(joomla)?

Upvotes: 0

Views: 169

Answers (2)

Fluinc
Fluinc

Reputation: 491

You can't detect the screen size with PHP cause it is only run on the server. You can try Mobile Detect it wont tell you the screen size but it will let you know what kind of device it is.

Upvotes: 0

plushyObject
plushyObject

Reputation: 1131

Just give the items a class with CSS that will hide them on mobile.

@media(max-width: 768px){
    .some-class{
        display: none;
    }
}

Upvotes: 2

Related Questions