dpus81500574 2013-06-13 12:52
浏览 46
已采纳

偶数/奇数确定CSS类

I've read a lot of information about the determination of a even/odd number and using it to change the class of a div.

In my case I want to switch the position of divs called MessagePicture and MessageText every new message posted.

  1. Picture left, Text right
  2. Picture right, Text left
  3. Picture left, Text right
  4. ect.

This is the code I am using to display the messages, I also included one of my tries to get the even/odd code to work.

Can anyone tell me what I should change to get it to work?

    <?PHP

    $Query =
    "
            SELECT
                ID,
                NameBox,
            MessageBox,
            Code
        FROM
            messages
            ORDER BY
                ID
            DESC LIMIT 10
    ";

    $Result = mysql_query($Query);

        if(!$Result)
            {
            echo 'ERROR: '.mysql_error();
            }

        else
            {
            if(mysql_num_rows($Result) == 0)
                {
                    echo 'No results';
                }

                else
                    {
                    $i = 0;
                $class = (++$i % 2) ? 'even' : 'odd';
                    while
                    ($Row = mysql_fetch_assoc($Result))

                        echo '

<div id="MessageWrapper">
    <div id="MessagePicture" class="'.$class.'">                                                                                
        <style>                                             
        #MessagePicture { 
        background-image: url(../../../Images/'.stripslashes($Row['Code']).'.png);  
        background-repeat: no-repeat; 
        background-position: center
        </style>
        </div>

    <div id="MessageText" class="'.$class.'">

        <div id="MessageTitle">

            <h1>'.$Row['NameBox'].'</h1>

        </div>

        <div id="MessageContent">                                                   
            <p>'.nl2br($Row['MessageBox']).'</p>
        </div>

    </div>

</div>  

';}}}?>
  • 写回答

2条回答 默认 最新

  • douyin8623 2013-06-13 12:58
    关注

    Your $i always stays 0. Add $i++ in the while loop to increment it.

    You can do that in one line:

    $class = ($i++ % 2 == 0) ? 'even' : 'odd';
    

    Full Example:

    $i = 0;
    while ($Row = mysql_fetch_assoc($Result)) {
      $class = ($i++ % 2 == 0) ? 'even' : 'odd';
      //echo ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划