0

I have using this php code for add clearfix div <div class="clearfix"></div> after 6 item in sequence. & it is working fine.

Here, I want to set Counter according to device width. that means, if, device width is more than 1024. So, Counter will calculate 6(add clearfix div after 6 item). & if device width is less than 1024 So. Counter will calculate 4(add clearfix div after 4 item).

I have this code: I have this code:

<?php $counter = 0; ?>
    <?php foreach ($categories as $category) { ?>

<div class="item"></div>
<?php 
        if(++$counter % 6 === 0) { ?>
          <div class="clearfix"></div>
        <?php  
        }
    } 
    ?>

It would look like this:

if device width is > 1024

    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="clearfix"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="clearfix"></div>
    <div class="item"></div>
    <div class="item"></div>

if device width is < 1024

    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="clearfix"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="clearfix"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="clearfix"></div>
    <div class="item"></div>
    <div class="item"></div>

Some help would be appreciated. Thank you!

HDP
  • 3,003
  • 2
  • 28
  • 50
  • 2
    You can't detect screen width with PHP : http://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php You need some Javascript to do that. (Or explain the purpose of your code to find another way to achieve your goal) – ClmentM Aug 04 '14 at 12:10
  • @Harnish have you tried my answer – Satish Sharma Aug 05 '14 at 04:56

3 Answers3

2

PHP runs on server. it doesn't know about the client machine width. howerver JS can get the width of the screen. so here is some trick to solve this

Step1: store width in cookie by JS on client machine when user comes on site by this code

var w = window.innerWidth;
document.cookie="screen_width="+w;

Step2: Now get cookie in php when needed the width of the screen

$screen_width = $_COOKIE['screen_width'];

Step3: Now you can use this width in your php code in the condition

<?php 
    $counter = 0;
    $screen_width = $_COOKIE['screen_width'];

    $limit = 4;
    if($screen_width>1024)
    {
        $limit = 6;
    }

    foreach ($categories as $category){
?>  
    <div class="item"></div>
        <?php 
        if(++$counter % $limit === 0) 
        { 
        ?>
          <div class="clearfix"></div>
        <?php  
        }
    } 
?>
Satish Sharma
  • 9,217
  • 6
  • 24
  • 49
  • nice way but for the `1st visit` there wont be any cookie named `screen_width` since server side execution takes place first and html is served which sets the cookie. `if its an ajax request it will work in such case too.` – Karan Thakkar Aug 04 '14 at 12:43
  • ya we can do like that – Satish Sharma Aug 04 '14 at 12:46
1

You can use this javascript code

<script> var iDiv = document.createElement('div'); iDiv.className = 'item'; var cfDiv = document.createElement('div'); cfDiv.className = 'clearfix'; var myWidth=screen.width; if(myWidth>=1024) { for(i=1; i<=2; i++) { for(j=1; j<=6; j++) { document.getElementsByTagName('body')[0].appendChild(iDiv); } document.getElementsByTagName('body')[0].appendChild(cfDiv); } } else { for(i=1; i<=3; i++) { for(j=1; j<=4; j++) { document.getElementsByTagName('body')[0].appendChild(iDiv); } document.getElementsByTagName('body')[0].appendChild(cfDiv); } } </script>

I think this will work.

happyCoding :D

GoalDone
  • 345
  • 1
  • 2
  • 14
0

Actuualy you would need to use javascript property screen.width and screen.height.Php cannot make any role in this.

See more answers and the opinions here

Community
  • 1
  • 1
Avinash Babu
  • 5,752
  • 2
  • 17
  • 24