Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

Changing $varCount & $row parameters in PHP


This topic is 2885 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

Hi, I'd like to creat a sorting order  with a $varCount like:

1

1

2

2

3

3

 

and alternating row colors that will print:

pink

pink

yellow

yellow

pink

pink

Here is the kind of coding I'm using that works fine for sequential numbering and odd/even row colors.  I appreciate anyone who can help me change these parameters, as I have tried every combination I could think of.

Thanks!

<?php

$row = 0;

$varCount = 0;

foreach($records as $record) {

$varCount = $varCount + 1;

if($row % 2 == 0)
        {
        echo '<tr class="alt_row_color">';
        }
    else
        {
        '<tr>';
        }


    ?>

 

Posted

Try this:

<?php
$row=0;
$bgcolor = 1;
foreach($records as $record){
    $row++;
    if($bgcolor == 3){
        $bgcolor = 1;
    }
    if($bgcolor == 1){
        echo '<tr class="alt_row_color">';// ...etc
        }
    else
        {
        echo '<tr>';// ...etc
        }
    if($row % 2 == 0){
        $bgcolor++;
    }
}
?>

 

Posted

Alternating rows is fairly simple - this divides by two and returns the remainder - 0 for an even row, 1 for an odd row

if($row % 2 == 0)

For double rows, I would probably divide by four, and check the remainder instead - remainders of 1 and 2 are one row colour, 3 and 0 (as exactly divisible by 4) would get the other row colour

 

This topic is 2885 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.