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”;}
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
$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
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.