-1

I want to display a php variable in a specific window width.

Example:

In my HTML I have <div class="scount"></div> with no content.

I'd like to display <?php $sumcount=3; ?> as a content on scount div element on a width that is greater than 768 else display <?php $sumcount=1; ?> that is less than 768

Please help, I badly need your cooperation. :)

bellabelle
  • 737
  • 6
  • 12
  • This isn't something to be solved with PHP; this is something to be solved with a CSS framework. Look into bootstrap or foundation. – Tim Morton Nov 08 '17 at 04:13
  • Ahh okay, but could you please add samples because I'm not that an expert on php – bellabelle Nov 08 '17 at 04:14
  • 1
    I didn't, although it deserves it. This question has nothing to do with PHP. It is a question about something on the browser side. By the time the browser renders it, the PHP is done. You need to be looking at a CSS and/or AJAX solution. – Tim Morton Nov 08 '17 at 04:19
  • ahh right ajax, thanks Tim... I have looking some samples but can't understand how to implement this on ajax – bellabelle Nov 08 '17 at 04:20
  • AJAX is the most accessible way to get information back to the server where PHP lives. You'll have to use javascript to determine screen size (assuming javascript will do that; I don't know), send that information back to the server, and then have the server send the content that you can then have the browser display. – Tim Morton Nov 08 '17 at 04:24
  • https://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php – Tim Morton Nov 08 '17 at 04:33

2 Answers2

1

If you want to detect what device is being used then you should examine

$_SERVER['HTTP_USER_AGENT']

for clues.

Dmitriy Buteiko
  • 574
  • 1
  • 5
  • 14
  • Can you have some samples, so I can understand more? I'm quite new to php – bellabelle Nov 08 '17 at 04:11
  • @bellabelle this varialbe contains user's browser name.If it is browser from Android/IPhone device, then more likely width of device will less than 768px. – Dmitriy Buteiko Nov 08 '17 at 04:15
  • ahh okay, but still I'm searching some code on how to do this – bellabelle Nov 08 '17 at 04:21
  • No, this doesn't directly answer your question. It may give you a clue to make a more educated **guess** about mobile, but it will not help you one little bit for desktop. – Tim Morton Nov 08 '17 at 04:32
-1

Another option is to use CSS. Create two divs and have the CSS effect them based on screen size

    @media screen and (max-width: 768px) {
        div1 {
            display: block;
        }
        div2 {
            display: none;
        }
    }

    @media screen and (min-width: 768px) {
        div1 {
            display: none;
        }
        div2 {
            display: block;
        }
    }

Otherwise if you need the screensize for PHP, you need to use JavaScript (AJAX)

ezw
  • 381
  • 2
  • 12
  • Hello ezw, I've tried this and add php as a content on each div elements but the php code runs even i display it to none – bellabelle Nov 08 '17 at 04:13
  • PHP will run, but CSS will display the right div block due to screen size – ezw Nov 08 '17 at 04:38