Jump to content
randyinla

Changing $varCount & $row parameters in PHP

Recommended Posts

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>';
        }


    ?>

 

Share this post


Link to post
Share on other sites

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++;
    }
}
?>

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

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