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
    219
    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,093
    Thanks
    11
    Thanked 101 Times in 99 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
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the answer, yes I want to highlight the value, before it change.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,441
    Thanks
    23
    Thanked 630 Times in 629 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.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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