简体   繁体   English

PHP垂直交替显示单元格颜色

[英]PHP alternating cell colors vertically

I am trying to show a table that shows different packages for membership and need to try to get each column to alternate colors to separate the packages. 我正在尝试显示一个表格,该表格显示了不同的会员资格软件包,并且需要尝试使每一列具有其他颜色以分隔软件包。 I have the code for alternating cells but they are doing them horizontal and not vertically. 我有交替单元格的代码,但是它们在水平而不是垂直方向上执行。 Please let me know if this is possible and where I have my code wrong to alternate the cells vertically. 请让我知道这是否可行,以及我的代码在哪里错误以垂直替换单元格。 Thank you 谢谢

<table width="100%" border="0" cellspacing="0">
    <tr>
        <td>&nbsp;</td>
            <?php 
            foreach((array) $membership_names as $name) { ?>
            <td align="center" style="background:#ffffe4; padding:5px; border-top:1px solid #ccc; border-right:1px solid #fff; border-left:1px solid #fff;"><strong><?php echo $this->escape($name); ?></strong></td>
            <?php } ?>
    </tr>
            <?php 
            $i = 1; // Our row counter
            foreach((array) $memberships as $membership) { 
                if ($i % 2 != 0) # An odd row
                    $rowStyle = "background:#FFFFE4; border-bottom:1px solid #ccc; border-top:1px solid #fff; height:40px; padding:10px;";
                else # An even row
                    $rowStyle = "background:#FFF4D8; border-bottom:1px solid #ccc; border-top:1px solid #fff; height:40px; padding:10px;";
                ?>
    <tr>
        <td align="left" style="<?php echo $rowStyle; ?>"><strong><?php echo $this->escape($membership['title']); ?>:</strong></td>
        <?php foreach((array) $membership['values'] as $value) { 
                if ($i % 2 != 0) # An odd row
                    $rowStyle = "background:#FFFFE4; border-bottom:1px solid #ccc; border-top:1px solid #fff; height:40px; padding:10px;";
                else # An even row
                    $rowStyle = "background:#FFF4D8; border-bottom:1px solid #ccc; border-top:1px solid #fff; height:40px; padding:10px;";
                    ?>
            <td align="center" style="<?php echo $rowStyle; ?>">
                <?php if($value == 'yes') { ?>
                    <img src="<?php echo BASE_URL.TEMPLATE_PATH; ?>images/icon_checkmark.gif" />
                <?php } elseif($value == '-') {?> 
                                            <img src="<?php echo BASE_URL.TEMPLATE_PATH; ?>images/icon_checkno.gif" />
                                    <?php } elseif($value == '0') {?>
                                            <img src="<?php echo BASE_URL.TEMPLATE_PATH; ?>images/icon_checkno.gif" />
                                    <?php } else { ?>
                    <?php echo $this->escape($value); ?>
                <?php } ?>
            </td>
        <?php } ?>
        </tr>

                    <?php 
                    $i++; # Increment our row counter
                    } 
                    ?>

                    <tr>
                    <td align="left" valign="top" style="background:#9C6C69; color: #FFFFE4; border-bottom:1px solid #ccc; border-top:1px solid #fff; font-weight:bold; height:40px; padding:10px;">&nbsp;</td>
                    <?php foreach((array) $pricing as $price) { ?>
                            <td align="center" valign="top" style="background:#9C6C69; border-bottom:1px solid #ccc; border-top:1px solid #fff; color: #FFFFE4; height:40px; padding:10px;">
                            <?php foreach($price['pricing'] as $key=>$value) { ?>
                                    <?php if(count($price['pricing']) > 1) { ?><p><strong><?php echo $lang['public_compare_option']; ?> <?php echo $key+1; ?></strong></p><?php } ?>
                                    <?php if($value['label']) { ?>
                                            <p style="font-size:11px;"><?php echo $this->escape($value['label']); ?></p>
                                    <?php } else { ?>
                                            <?php //echo $lang['public_compare_term']; ?>
                                            <?php if($value['period_count']) { ?>
                                                     <p style="font-size:11px; font-weight:bold;"><?php echo $this->escape($value['period_count']); ?> <?php echo $this->escape($value['period']); ?></p>
                                            <?php } else { ?>
                                                    <p><?php echo $lang['public_compare_lifetime']; ?></p>
                                            <?php } ?>
                                            <?php if($value['setup_price'] != '0.00') { ?> 
                                                    <p><?php echo $lang['public_compare_setup']; ?>: <?php echo $this->escape(format_number_currency($value['setup_price'])); ?></p> 
                                            <?php } ?>
                                            <?php //echo $lang['public_compare_price']; ?>
                                            <?php if($value['price'] != '0.00') { ?>
                                                    <p style="border-bottom: 1px dashed #FFFFFF; border-top: 1px dashed #FFFFFF; font-size: 14px; font-weight: bold; margin-top: 2px; padding: 2px;">$<?php echo $this->escape($value['price']); ?></p>
                                            <?php } else { ?>
                                                    <p style="border-bottom: 1px dashed #FFFFFF; border-top: 1px dashed #FFFFFF; font-size: 14px; font-weight: bold; margin-top: 2px; padding: 2px;"><?php echo $lang['public_compare_free']; ?></p>
                                            <?php } ?>
                                    <?php } ?>
                                    <p><a href="<?php echo BASE_URL.MEMBERS_FOLDER; ?>user_orders_add_listing.php?pricing_id=<?php echo $value['id']; ?>" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="margin-top:3px;padding:5px;"><?php echo $lang['public_compare_order']; ?></a></p>
                            <?php } ?>
                            </td>
                    <?php } ?>
                    </tr>

                </table>

ok cracks knuckles , lets try this: remove the (array) from the foreach statements, you don't need it. 确定裂缝的指关节 ,让我们试试这个:删除从在foreach语句(阵列),你不需要它。 Also, the $i %2 != 0, you don't need all that, just use $i %2, it is either true or false, so if($i %2) will either be true or false, you don't need the != 0. Instead of having two different $rowstyles, just have 2 of them setup in your CSS, and use a php short if to pick which one to use, like this 此外,$ i%2!= 0,您不需要所有这些,只需使用$ i%2,它为true或false,所以if($ i%2)为true或false时,您不需要不需要!=0。与其使用两个不同的$ rowstyles,不如在CSS中设置其中两个,并使用php short来选择要使用的php,例如

($i %2 ? 'style1' : 'style2')

and put it in your table cell like this: 并将其放在您的表格单元格中,如下所示:

<td style="($i %2 ? 'style1' : 'style2')">

That will alternate the styles depending on whether your $i is even or odd. 这将根据您的$ i是偶数还是奇数来替换样式。 You also need to make sure that your $i is reset to 0 at the beginning of each row, since you are alternating column colors, you want the rows to all start with 0 so they line up correctly. 您还需要确保在每行的开头将$ i重置为0,因为您要交替使用列颜色,所以您希望所有行都从0开始,以便它们正确对齐。 You don't need to track the rows with a $i, since you don't care what color they are and are only concerned with columns, just make sure to reset $i to 0 every time you start a new table row. 您不需要跟踪带有$ i的行,因为您不在乎它们是什么颜色,并且只关心列,因此只需确保每次启动新表行时都将$ i重置为0。

You are using $i two times in two nested loops, which is bound to fail. 您在两个嵌套循环中两次使用$i ,注定会失败。 Also try to reset it before the second foreach loop. 也尝试在第二个foreach循环之前将其重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM