How can I change every second row?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • madsgormlarsen@gmail.com

    How can I change every second row?

    Hi

    I am making a html table builder, and would like every second row to be
    blue. So for that purpose I need a number that changes on every
    iteration of a while loop, for exampel so that a value is 0 then 1 then
    0 a so forth.

    Any suggestions ?

    Mads

  • Nikola Veber

    #2
    Re: How can I change every second row?

    Well, if you get the results with

    while ($row = mysql_fetch_ass oc($result)){}

    you could have a integer variable defined before the loop

    $x = 0;
    while ($row = mysql_fetch_ass oc($result)){
    if ($x % 2 == 0) blue
    else ...
    }




    madsgormlarsen@ gmail.com wrote:[color=blue]
    > Hi
    >
    > I am making a html table builder, and would like every second row to be
    > blue. So for that purpose I need a number that changes on every
    > iteration of a while loop, for exampel so that a value is 0 then 1 then
    > 0 a so forth.
    >
    > Any suggestions ?
    >
    > Mads
    >[/color]

    Comment

    • madsgormlarsen@gmail.com

      #3
      Re: How can I change every second row?

      Thanks a lot, I totally cool.

      I had to ad $x=$x+1;

      but perhaps there is another way?

      Comment

      • Geoff Berrow

        #4
        Re: How can I change every second row?

        I noticed that Message-ID:
        <1119724912.172 354.91350@g47g2 000cwa.googlegr oups.com> from
        madsgormlarsen@ gmail.com contained the following:
        [color=blue]
        >
        >I had to ad $x=$x+1;
        >
        >but perhaps there is another way?[/color]

        $x++;

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • Nikola Veber

          #5
          Re: How can I change every second row?

          Sorry, forgot that :)

          Geoff Berrow wrote:[color=blue]
          > I noticed that Message-ID:
          > <1119724912.172 354.91350@g47g2 000cwa.googlegr oups.com> from
          > madsgormlarsen@ gmail.com contained the following:
          >
          >[color=green]
          >>I had to ad $x=$x+1;
          >>
          >>but perhaps there is another way?[/color]
          >
          >
          > $x++;
          >[/color]

          Comment

          • Martijn

            #6
            Re: How can I change every second row?

            >>> I had to add $x=$x+1;[color=blue][color=green][color=darkred]
            >>>
            >>> but perhaps there is another way?[/color]
            >>
            >>
            >> $x++;[/color][/color]

            Some other approaches:

            $x = 0;
            if ( ++$x % 2 )
            {
            ...
            }

            would be even shorter, or:

            $x = 0;
            if ( $x = ~$x )
            {
            ...
            }

            would also work.

            --
            Martijn



            Comment

            • R. Rajesh Jeba Anbiah

              #7
              [FAQ] Outputting alternate values (Was Re: How can I change every second row?)

              Q: How can I output alternate values from a set of values-list?
              A:

              1. Just loop through your values-list array:
              <?php
              $values_arr = array('A', 'B', 'C', 'D');
              for($i=0; $i<10; ++$i)
              {
              $value = current($values _arr) and next($values_ar r) or
              reset($values_a rr);
              echo $value;
              }
              ?>

              2. If you want just to flip-flop between two integers, use XOR logic:
              <?php
              $flip_num = 1;
              $flop_num = 5;
              for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flo p_num))
              echo $n;
              ?>

              The idea may be incorporated to echo a HTML table with alternate row
              colors, row styles (using CSS class), etc.

              ++++++
              @todo Better wording. Grammar cleanup. Someone may help.

              Comment

              • Jerry Stuckle

                #8
                Re: [FAQ] Outputting alternate values (Was Re: How can I change everysecond row?)

                R. Rajesh Jeba Anbiah wrote:[color=blue]
                > Q: How can I output alternate values from a set of values-list?
                > A:
                >
                >
                > 2. If you want just to flip-flop between two integers, use XOR logic:
                > <?php
                > $flip_num = 1;
                > $flop_num = 5;
                > for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flo p_num))
                > echo $n;
                > ?>[/color]

                Way more complicated than necessary. Much better:

                <?php
                $flip_num = 1;
                $flop_num = 5;
                for($i=0; $i<10; $i++)
                echo $i % 2 ? $flip_num : $flop_num;
                ?>

                KISS.
                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • R. Rajesh Jeba Anbiah

                  #9
                  Re: [FAQ] Outputting alternate values (Was Re: How can I change every second row?)

                  Jerry Stuckle wrote:[color=blue]
                  > R. Rajesh Jeba Anbiah wrote:[color=green]
                  > > Q: How can I output alternate values from a set of values-list?
                  > > A:
                  > >
                  > >
                  > > 2. If you want just to flip-flop between two integers, use XOR logic:
                  > > <?php
                  > > $flip_num = 1;
                  > > $flop_num = 5;
                  > > for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flo p_num))
                  > > echo $n;
                  > > ?>[/color]
                  >
                  > Way more complicated than necessary. Much better:
                  >
                  > <?php
                  > $flip_num = 1;
                  > $flop_num = 5;
                  > for($i=0; $i<10; $i++)
                  > echo $i % 2 ? $flip_num : $flop_num;
                  > ?>
                  >
                  > KISS.[/color]

                  Not really. Your logic requires an iterator variable $i; but none of
                  the above logic requires such. So, the above logics can easily be
                  incorporated while reading DB or using foreach or while loops.

                  --
                  <?php echo 'Just another PHP saint'; ?>
                  Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: [FAQ] Outputting alternate values (Was Re: How can I change everysecond row?)

                    R. Rajesh Jeba Anbiah wrote:[color=blue]
                    > Jerry Stuckle wrote:
                    >[color=green]
                    >>R. Rajesh Jeba Anbiah wrote:
                    >>[color=darkred]
                    >>>Q: How can I output alternate values from a set of values-list?
                    >>>A:
                    >>>
                    >>>
                    >>> 2. If you want just to flip-flop between two integers, use XOR logic:
                    >>><?php
                    >>>$flip_num = 1;
                    >>>$flop_num = 5;
                    >>>for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flo p_num))
                    >>> echo $n;
                    >>>?>[/color]
                    >>
                    >>Way more complicated than necessary. Much better:
                    >>
                    >><?php
                    >> $flip_num = 1;
                    >> $flop_num = 5;
                    >> for($i=0; $i<10; $i++)
                    >> echo $i % 2 ? $flip_num : $flop_num;
                    >>?>
                    >>
                    >>KISS.[/color]
                    >
                    >
                    > Not really. Your logic requires an iterator variable $i; but none of
                    > the above logic requires such. So, the above logics can easily be
                    > incorporated while reading DB or using foreach or while loops.
                    >
                    > --
                    > <?php echo 'Just another PHP saint'; ?>
                    > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com
                    >[/color]

                    Rajesh,

                    Your code also requires and iterator variable $i; additionally it
                    requires a second variable $n and uses a double xor logic.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    Working...