duanaigua4033 2014-08-04 12:06
浏览 12
已采纳

根据设备宽度进行计数器更改

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!

  • 写回答

3条回答 默认 最新

  • drudfe0446838 2014-08-04 12:21
    关注

    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  
            }
        } 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序