Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Array problem

  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    257
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array problem

    I have been trying, but I can't figure how to do these. Let say I have an array below that has similar values.

    $out[0]="144";
    $out[1]="144";
    $out[2]="133";
    $out[3]="133";
    $out[4]="133";
    $out[5]="288";
    $out[6]="288";

    How can I output this in an html table using foreach loop in which before a value change to a different value, the background color will be different. Kinda like this.


  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,167
    Thanks
    11
    Thanked 105 Times in 103 Posts
    one way of many...

    PHP Code:
    <?php
    $lastval
    =0;
    foreach(
    $out as $v){
        
    $class=($lastval!=$v) ? 'highlightdiv' 'regdiv' ;
        echo 
    "<div class=\"{$class}\">$v</div>";
        
    $lastval=$v;
    }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Jun 2013
    Location
    The Republic of Texas
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    That is a good way, however it looks like they want to highlight the row immediately BEFORE the value change. Something like this for numerical indexes only (not tested):
    PHP Code:
    foreach($out as $k => $v) {
        
    $class 'regdiv';

        if(isset(
    $out[$k 1]) && $out[$k 1] != $v) {
            
    $class 'highlightdiv';
        }
        echo 
    "<div class=\"{$class}\">$v</div>";

    Or for numeric or associative indexes (not tested):
    PHP Code:
    foreach($out as $v) {
        
    $class 'regdiv';

        if((
    $next next($out)) !== false && $next != $v) {
            
    $class 'highlightdiv';
        }
        
    prev($out);
        echo 
    "<div class=\"{$class}\">$v</div>";


  • #4
    Regular Coder
    Join Date
    May 2010
    Posts
    257
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the answer, yes I want to highlight the value, before it change.

  • #5
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,485
    Thanks
    26
    Thanked 754 Times in 752 Posts
    PHP Code:
    <?php
    $out
    [0]="144";
    $out[1]="144";
    $out[2]="133";
    $out[3]="133";
    $out[4]="133";
    $out[5]="288";
    $out[6]="288";
    $last '';
    echo 
    "<table border='1'>";
    for(
    $i=1$i sizeof($out); $i++){
        
    $y $i 1;
        
    $line "<tr><td>" $out[$y] . "</td></tr>";
        
    $colorline "<tr><td bgcolor='pink'>" $out[$y] . "</td></tr>";
        if(
    $out[$i] == $out[$y]){
            echo 
    $line;
        }else{
            echo 
    $colorline;
        }
    }
    echo 
    "<tr><td>" $out[$y] . "</td></tr>";
    echo 
    "</table>";
    ?>
    Last edited by sunfighter; 09-16-2013 at 07:44 PM.
    Evolution - The non-random survival of random variants.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •