SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Compare value to last value in loop

    Hi all

    This is probably really simple but I stumble through code!

    I have:

    <?php while($rank=mysql_fetch_assoc($ranks)){?>

    <?php echo $rank['rank'];?></br>

    <?php }?>

    This prints out numbers:

    2
    6
    7
    3

    etc.

    I want to style the output depending on if the value is higher or lower than the last in the loop

    so...

    2 is the first so would be black
    6 is higher than 2 so would be green
    7 is higher than 6 so would be green
    3 is lower than 7 so would be red

    I understand that I would use styles to style the output, I will create a class of green and red but how do I compare to the last in the loop. This would be an if statement

    if $rank['rank'] > ?????????? {echo "green";}
    else {echo "red";}

    Am I on the right lines?

    some help would be great

    James

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to store the previous rank value as you progress through the loop, like below:

    PHP Code:
    <?php
    $previous 
    0;
    while (
    $rank=mysql_fetch_assoc($ranks))
    {
        if (
    $rank $previous)
        {
            
    // ...
        
    }
        else
        {
            
    // ...
        
    }
        
        
    $previous rank;
    }
    ?>
    I hope that this helps!

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much, I have done that and it works to an extent I have:


    $previousRank = 0;

    while($rank=mysql_fetch_assoc($ranks)){
    if ($previousRank == 0 )
    {
    echo $rank['rank'];
    }
    elseif ($rank < $previousRank)
    {
    echo "<span class=\"valueup\">".$rank['rank']."</span>";
    }
    elseif ($rank > $previousRank)
    {
    echo "<span class=\"valuedown\">".$rank['rank']."</span>";
    }
    $previousRank = $rank['rank'];
    )


    however this means that the first value is genuinly black, but if we get the scenario:

    3
    0
    4

    Then the middle 0 will be black where I need it to be red

    I could just accept that all the first row will be green, but its not ideal

    James

  4. #4
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried setting

    $previousRank = null;

    and then

    if ($previousRank == null )
    {
    echo $rank['rank'];
    }

    but this then means that if I have:

    0
    0
    2
    3

    then the fist 0 is black which is correct, but then the second 0 is red which it is not it is the same

  5. #5
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should then use something like this:

    PHP Code:
    <?php
    if ($previousRank === null

        echo 
    $rank['rank']; 

    ?>
    The === is the identical comparison operator (PHP: Comparison Operators - Manual).

  6. #6
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah schoolboy error!

    That woks until I have two 0's together when the second is red i.e. it is saying it is lower than the last 0

    I have:

    PHP Code:
    <?php
    $site
    =mysql_fetch_assoc(mysql_query("select * from site where id=$_GET[id]"));
    $ranks=mysql_query("select * from site_rank where siteid=$_GET[id]");


    $previousRank "";

    while(
    $rank=mysql_fetch_assoc($ranks)){

         if (
    $previousRank === null )     
         {         
         echo 
    $rank['rank'];   
         }     
         elseif (
    $rank $previousRank
         {         
          echo 
    "<span class=\"valueup\">".$rank['rank']."</span>"
         }   
         elseif  (
    $rank $previousRank
         {
          echo 
    "<span class=\"valuedown\">".$rank['rank']."</span>";
         }
         else
         {
         echo 
    $rank['rank'];   
         }
        
    $previousRank $rank['rank'];
    }
    This works for all cases as far as I can tell (1 followed by a 1 stays black, 3 followed by a 2 goes red) apart from where I have two 0's where the second goes red and a 0 followed by a 2 stays black.

    I have tried both:

    $previousRank = "";
    and
    $previousRank = null;


    at the top of the script
    I am sure I have probably does somethign really simple and silly

    Thanks

    James

  7. #7
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Update

    I missed the === part there so I have changed it but now:

    3 followed by a 2 both the 3 and 2 are red (3 being the first in the loop)
    0 followed by 0 both are red

  8. #8
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have echoed $previousRank on each loop and it is empty, I think this is my problem.

  9. #9
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that you have a few problems:

    - First, you're using $rank['rank'] and $rank interchangeably, but the values are clearly different. $rank is an entire array, whilst $rank['rank'] is a single entry of said array. I'm not sure what PHP will return when comparing an array to a numeric value, but it probably isn't want you expect.

    - Second, if $rank < $previousRank, this means that the value has gone down, not up. I think that you have these two confused.


Bookmarks

Posting Permissions

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